Fixed requirements. Rewrote deployment script to use github.
[redakcja.git] / apps / wiki / helpers.py
1 from django import http
2 from django.utils import simplejson as json
3 from django.utils.functional import Promise
4 from django.template.loader import render_to_string
5 from datetime import datetime
6
7
8 class ExtendedEncoder(json.JSONEncoder):
9
10     def default(self, obj):
11         if isinstance(obj, Promise):
12             return unicode(obj)
13
14         if isinstance(obj, datetime):
15             return datetime.ctime(obj) + " " + (datetime.tzname(obj) or 'GMT')
16
17         return json.JSONEncoder.default(self, obj)
18
19
20 # shortcut for JSON reponses
21 class JSONResponse(http.HttpResponse):
22
23     def __init__(self, data={}, **kwargs):
24         # get rid of mimetype
25         kwargs.pop('mimetype', None)
26
27         super(JSONResponse, self).__init__(
28             json.dumps(data, cls=ExtendedEncoder),
29             mimetype="application/json", **kwargs)
30
31
32 # return errors
33 class JSONFormInvalid(JSONResponse):
34     def __init__(self, form):
35         super(JSONFormInvalid, self).__init__(form.errors, status=400)
36
37
38 class JSONServerError(JSONResponse):
39     def __init__(self, *args, **kwargs):
40         kwargs['status'] = 500
41         super(JSONServerError, self).__init__(*args, **kwargs)