Ticket #22162: extra_filters.py

File extra_filters.py, 4.8 KB (added by alvaro@…, 11 years ago)

this .py contains new features for filter tags.

Line 
1import exceptions
2import math
3###################
4# EXTRA FILTERS #
5###################
6@register.filter
7@stringfilter
8def reverseString(value):
9 """
10 Reverse the string from the given string
11 """
12 safe = isinstance(value, SafeData)
13 value = value[::-1]
14 if safe :
15 return mark_safe(value)
16 return value
17
18@register.filter
19@stringfilter
20def trim(value):
21 """
22 Removes all blank values from the given string.
23 """
24 safe = isinstance(value, SafeData)
25 value = value.replace(' ', '')
26 if safe :
27 return mark_safe(value)
28 return value
29
30@register.filter
31@stringfilter
32def ParseInt(value,arg=None):
33 """
34 Parse a string to int and the value of arg is for error message.
35 """
36 safe = isinstance(value, SafeData)
37 try:
38 value = int(value)
39 except exceptions.ValueError:
40 if(arg is None or arg==""):
41 value = "Value-Error"
42 else:
43 value = arg
44
45 if safe and arg != ';':
46 return mark_safe(value)
47 return value
48
49@register.filter
50@stringfilter
51def endsWith(value,arg=None):
52 """
53 Returns a boolean if the string ends with a patner.
54 """
55 safe = isinstance(value, SafeData)
56 if(arg is None):
57 value = False
58 else:
59 value = value.__str__().endswith(arg)
60 if safe and arg != ';':
61 return mark_safe(value)
62 return value
63
64@register.filter
65@stringfilter
66def starstWith(value,arg=None):
67 """
68 Returns a boolean if the string strats with a patner.
69 """
70 safe = isinstance(value, SafeData)
71 if(arg is None):
72 value = False
73 else:
74 value = value.__str__().startswith(arg)
75 if safe and arg != ';':
76 return mark_safe(value)
77 return value
78
79@register.filter
80@stringfilter
81def toLowerCase(value):
82 """
83 Converts the string to Lower case.
84 """
85 safe = isinstance(value, SafeData)
86 try:
87 value = value.__str__().lower()
88 except:
89 value = "error"
90 if safe:
91 return mark_safe(value)
92 return value
93
94@register.filter
95@stringfilter
96def toUpperCase(value):
97 """
98 Converts the String to Upper Case.
99 """
100 safe = isinstance(value, SafeData)
101 try:
102 value = value.__str__().upper()
103 except:
104 value = "error"
105 if safe:
106 return mark_safe(value)
107 return value
108
109@register.filter
110@stringfilter
111def arrayMin(value):
112 """
113 Return the lowest value element of a array.
114 """
115 safe = isinstance(value, SafeData)
116 try:
117 value = min(value)
118 except:
119 value = "error"
120 if safe:
121 return mark_safe(value)
122 return value
123
124@register.filter
125@stringfilter
126def arrayMax(value):
127 """
128 Return the highest value element of a array.
129 """
130 safe = isinstance(value, SafeData)
131 try:
132 value = max(value)
133 except:
134 value = "error"
135 if safe:
136 return mark_safe(value)
137 return value
138
139@register.filter
140def exp(value):
141 """
142 Return the exponential value of the constant e and the given value,(e.g.: pow(e,value), e^value).
143 """
144 safe = isinstance(value, SafeData)
145 try:
146 value = math.exp(value)
147 except:
148 value = "error"
149 if safe and arg != ';':
150 return mark_safe(value)
151 return value
152
153@register.filter
154def pow(value,arg):
155 """
156 Return the exponential value of the value with the given arg.
157 """
158 safe = isinstance(value, SafeData)
159 try:
160 value = math.pow(value,arg)
161 except:
162 value = "error"
163 if safe and arg != ';':
164 return mark_safe(value)
165 return value
166
167
168@register.filter
169def factorial(value):
170 """
171 Return the factorial result of the following value.
172 """
173 safe = isinstance(value, SafeData)
174 try:
175 value = math.factorial(value)
176 except:
177 value = "error"
178 if safe and arg != ';':
179 return mark_safe(value)
180 return value
181
182@register.filter
183@stringfilter
184def toHiperLink(value,arg):
185 """
186 Transforms the link(value) to a hiper link form using the given arg as alias.
187 """
188 safe = isinstance(value, SafeData)
189 value = "<a href='%s'>%s</a>"%(value,arg.__str__())
190 if safe and arg != ';':
191 return mark_safe(value)
192 return value
193
194@register.filter
195def DictShowKeys(value):
196 """
197 Show all the keys of a dictionary, passed as value.
198 """
199
200 safe = isinstance(value, SafeData)
201 value = value.keys().__str__().replace('[', '').replace(']','')
202 if safe:
203 return mark_safe(value)
204 return value
205
206@register.filter
207def DictToString(value):
208 """
209 Return the __str__ form of a Dictionary, passed as value.
210 """
211 safe = isinstance(value, SafeData)
212 if type(value) is dict:
213 value = value.__str__()
214 else:
215 value = "error"
216 if safe:
217 return mark_safe(value)
218 return value
Back to Top