~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to threadedcomments/utils.py

  • Committer: franku
  • Date: 2016-05-15 14:41:54 UTC
  • mto: This revision was merged to the branch mainline in revision 409.
  • Revision ID: somal@arcor.de-20160515144154-00m3tiibyxm0nw2w
added the old threadedcomments app as wildelands app

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from django.core.serializers import serialize
 
2
from django.http import HttpResponse
 
3
from django.utils.functional import Promise
 
4
from django.utils.encoding import force_unicode 
 
5
import json as simplejson
 
6
 
 
7
class LazyEncoder(simplejson.JSONEncoder):
 
8
    def default(self, obj):
 
9
        if isinstance(obj, Promise):
 
10
            return force_unicode(obj)
 
11
        return obj
 
12
 
 
13
class JSONResponse(HttpResponse):
 
14
    """
 
15
    A simple subclass of ``HttpResponse`` which makes serializing to JSON easy.
 
16
    """
 
17
    def __init__(self, object, is_iterable = True):
 
18
        if is_iterable:
 
19
            content = serialize('json', object)
 
20
        else:
 
21
            content = simplejson.dumps(object, cls=LazyEncoder)
 
22
        super(JSONResponse, self).__init__(content, mimetype='application/json')
 
23
 
 
24
class XMLResponse(HttpResponse):
 
25
    """
 
26
    A simple subclass of ``HttpResponse`` which makes serializing to XML easy.
 
27
    """
 
28
    def __init__(self, object, is_iterable = True):
 
29
        if is_iterable:
 
30
            content = serialize('xml', object)
 
31
        else:
 
32
            content = object
 
33
        super(XMLResponse, self).__init__(content, mimetype='application/xml')
 
 
b'\\ No newline at end of file'