cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
templatetags.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.wi.commontags.templatetags.templatetags
22 # @author Piotr Wójcik
23 # @author Krzysztof Danielowski
24 # @author Przemyslaw Syktus
25 # @date 11.02.2010
26 #
27 from django import template
28 from django.utils.translation import ugettext
29 from django.core.urlresolvers import get_resolver
30 
31 register = template.Library()
32 
33 
34 @register.inclusion_tag('tags/resizable_field.html')
35 ##
36 #
37 # Renders a resizable field.
38 #
39 def resizable_field(field_name):
40  return {'field_name': field_name}
41 
42 
43 @register.filter
44 ##
45 #
46 # Templatetag for fetching dictionary attribute values.
47 #
48 def getitem(item, string):
49  return item.get(string, '')
50 
51 
52 @register.simple_tag
53 def url_pattern(url_name):
54  resolver = get_resolver(None)
55  pattern = resolver.reverse_dict.getlist(url_name)[0][0][0][0]
56  return str(pattern)
57 
58 
59 @register.filter
60 ##
61 #
62 # Function formats the [mbytes] value like a 'human-readable' file size.
63 #
64 # (i.e. 13 KB, 4.1 MB, 102 bytes, etc).
65 #
66 def filesizeformatmb(mbytes):
67  try:
68  mbytes = float(mbytes)
69  except (TypeError, ValueError, UnicodeDecodeError):
70  return u"0 bytes"
71 
72  if mbytes < 1024:
73  return ugettext("%d MB") % (mbytes)
74  if mbytes < 1024 * 1024:
75  return ugettext("%.1f GB") % (mbytes / 1024)
76  if mbytes < 1024 * 1024 * 1024:
77  return ugettext("%.1f TB") % (mbytes / (1024 * 1024))
78  return ugettext("%.1f PB") % (mbytes / (1024 * 1024 * 1024))
79 filesizeformatmb.is_safe = True
80 
81 
82 ##
83 #
84 # jQuery templates use constructs like:
85 #
86 # {{if condition}} print something{{/if}}
87 #
88 # This, of course, completely screws up Django templates,
89 # because Django thinks {{ and }} means something.
90 #
91 # Wrap {% verbatim2 %} and {% endverbatim2 %} around those
92 # blocks of jQuery templates and this will try its best
93 # to output the contents with no changes.
94 #
95 # This version of verbatim template tag allows you to use tags
96 # like url {% url 'name' %} or {% csrf_token %} within.
97 #
98 class VerbatimNode(template.Node):
99  def __init__(self, text_and_nodes):
100  self.text_and_nodes = text_and_nodes
101 
102  def render(self, context):
103  output = ""
104 
105  # If its text we concatenate it, otherwise it's a node and we render it
106  for bit in self.text_and_nodes:
107  if isinstance(bit, basestring):
108  output += bit
109  else:
110  output += bit.render(context)
111 
112  return output
113 
114 
115 @register.tag
116 ##
117 #
118 # Templatetag used in workaround of the '{{' conflict between jquery templates and django templates.
119 #
120 def verbatim2(parser, token):
121  text_and_nodes = []
122  while 1:
123  token = parser.tokens.pop(0)
124  if token.contents == 'endverbatim2':
125  break
126 
127  if token.token_type == template.TOKEN_VAR:
128  text_and_nodes.append('{{')
129  text_and_nodes.append(token.contents)
130 
131  elif token.token_type == template.TOKEN_TEXT:
132  text_and_nodes.append(token.contents)
133 
134  elif token.token_type == template.TOKEN_BLOCK:
135  try:
136  command = token.contents.split()[0]
137  except IndexError:
138  parser.empty_block_tag(token)
139 
140  try:
141  compile_func = parser.tags[command]
142  except KeyError:
143  parser.invalid_block_tag(token, command, None)
144  try:
145  node = compile_func(parser, token)
146  except template.TemplateSyntaxError, ex:
147  if not parser.compile_function_error(token, ex):
148  raise
149 
150  text_and_nodes.append(node)
151 
152  if token.token_type == template.TOKEN_VAR:
153  text_and_nodes.append('}}')
154 
155  return VerbatimNode(text_and_nodes)
156