Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
27e89539
Commit
27e89539
authored
Jun 25, 2013
by
David Baumgold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added generic JsonResponse class
Based on
http://djangosnippets.org/snippets/154/
parent
307c6c17
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
6 deletions
+26
-6
cms/djangoapps/contentstore/views/course.py
+5
-6
common/djangoapps/util/json_request.py
+21
-0
No files found.
cms/djangoapps/contentstore/views/course.py
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
HttpResponse
(
json
.
dumps
(
course_module
.
pdf_textbooks
),
content_type
=
"application/json"
)
return
JsonResponse
(
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
,
...
...
common/djangoapps/util/json_request.py
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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment