-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlrender.py
79 lines (70 loc) · 2.65 KB
/
lrender.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/python
#-*coding:utf-8*-
from django.http import HttpResponse, Http404, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.conf import settings
import json
def lrender(tpl=None, auth=False, staff=False):
def func(f):
def infunc(req, *args, **kwargs):
if auth and not req.user.is_authenticated():
return HttpResponseRedirect(settings.LOGIN_URL +
'?next=' + req.path)
if staff and not req.user.is_staff:
raise Http404
r = f(req, *args, **kwargs)
if isinstance(r, HttpResponse):
return r
elif tpl and 'template' not in r:
return render_to_response(tpl, RequestContext(req, r))
else:
return render_to_response(
r.pop('template'), RequestContext(req, r))
return infunc
if callable(tpl):
return func(tpl)
return func
def ensure_staff(func):
def infunc(request, *argv, **kwargv):
if not request.user.is_staff:
raise Http404
else:
return func(request, *argv, **kwargv)
return infunc
class JError(Exception):
pass
def tojson(auth=True, staff=False, method=None, need_page=False):
def func(f):
def infunc(req, *args, **kwargs):
if method is not None:
if req.method != method:
return HttpResponse(u'{"error": "invalid http method"}',
'application/json')
if auth and not req.user.is_authenticated():
return HttpResponse(u'{"error": "login first"}',
'application/json')
if staff and not req.user.is_staff:
return HttpResponse(u'{"error": "staff required"}',
'application/json')
if need_page:
page = req.GET.get('page', '1')
if not all([x.isdigit() for x in page]):
return HttpResponse(u'{"error": "invalid page"}',
'application/json')
kwargs['page'] = int(page)
try:
r = f(req, *args, **kwargs)
except JError, e:
return HttpResponse(
'{"error": ' + json.dumps(unicode(e)) + '}',
'application/json')
if isinstance(r, bool):
r = {'ok': r}
elif r == None:
r = {}
return HttpResponse(json.dumps(r), 'application/json')
return infunc
if callable(auth):
return func(auth)
return func