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

Source Code for Module omeroweb.webgateway.templatetags.common_filters

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # #!/usr/bin/env python 
  4  #  
  5  #  
  6  #  
  7  # Copyright (c) 2008 University of Dundee.  
  8  #  
  9  # This program is free software: you can redistribute it and/or modify 
 10  # it under the terms of the GNU Affero General Public License as 
 11  # published by the Free Software Foundation, either version 3 of the 
 12  # License, or (at your option) any later version. 
 13  #  
 14  # This program is distributed in the hope that it will be useful, 
 15  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 16  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 17  # GNU Affero General Public License for more details. 
 18  #  
 19  # You should have received a copy of the GNU Affero General Public License 
 20  # along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 21  #  
 22  # Author: Aleksandra Tarkowska <A(dot)Tarkowska(at)dundee(dot)ac(dot)uk>, 2008. 
 23  #  
 24  # Version: 1.0 
 25  # 
 26   
 27   
 28  import datetime 
 29  import traceback 
 30  import logging 
 31   
 32  from django.conf import settings 
 33  from django import template 
 34   
 35  register = template.Library() 
 36   
 37  logger = logging.getLogger(__name__) 
38 39 40 @register.filter 41 -def hash(value, key):
42 return value[key]
43
44 @register.filter 45 -def ago(value):
46 """ Formats a datetime.datetime object as time Ago. E.g. '3 days 2 hours 10 minutes' """ 47 try: 48 ago = datetime.datetime.now() - value 49 except TypeError: 50 return str(value) 51 def plurals(val): 52 return val != 1 and "s" or ""
53 hours, remainder = divmod(ago.seconds, 3600) 54 mins, secs = divmod(remainder, 60) 55 if ago.days >= 365: 56 years = ago.days / 365 57 return "%s year%s" % (years, plurals(years)) 58 if ago.days > 28: 59 months = ago.days / 30 60 return "%s month%s" % (months, plurals(months)) 61 if ago.days > 0: 62 return "%s day%s" % (ago.days, plurals(ago.days)) 63 if hours > 0: 64 return "%s hour%s" % (hours, plurals(hours)) 65 if mins > 1: 66 return "%s minutes" % (mins) 67 if mins == 1: 68 return "a minute" 69 return "less than a minute" 70
71 @register.filter 72 -def truncateafter(value, arg):
73 """ 74 Truncates a string after a given number of chars 75 Argument: Number of chars to truncate after 76 """ 77 try: 78 length = int(arg) 79 except ValueError: # invalid literal for int() 80 return value # Fail silently. 81 if not isinstance(value, basestring): 82 value = str(value) 83 if (len(value) > length): 84 return value[:length] + "..." 85 else: 86 return value
87
88 @register.filter 89 -def truncatebefor(value, arg):
90 """ 91 Truncates a string after a given number of chars 92 Argument: Number of chars to truncate befor 93 """ 94 try: 95 length = int(arg) 96 except ValueError: # invalid literal for int() 97 return value # Fail silently. 98 if not isinstance(value, basestring): 99 value = str(value) 100 if (len(value) > length): 101 return "..."+value[len(value)-length:] 102 else: 103 return value
104
105 @register.filter 106 -def shortening(value, arg):
107 try: 108 length = int(arg) 109 except ValueError: # invalid literal for int() 110 return value # Fail silently. 111 front = length/2-3 112 end = length/2-3 113 114 if not isinstance(value, basestring): 115 value = str(value) 116 try: 117 l = len(value) 118 if l < length: 119 return value 120 elif l >= length: 121 return value[:front]+"..."+value[l-end:] 122 except Exception, x: 123 logger.error(traceback.format_exc()) 124 return value
125
126 # See https://code.djangoproject.com/ticket/361 127 @register.filter 128 -def subtract(value, arg):
129 "Subtracts the arg from the value" 130 return int(value) - int(arg)
131
132 133 # From http://djangosnippets.org/snippets/1357/ 134 @register.filter 135 -def get_range( value ):
136 """ 137 Filter - returns a list containing range made from given value 138 Usage (in template): 139 140 <ul>{% for i in 3|get_range %} 141 <li>{{ i }}. Do something</li> 142 {% endfor %}</ul> 143 144 Results with the HTML: 145 <ul> 146 <li>0. Do something</li> 147 <li>1. Do something</li> 148 <li>2. Do something</li> 149 </ul> 150 151 Instead of 3 one may use the variable set in the views 152 """ 153 return range( value )
154