Package pytils :: Package templatetags :: Module pytils_dt
[hide private]

Source Code for Module pytils.templatetags.pytils_dt

  1  # -*- coding: utf-8 -*- 
  2  # -*- test-case-name: pytils.test.templatetags.test_dt -*-  
  3  # pytils - simple processing for russian strings 
  4  # Copyright (C) 2006-2007  Yury Yurevich 
  5  # 
  6  # http://www.pyobject.ru/projects/pytils/ 
  7  # 
  8  # This program is free software; you can redistribute it and/or 
  9  # modify it under the terms of the GNU General Public License 
 10  # as published by the Free Software Foundation, version 2 
 11  # of the License. 
 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 General Public License for more details. 
 17  """ 
 18  pytils.dt templatetags for Django web-framework 
 19  """ 
 20   
 21  __id__ = __revision__ = "$Id: pytils_dt.py 102 2007-07-12 12:33:36Z the.pythy $" 
 22  __url__ = "$URL: https://pythy.googlecode.com/svn/tags/pytils/0_2_2/pytils/templatetags/pytils_dt.py $" 
 23   
 24  import time 
 25  from django import template, conf 
 26  from pytils import dt, utils 
 27   
 28  register = template.Library()  #: Django template tag/filter registrator 
 29  encoding = conf.settings.DEFAULT_CHARSET  #: Current charset (sets in Django project's settings) 
 30  debug = conf.settings.DEBUG  #: Debug mode (sets in Django project's settings) 
 31  show_value = getattr(conf.settings, 'PYTILS_SHOW_VALUES_ON_ERROR', False)  #: Show values on errors (sets in Django project's settings) 
 32   
 33  # Если отладка, то показываем 'unknown+сообщение об ошибке'. 
 34  # Если отладка выключена, то можно чтобы при ошибках показывалось 
 35  # значение, переданное фильтру (PYTILS_SHOW_VALUES_ON_ERROR=True) 
 36  # либо пустая строка. 
 37   
 38  if debug: 
 39      default_value = "unknown: %(error)s" 
 40      default_uvalue = u"unknown: %(error)s" 
 41  elif show_value: 
 42      default_value = "%(value)s" 
 43      default_uvalue = u"%(value)s" 
 44  else: 
 45      default_value = "" 
 46      default_uvalue = u"" 
 47   
 48  # -- filters -- 
 49   
50 -def distance_of_time(from_time, accuracy=1):
51 """ 52 Display distance of time from current time. 53 54 Parameter is an accuracy level (deafult is 1). 55 Value must be numeral (i.e. time.time() result) or 56 datetime.datetime (i.e. datetime.datetime.now() 57 result). 58 59 Examples:: 60 {{ some_time|distance_of_time }} 61 {{ some_dtime|distance_of_time:2 }} 62 """ 63 try: 64 res = utils.provide_str( 65 dt.distance_of_time_in_words(from_time, accuracy), 66 encoding, 67 default=default_value) 68 except Exception, err: 69 # because filter must die silently 70 try: 71 default_distance = "%s seconds" % str(int(time.time() - from_time)) 72 except Exception: 73 default_distance = "" 74 res = default_value % {'error': err, 'value': default_distance} 75 return res
76
77 -def ru_strftime(date, format="%d.%m.%Y", inflected_day=False, preposition=False):
78 """ 79 Russian strftime, formats date with given format. 80 81 Value is a date (supports datetime.date and datetime.datetime), 82 parameter is a format (string). For explainings about format, 83 see documentation for original strftime: 84 http://docs.python.org/lib/module-time.html 85 86 Examples:: 87 {{ some_date|ru_strftime:"%d %B %Y, %A" }} 88 """ 89 try: 90 uformat = utils.provide_unicode(format, encoding, default=u"%d.%m.%Y") 91 ures = dt.ru_strftime(uformat, 92 date, 93 inflected=True, 94 inflected_day=inflected_day, 95 preposition=preposition) 96 res = utils.provide_str(ures, encoding) 97 except Exception, err: 98 # because filter must die silently 99 try: 100 default_date = date.strftime(format) 101 except Exception: 102 default_date = str(date) 103 res = default_value % {'error': err, 'value': default_date} 104 return res
105
106 -def ru_strftime_inflected(date, format="%d.%m.%Y"):
107 """ 108 Russian strftime with inflected day, formats date 109 with given format (similar to ru_strftime), 110 also inflects day in proper form. 111 112 Examples:: 113 {{ some_date|ru_strftime_inflected:"in %A (%d %B %Y)" 114 """ 115 return ru_strftime(date, format, inflected_day=True)
116
117 -def ru_strftime_preposition(date, format="%d.%m.%Y"):
118 """ 119 Russian strftime with inflected day and correct preposition, 120 formats date with given format (similar to ru_strftime), 121 also inflects day in proper form and inserts correct 122 preposition. 123 124 Examples:: 125 {{ some_date|ru_strftime_prepoisiton:"%A (%d %B %Y)" 126 """ 127 return ru_strftime(date, format, preposition=True)
128 129 130 # -- register filters 131 register.filter('distance_of_time', distance_of_time) 132 register.filter('ru_strftime', ru_strftime) 133 register.filter('ru_strftime_inflected', ru_strftime_inflected) 134 register.filter('ru_strftime_preposition', ru_strftime_preposition) 135