-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_functions.py
44 lines (40 loc) · 1.5 KB
/
json_functions.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from django.http import HttpResponse
from django.utils import simplejson
from django.core.mail import mail_admins
from django.utils.translation import ugettext as _
import sys
def json_view(func):
def wrap(request, *a, **kw):
response = None
try:
response = func(request, *a, **kw)
assert isinstance(response, dict)
if 'result' not in response:
response['result'] = 'ok'
except KeyboardInterrupt:
# Allow keyboard interrupts through for debugging.
raise
except Exception, e:
# Mail the admins with the error
exc_info = sys.exc_info()
subject = 'JSON view error: %s' % request.path
try:
request_repr = repr(request)
except:
request_repr = 'Request repr() unavailable'
import traceback
message = 'Traceback:\n%s\n\nRequest:\n%s' % (
'\n'.join(traceback.format_exception(*exc_info)),
request_repr,
)
mail_admins(subject, message, fail_silently=True)
# Come what may, we're returning JSON.
if hasattr(e, 'message'):
msg = e.message
else:
msg = _('Internal error')+': '+str(e)
response = {'result': 'error',
'text': msg}
json = simplejson.dumps(response)
return HttpResponse(json, mimetype='application/json')
return wrap