You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
19 lines
607 B
19 lines
607 B
import json
|
|
from django.http import HttpResponse
|
|
|
|
MIMEANY = '*/*'
|
|
MIMEJSON = 'application/json'
|
|
MIMETEXT = 'text/plain'
|
|
|
|
|
|
def response_mimetype(request):
|
|
can_json = MIMEJSON in request.META['HTTP_ACCEPT']
|
|
can_json |= MIMEANY in request.META['HTTP_ACCEPT']
|
|
return MIMEJSON if can_json else MIMETEXT
|
|
|
|
|
|
class JSONResponse(HttpResponse):
|
|
def __init__(self, obj='', json_opts=None, mimetype=MIMEJSON, *args, **kwargs):
|
|
json_opts = json_opts if isinstance(json_opts, dict) else {}
|
|
content = json.dumps(obj, **json_opts)
|
|
super().__init__(content, mimetype, *args, **kwargs)
|
|
|