/
views.py
165 lines (146 loc) · 5.49 KB
/
views.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Create your views here.
from django.template import Template, Context
from django.http import HttpResponse
from django.template.loader import render_to_string
from django.shortcuts import render_to_response
from django.core import serializers
from django.contrib.admindocs.views import load_all_installed_template_libraries
from django.contrib.admindocs import utils
from django.template.context import RequestContext
from django import template
from BeautifulSoup import BeautifulSoup
import re
def parse_ttag(token):
bits = token.split()
tags = {}
possible_tags = ['as', 'for', 'limit', 'exclude']
for index, bit in enumerate(bits):
if bit.strip() in possible_tags:
tags[bit.strip()] = bits[index+1]
return tags
def context_for_object(token, Node):
"""This is a function that returns a Node.
It takes a string from a template tag in the format
TagName for [object] as [context variable]
"""
tags = parse_ttag(token)
if len(tags) == 2:
return Node(tags['for'], tags['as'])
elif len(tags) == 1:
return Node(tags['for'])
else:
#raise template.TemplateSyntaxError, "%s: Fail" % bits[]
print "ERROR"
def _get_tags():
load_all_installed_template_libraries()
tags = []
for module_name, library in template.libraries.items():
for tag_name, tag_func in library.tags.items():
title, body, metadata = utils.parse_docstring(tag_func.__doc__)
if title:
title = utils.parse_rst(title, 'tag', ('tag:') + tag_name)
if body:
body = utils.parse_rst(body, 'tag', ('tag:') + tag_name)
for key in metadata:
metadata[key] = utils.parse_rst(metadata[key], 'tag', ('tag:') + tag_name)
if library in template.builtins:
tag_library = None
else:
tag_library = module_name.split('.')[-1]
tags.append({
'name': tag_name,
'title': title,
'body': body,
'meta': metadata,
'library': tag_library,
})
return tags
def _get_filters():
load_all_installed_template_libraries()
filters = []
for module_name, library in template.libraries.items():
for filter_name, filter_func in library.filters.items():
title, body, metadata = utils.parse_docstring(filter_func.__doc__)
if title:
title = utils.parse_rst(title, 'filter', ('filter:') + filter_name)
if body:
body = utils.parse_rst(body, 'filter', ('filter:') + filter_name)
for key in metadata:
metadata[key] = utils.parse_rst(metadata[key], 'filter', ('filter:') + filter_name)
if library in template.builtins:
tag_library = None
else:
tag_library = module_name.split('.')[-1]
filters.append({
'name': filter_name,
'title': title,
'body': body,
'meta': metadata,
'library': tag_library,
})
return filters
def index(request):
return render_to_response('playground/index.html',
{'tags': _get_tags() },
context_instance=RequestContext(request))
def tag(request, tag):
tags = _get_tags()
this_tag = None
for _tag in tags:
if _tag['name'].strip() == tag.strip():
this_tag = _tag
soup = BeautifulSoup(this_tag['body'])
code = soup.find('pre')
code = re.sub('<.*>', '', str(code), count=99)
tag_re = re.compile('({% .* %})')
match = tag_re.search(code)
as_var = ""
if match:
__tags = parse_ttag(match.group(1))
if 'as' in __tags:
as_var = __tags['as']
return render_to_response('playground/tag.html',
{ 'tag_obj': this_tag,
'tags': _get_tags(),
'template_code': code,
'as_var': as_var },
context_instance=RequestContext(request))
def filter(request, filter):
filters = _get_filters()
this_filter = None
for _filter in filters:
if _filter['name'].strip() == filter.strip():
this_filter = _filter
soup = BeautifulSoup(this_filter['body'])
code = soup.find('pre')
code = re.sub('<.*>', '', str(code), count=99)
filter_re = re.compile('({% .* %})')
match = filter_re.search(code)
as_var = ""
if match:
__filters = parse_tfilter(match.group(1))
if 'as' in __filters:
as_var = __filters['as']
return render_to_response('playground/filter.html',
{ 'filter_obj': this_filter,
'filters': _get_filters(),
'template_code': code,
'as_var': as_var },
context_instance=RequestContext(request))
def render_template(request):
context = request.POST['context']
context_dict = {}
for line in context.split('\n'):
try:
name, val = line.split(':')
context_dict[name] = val.strip()
except: #Blank lines
pass
try:
c = Context(context_dict)
template = request.POST['template']
t = Template(template)
string = t.render(c)
except Exception, e:
string = e
return HttpResponse(string)