Package omeroweb :: Package webadmin :: Package templatetags :: Module custom_tags
[hide private]
[frames] | no frames]

Source Code for Module omeroweb.webadmin.templatetags.custom_tags

  1  #!/usr/bin/env python 
  2  #  
  3  #  
  4  #  
  5  # Copyright (c) 2008 University of Dundee.  
  6  #  
  7  # This program is free software: you can redistribute it and/or modify 
  8  # it under the terms of the GNU Affero General Public License as 
  9  # published by the Free Software Foundation, either version 3 of the 
 10  # License, or (at your option) any later version. 
 11  #  
 12  # This program is distributed in the hope that it will be useful, 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 15  # GNU Affero General Public License for more details. 
 16  #  
 17  # You should have received a copy of the GNU Affero General Public License 
 18  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 19  #  
 20  # Author: Aleksandra Tarkowska <A(dot)Tarkowska(at)dundee(dot)ac(dot)uk>, 2008. 
 21  #  
 22  # Version: 1.0 
 23  # 
 24   
 25   
 26  import datetime 
 27  import traceback 
 28  import logging 
 29   
 30  from django.conf import settings 
 31  from django import template 
 32   
 33  register = template.Library() 
 34   
 35  logger = logging.getLogger('custom_tags') 
36 37 38 @register.filter 39 -def hash(value, key):
40 return value[key]
41
42 @register.filter 43 -def truncateafter(value, arg):
44 """ 45 Truncates a string after a given number of chars 46 Argument: Number of chars to truncate after 47 """ 48 try: 49 length = int(arg) 50 except ValueError: # invalid literal for int() 51 return value # Fail silently. 52 if not isinstance(value, basestring): 53 value = str(value) 54 if (len(value) > length): 55 return value[:length] + "..." 56 else: 57 return value
58
59 @register.filter 60 -def truncatebefor(value, arg):
61 """ 62 Truncates a string after a given number of chars 63 Argument: Number of chars to truncate befor 64 """ 65 try: 66 length = int(arg) 67 except ValueError: # invalid literal for int() 68 return value # Fail silently. 69 if not isinstance(value, basestring): 70 value = str(value) 71 if (len(value) > length): 72 return "..."+value[len(value)-length:] 73 else: 74 return value
75
76 @register.filter 77 -def shortening(value, arg):
78 try: 79 length = int(arg) 80 except ValueError: # invalid literal for int() 81 return value # Fail silently. 82 front = length/2-3 83 end = length/2-3 84 85 if not isinstance(value, basestring): 86 value = str(value) 87 try: 88 l = len(value) 89 if l < length: 90 return value 91 elif l >= length: 92 return value[:front]+"..."+value[l-end:] 93 except Exception, x: 94 logger.error(traceback.format_exc()) 95 return value
96 97 # makes settings available in template 98 @register.tag
99 -def setting ( parser, token ):
100 try: 101 tag_name, option = token.split_contents() 102 except ValueError: 103 raise template.TemplateSyntaxError, "%r tag requires a single argument" % token.contents[0] 104 return SettingNode( option ) 105
106 -class SettingNode ( template.Node ):
107 - def __init__ ( self, option ):
108 self.option = option 109
110 - def render ( self, context ):
111 # if FAILURE then FAIL silently 112 try: 113 return str(settings.__getattr__(self.option)) 114 except: 115 return "" 116
117 -class PluralNode(template.Node):
118 - def __init__(self, quantity, single, plural):
119 self.quantity = template.Variable(quantity) 120 self.single = template.Variable(single) 121 self.plural = template.Variable(plural)
122
123 - def render(self, context):
124 if self.quantity.resolve(context) == 1: 125 return u'%s' % self.single.resolve(context) 126 else: 127 return u'%s' % self.plural.resolve(context)
128 129 @register.tag(name="plural")
130 -def do_plural(parser, token):
131 """ 132 Usage: {% plural quantity name_singular name_plural %} 133 134 This simple version only works with template variable since we will use blocktrans for strings. 135 """ 136 137 try: 138 # split_contents() knows not to split quoted strings. 139 tag_name, quantity, single, plural = token.split_contents() 140 except ValueError: 141 raise template.TemplateSyntaxError, "%r tag requires exactly three arguments" % token.contents.split()[0] 142 143 return PluralNode(quantity, single, plural)
144