Package pytils :: Package templatetags :: Module pytils_numeral
[hide private]
[frames] | no frames]

Source Code for Module pytils.templatetags.pytils_numeral

  1  # -*- coding: utf-8 -*- 
  2  # License: GNU GPL2 
  3  # Author: Pythy <the.pythy@gmail.com> 
  4  """ 
  5  pytils.numeral templatetags for Django web-framework 
  6  """ 
  7   
  8  __id__ = __revision__ = "$Id: pytils_numeral.py 29 2006-10-21 08:28:27Z the.pythy $" 
  9  __url__ = "$URL: https://pythy.googlecode.com/svn/trunk/pytils/pytils/templatetags/pytils_numeral.py $" 
 10   
 11  from django import template, conf 
 12  from pytils import numeral, utils 
 13   
 14  register = template.Library()  #: Django template tag/filter registrator 
 15  encoding = conf.settings.DEFAULT_CHARSET  #: Current charset (sets in Django project's settings) 
 16  debug = conf.settings.DEBUG  #: Debug mode (sets in Django project's settings) 
 17  show_value = getattr(conf.settings, 'PYTILS_SHOW_VALUES_ON_ERROR', False)  #: Show values on errors (sets in Django project's settings) 
 18   
 19  # Если отладка, то показываем 'unknown+сообщение об ошибке'. 
 20  # Если отладка выключена, то можно чтобы при ошибках показывалось 
 21  # значение, переданное фильтру (PYTILS_SHOW_VALUES_ON_ERROR=True) 
 22  # либо пустая строка. 
 23   
 24  if debug: 
 25      default_value = "unknown: %(error)s" 
 26      default_uvalue = u"unknown: %(error)s" 
 27  elif show_value: 
 28      default_value = "%(value)s" 
 29      default_uvalue = u"%(value)s" 
 30  else: 
 31      default_value = "" 
 32      default_uvalue = u"" 
 33       
 34  # -- filters 
 35   
36 -def choose_plural(amount, variants):
37 """ 38 Choose proper form for plural. 39 40 Value is a amount, parameters are forms of noun. 41 Forms are variants for 1, 2, 5 nouns. It may be tuple 42 of elements, or string where variants separates each other 43 by comma. 44 45 Examples:: 46 {{ some_int|choose_plural:"пример,примера,примеров" }} 47 """ 48 try: 49 if isinstance(variants, str): 50 uvariants = utils.provide_unicode(variants, encoding, default_value) 51 else: 52 uvariants = [utils.provide_unicode(v, encoding, default_uvalue) for v in variants] 53 res = utils.provide_str( 54 numeral.choose_plural(amount, uvariants), 55 encoding, 56 default=default_value 57 ) 58 except Exception, err: 59 # because filter must die silently 60 try: 61 default_variant = variants[0] 62 except Exception: 63 default_variant = "" 64 res = default_value % {'error': err, 'value': default_variant} 65 return res
66
67 -def rubles(amount, zero_for_kopeck=False):
68 """Converts float value to in-words representation (for money)""" 69 try: 70 res = utils.provide_str( 71 numeral.rubles(amount, zero_for_kopeck), 72 encoding, 73 default=default_value 74 ) 75 except Exception, err: 76 # because filter must die silently 77 res = default_value % {'error': err, 'value': str(amount)} 78 return res
79
80 -def in_words(amount, gender=None):
81 """ 82 In-words representation of amount. 83 84 Parameter is a gender: 1=male, 2=female, 3=neuter 85 86 Examples:: 87 {{ some_int|in_words }} 88 {{ some_other_int|in_words:2 }} 89 """ 90 try: 91 res = utils.provide_str( 92 numeral.in_words(amount, gender), 93 encoding, 94 default=default_value 95 ) 96 except Exception, err: 97 # because filter must die silently 98 res = default_value % {'error': err, 'value': str(amount)} 99 return res
100 101 # -- register filters 102 103 register.filter('choose_plural', choose_plural) 104 register.filter('rubles', rubles) 105 register.filter('in_words', in_words) 106 107 # -- tags 108
109 -def sum_string(amount, gender, items):
110 """ 111 in_words and choose_plural in a one flask 112 Makes in-words representation of value with 113 choosing correct form of noun. 114 115 First parameter is an amount of objects. Second is a 116 gender (1=male, 2=female, 3=neuter). Third is a variants 117 of forms for object name. 118 119 Examples:: 120 {% sum_string some_int 1 "пример,примера,примеров" %} 121 {% sum_string some_other_int 2 "задача,задачи,задач" %} 122 """ 123 try: 124 uitems = [utils.provide_unicode(i, encoding, default_uvalue) for i in items] 125 res = utils.provide_str( 126 numeral.sum_string(amount, gender, uitems), 127 encoding, 128 default=default_value 129 ) 130 except Exception, err: 131 # because tag's renderer must die silently 132 res = default_value % {'error': err, 'value': str(amount)} 133 return res
134 135 # -- register tags 136 137 register.simple_tag(sum_string) 138