Added generic JsonResponse class
Based on http://djangosnippets.org/snippets/154/
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user