coding utf-8 from django import template from django template defaultf

  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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# coding: utf-8
from django import template
from django.template.defaultfilters import stringfilter
import string
import sys
register = template.Library()
@register.inclusion_tag('menu.html')
def include_menu(menu, menu_id, request):
return {
'menu_id': menu_id,
'menu': menu,
'request': request,
}
@register.simple_tag
def trans_class_vn_plural(cls):
parts = string.split(force_unicode(cls._meta.verbose_name_plural))
parts[0] = string.capitalize(parts[0])
return ' '.join(parts)
@register.simple_tag
def trans_class_vn(cls):
parts = string.split(force_unicode(cls._meta.verbose_name))
parts[0] = string.capitalize(parts[0])
return ' '.join(parts)
@register.simple_tag
def class_raw_vn_name(cls):
return cls._meta.verbose_name_raw
@register.filter
@stringfilter
def strip(value):
return value.strip()
@register.simple_tag
def trans_app_name(app_name):
app_name = app_name.lower()
if app_name == 'auth':
return u'Пользователи'
elif app_name == 'comments':
return u'Комментарии'
else:
try:
app = sys.modules[app_name]
return app.app_label.capitalize()
except:
try:
app = sys.modules["apps.%s" % app_name]
return app.app_label.capitalize()
except:
return app_name.capitalize()
@register.inclusion_tag('sidebox.html', takes_context=True)
def include_sidebox(context, objects, title, link_text, no):
try:
index_url = reverse("%s_index" % objects)
except:
index_url = reverse(objects)
return {
'objects': context["cp_%s" % objects],
'title': title,
'index_url': index_url,
'link_text': link_text,
'no': no,
}
@register.tag
def columnize(parser, token):
"""
Put stuff into columns. Can also define class tags for rows and cells
Usage: {% columnize num_cols [row_class[,row_class2...]|'' [cell_class[,cell_class2]]] %}
num_cols: the number of columns to format.
row_class: can use a comma (no spaces, please) separated list that cycles
(utilizing the cycle code) can also put in '' for nothing,
if you want no row_class, but want a cell_class.
cell_class: same format as row_class, but the cells only loop within a row.
Every row resets the cell counter.
Typical usage:
<table border="0" cellspacing="5" cellpadding="5">
{% for o in some_list %}
{% columnize 3 %}
<a href="{{ o.get_absolute_url }}">{{ o.name }}</a>
{% endcolumnize %}
{% endfor %}
</table>
"""
nodelist = parser.parse(('endcolumnize',))
parser.delete_first_token()
#Get the number of columns, default 1
columns = 1
row_class = ''
cell_class = ''
args = token.contents.split(None, 3)
num_args = len(args)
if num_args >= 2:
#{% columnize columns %}
if args[1].isdigit():
columns = int(args[1])
else:
raise template.TemplateSyntaxError('The number of columns must be a number. "%s" is not a number.') % args[2]
if num_args >= 3:
#{% columnize columns row_class %}
if "," in args[2]:
#{% columnize columns row1,row2,row3 %}
row_class = [v for v in args[2].split(",") if v] # split and kill blanks
else:
row_class = [args[2]]
if row_class == "''":
# Allow the designer to pass an empty string (two quotes) to skip the row_class and
# only have a cell_class
row_class = []
if num_args == 4:
#{% columnize columns row_class cell_class %}
if "," in args[3]:
#{% columnize row_class cell1,cell2,cell3 %}
cell_class = [v for v in args[3].split(",") if v] # split and kill blanks
else:
cell_class = [args[3]]
if cell_class == "''":
# This shouldn't be necessary, but might as well test for it
cell_class = []
return ColumnizeNode(nodelist, columns, row_class, cell_class)
class ColumnizeNode(template.Node):
def __init__(self, nodelist, columns = 1, row_class = '', cell_class = ''):
self.nodelist = nodelist
self.columns = int(columns)
self.counter = 0
self.rowcounter = -1
self.cellcounter = -1
self.row_class_len = len(row_class)
self.row_class = row_class
self.cell_class_len = len(cell_class)
self.cell_class = cell_class
def render(self, context):
output = ''
self.counter += 1
if (self.counter > self.columns):
self.counter = 1
self.cellcounter = -1
if (self.counter == 1):
output = '<tr'
if self.row_class:
self.rowcounter += 1
output += ' class="%s">' % self.row_class[self.rowcounter % self.row_class_len]
else:
output += '>'
output += '<td'
if self.cell_class:
self.cellcounter += 1
output += ' class="%s">' % self.cell_class[self.cellcounter % self.cell_class_len]
else:
output += '>'
output += self.nodelist.render(context) + '</td>'
forloop = context['forloop']
if self.counter == self.columns or forloop['last'] and forloop['counter0'] % self.columns != (self.columns - 1):
if (self.columns < self.counter):
for i in range(0,self.columns-self.counter):
output += '<td>&nbsp;</td>'
output += '</tr>'
return output