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://gorod-omsk.ru/blog/pythy/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 70 2007-02-19 03:42:32Z the.pythy $" 
 22  __url__ = "$URL: https://pythy.googlecode.com/svn/trunk/pytils/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):
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 res = utils.provide_str(ures, encoding) 96 except Exception, err: 97 # because filter must die silently 98 try: 99 default_date = date.strftime(format) 100 except Exception: 101 default_date = str(date) 102 res = default_value % {'error': err, 'value': default_date} 103 return res
104
105 -def ru_strftime_inflected(date, format="%d.%m.%Y", inflected_day=True):
106 """ 107 Russian strftime with inflected day, formats date 108 with given format (similar to ru_strftime), 109 also inflects day in proper form. 110 111 Examples:: 112 {{ some_date|ru_strftime_inflected:"in %A (%d %B %Y)" 113 """ 114 return ru_strftime(date, format, inflected_day)
115 116 # -- register filters 117 register.filter('distance_of_time', distance_of_time) 118 register.filter('ru_strftime', ru_strftime) 119 register.filter('ru_strftime_inflected', ru_strftime_inflected) 120