~widelands-dev/widelands-website/django_staticfiles

« back to all changes in this revision

Viewing changes to threadedcomments/utils.py

  • Committer: Holger Rapp
  • Date: 2010-09-26 13:30:30 UTC
  • Revision ID: sirver@gmx.de-20100926133030-ceirjf83vde91tyt
Added a simple events model to display dates on the homepage

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
 
 
8
 
class LazyEncoder(simplejson.JSONEncoder):
9
 
 
10
 
    def default(self, obj):
11
 
        if isinstance(obj, Promise):
12
 
            return force_unicode(obj)
13
 
        return obj
14
 
 
15
 
 
16
 
class JSONResponse(HttpResponse):
17
 
    """A simple subclass of ``HttpResponse`` which makes serializing to JSON
18
 
    easy."""
19
 
 
20
 
    def __init__(self, object, is_iterable=True):
21
 
        if is_iterable:
22
 
            content = serialize('json', object)
23
 
        else:
24
 
            content = simplejson.dumps(object, cls=LazyEncoder)
25
 
        super(JSONResponse, self).__init__(
26
 
            content, mimetype='application/json')
27
 
 
28
 
 
29
 
class XMLResponse(HttpResponse):
30
 
    """A simple subclass of ``HttpResponse`` which makes serializing to XML
31
 
    easy."""
32
 
 
33
 
    def __init__(self, object, is_iterable=True):
34
 
        if is_iterable:
35
 
            content = serialize('xml', object)
36
 
        else:
37
 
            content = object
38
 
        super(XMLResponse, self).__init__(content, mimetype='application/xml')