1
2
3
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()
15 encoding = conf.settings.DEFAULT_CHARSET
16 debug = conf.settings.DEBUG
17 show_value = getattr(conf.settings, 'PYTILS_SHOW_VALUES_ON_ERROR', False)
18
19
20
21
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
35
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
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):
79
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
98 res = default_value % {'error': err, 'value': str(amount)}
99 return res
100
101
102
103 register.filter('choose_plural', choose_plural)
104 register.filter('rubles', rubles)
105 register.filter('in_words', in_words)
106
107
108
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
132 res = default_value % {'error': err, 'value': str(amount)}
133 return res
134
135
136
137 register.simple_tag(sum_string)
138