/
utils.py
30 lines (25 loc) · 1.06 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from django.utils import simplejson
from django.http import HttpResponse
from django.template import RequestContext
class JSONResponse(HttpResponse):
""" JSON response class """
def __init__(self,content='',json_opts={},mimetype="application/json",*args,**kwargs):
if content:
content = simplejson.dumps(content,**json_opts)
else:
content = simplejson.dumps([],**json_opts)
super(JSONResponse,self).__init__(content,mimetype,*args,**kwargs)
class ContextHackMixin(object):
def render_to_response(self, context, **response_kwargs):
"""
Returns a response with a template rendered with the given context.
"""
context['request'] = self.request
if hasattr(self, "get_extra_context"):
context.update(self.get_extra_context())
return self.response_class(
request = self.request,
template = self.get_template_names(),
context = RequestContext(self.request, context),
**response_kwargs
)