~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to threadedcomments/utils.py

  • Committer: franku
  • Date: 2016-12-13 18:28:51 UTC
  • mto: This revision was merged to the branch mainline in revision 443.
  • Revision ID: somal@arcor.de-20161213182851-bo5ebf8pdvw5beua
run the script

Show diffs side-by-side

added added

removed removed

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