Package omeroweb :: Package webgateway :: Package templatetags :: Module common_tags
[hide private]
[frames] | no frames]

Source Code for Module omeroweb.webgateway.templatetags.common_tags

 1  #!/usr/bin/env python 
 2  # -*- coding: utf-8 -*- 
 3  #  
 4  #  
 5  #  
 6  # Copyright (c) 2008 University of Dundee.  
 7  #  
 8  # This program is free software: you can redistribute it and/or modify 
 9  # it under the terms of the GNU Affero General Public License as 
10  # published by the Free Software Foundation, either version 3 of the 
11  # License, or (at your option) any later version. 
12  #  
13  # This program is distributed in the hope that it will be useful, 
14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
16  # GNU Affero General Public License for more details. 
17  #  
18  # You should have received a copy of the GNU Affero General Public License 
19  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
20  #  
21  # Author: Aleksandra Tarkowska <A(dot)Tarkowska(at)dundee(dot)ac(dot)uk>, 2008. 
22  #  
23  # Version: 1.0 
24  # 
25   
26   
27  import datetime 
28  import traceback 
29  import logging 
30   
31  from django.conf import settings 
32  from django import template 
33   
34  register = template.Library() 
35   
36  logger = logging.getLogger(__name__) 
37   
38  # makes settings available in template 
39  @register.tag 
40 -def setting ( parser, token ):
41 try: 42 tag_name, option = token.split_contents() 43 except ValueError: 44 raise template.TemplateSyntaxError, "%r tag requires a single argument" % token.contents[0] 45 return SettingNode( option ) 46
47 -class SettingNode ( template.Node ):
48 - def __init__ ( self, option ):
49 self.option = option 50
51 - def render ( self, context ):
52 # if FAILURE then FAIL silently 53 try: 54 return str(settings.__getattr__(self.option)) 55 except: 56 return "" 57
58 -class PluralNode(template.Node):
59 - def __init__(self, quantity, single, plural):
60 self.quantity = template.Variable(quantity) 61 self.single = template.Variable(single) 62 self.plural = template.Variable(plural)
63
64 - def render(self, context):
65 if self.quantity.resolve(context) == 1: 66 return u'%s' % self.single.resolve(context) 67 else: 68 return u'%s' % self.plural.resolve(context)
69 70 @register.tag(name="plural")
71 -def do_plural(parser, token):
72 """ 73 Usage: {% plural quantity name_singular name_plural %} 74 75 This simple version only works with template variable since we will use blocktrans for strings. 76 """ 77 78 try: 79 # split_contents() knows not to split quoted strings. 80 tag_name, quantity, single, plural = token.split_contents() 81 except ValueError: 82 raise template.TemplateSyntaxError, "%r tag requires exactly three arguments" % token.contents.split()[0] 83 84 return PluralNode(quantity, single, plural)
85