Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
edx-platform-release
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
1
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hsin-Yu Chien
edx-platform-release
Commits
27e89539
Commit
27e89539
authored
11 years ago
by
David Baumgold
Browse files
Options
Downloads
Patches
Plain Diff
Added generic JsonResponse class
Based on
http://djangosnippets.org/snippets/154/
parent
307c6c17
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
cms/djangoapps/contentstore/views/course.py
+5
-6
5 additions, 6 deletions
cms/djangoapps/contentstore/views/course.py
common/djangoapps/util/json_request.py
+21
-0
21 additions, 0 deletions
common/djangoapps/util/json_request.py
with
26 additions
and
6 deletions
cms/djangoapps/contentstore/views/course.py
+
5
−
6
View file @
27e89539
...
...
@@ -7,8 +7,9 @@ from django.contrib.auth.decorators import login_required
from
django_future.csrf
import
ensure_csrf_cookie
from
django.conf
import
settings
from
django.core.exceptions
import
PermissionDenied
from
django.http
import
HttpResponse
,
HttpResponseBadRequest
from
django.core.urlresolvers
import
reverse
from
django.http
import
HttpResponse
,
HttpResponseBadRequest
from
util.json_request
import
JsonResponse
from
mitxmako.shortcuts
import
render_to_response
from
xmodule.modulestore.django
import
modulestore
...
...
@@ -447,18 +448,16 @@ def textbook_index(request, org, course, name):
if
request
.
is_ajax
():
if
request
.
method
==
'
GET
'
:
return
Http
Response
(
json
.
dumps
(
course_module
.
pdf_textbooks
)
,
content_type
=
"
application/json
"
)
return
Json
Response
(
course_module
.
pdf_textbooks
)
elif
request
.
method
==
'
POST
'
:
try
:
course_module
.
pdf_textbooks
=
validate_textbook_json
(
request
.
body
)
except
TextbookValidationError
as
e
:
msg
=
{
"
error
"
:
e
.
message
}
return
HttpResponseBadRequest
(
json
.
dumps
(
msg
),
content_type
=
"
application/json
"
)
return
JsonResponse
({
"
error
"
:
e
.
message
},
status
=
400
)
if
not
any
(
tab
[
'
type
'
]
==
'
pdf_textbooks
'
for
tab
in
course_module
.
tabs
):
course_module
.
tabs
.
append
({
"
type
"
:
"
pdf_textbooks
"
})
store
.
update_metadata
(
course_module
.
location
,
own_metadata
(
course_module
))
return
HttpResponse
(
''
,
content_type
=
"
application/json
"
,
status
=
204
)
return
JsonResponse
(
''
,
status
=
204
)
else
:
upload_asset_url
=
reverse
(
'
upload_asset
'
,
kwargs
=
{
'
org
'
:
org
,
...
...
This diff is collapsed.
Click to expand it.
common/djangoapps/util/json_request.py
+
21
−
0
View file @
27e89539
from
functools
import
wraps
import
copy
import
json
from
django.core.serializers
import
serialize
from
django.core.serializers.json
import
DjangoJSONEncoder
from
django.db.models.query
import
QuerySet
from
django.http
import
HttpResponse
def
expect_json
(
view_function
):
...
...
@@ -21,3 +25,20 @@ def expect_json(view_function):
return
view_function
(
request
,
*
args
,
**
kwargs
)
return
expect_json_with_cloned_request
class
JsonResponse
(
HttpResponse
):
"""
Django HttpResponse subclass that has sensible defaults for outputting JSON.
"""
def
__init__
(
self
,
object
=
None
,
*
args
,
**
kwargs
):
if
object
in
(
None
,
""
):
content
=
""
kwargs
.
setdefault
(
"
status
"
,
204
)
elif
isinstance
(
object
,
QuerySet
):
content
=
serialize
(
'
json
'
,
object
)
else
:
content
=
json
.
dumps
(
object
,
indent
=
2
,
cls
=
DjangoJSONEncoder
,
ensure_ascii
=
False
)
kwargs
.
setdefault
(
"
content_type
"
,
"
application/json
"
)
super
(
JsonResponse
,
self
).
__init__
(
content
,
*
args
,
**
kwargs
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment