-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorators.py
97 lines (79 loc) · 3.09 KB
/
decorators.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#django_utils/decorators.py
try:
import json
except ImportError:
# Python 2.5 fallback
import simplejson as json
from django.shortcuts import render
from django.http import HttpResponse
from functools import wraps
def templated(template):
"""Templating decorator
@param template string, template name, passed to django's render function
Decorated function result will be processed as:
- Dictionary: will be passed as context into render
- None: will be converted into empty dictionary and passed to render
- Any other case: result will be passed as output
"""
def decorated(f):
@wraps(f)
def rendered(request, *args, **kwargs):
_tpl = template
result = f(request, *args, **kwargs)
if result is None:
# Function returns nothing, convert into dict
result = {}
elif not isinstance(result, dict):
# Any other case, simply return result
return result
# Render template
return render(request, _tpl, result)
return rendered
return decorated
def pdfed(template, filename=None):
"""PDF output decorator, uses pisa to generate pdf
@param template string, template name, to be rendered as PDF
@param filename string/None, final filename, may be None
Decorated function result will be processed as:
- Dictionary: will be passed as context into render
- None: will be converted into empty dictionary and passed to render
- Any other case: result will be passed as output
"""
def decorated(f):
@wraps(f)
def rendered(*args, **kwargs):
_tpl = template
result = f(*args, **kwargs)
if result is None:
# Function returns nothing, convert into dict
result = {}
elif not isinstance(result, dict):
# Any other case, simply return result
return result
# Render pdf, using multipage_pdf function from django-utils package
from django_utils.pdf import multipage_pdf
return multipage_pdf([('index', _tpl, result),], filename)
return rendered
return decorated
def jsoned(f):
"""Jsoned decorator
Decorated function result will be processed as:
- Dictionary: will be passed as context into render
- String/Unicode: will be passed as 'status' in response {'status': %(result)}
- None: will be converted into empty dictionary and passed to render
- Any other case: result will be passed as output
"""
@wraps(f)
def rendered(*args, **kwargs):
result = f(*args, **kwargs)
if result is None:
# Function returns nothing, convert into dict
result = {}
elif isinstance(result, basestring):
# Function returns string, pass it as status value
result = {'status': result}
elif not isinstance(result, dict):
# Any other case, simply return result
return result
return HttpResponse(json.dumps(result))
return rendered