Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: sitescripts/templateFilters.py

Issue 9503001: Subscription stats: Make sure there can be no division by zero errors (Closed)
Patch Set: Fixed the issue in all the relevant places now Created March 1, 2013, 8:29 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sitescripts/logs/template/subscriptionsMain.html ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This file is part of the Adblock Plus web scripts, 3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2012 Eyeo GmbH 4 # Copyright (C) 2006-2012 Eyeo GmbH
5 # 5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify 6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as 7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation. 8 # published by the Free Software Foundation.
9 # 9 #
10 # Adblock Plus is distributed in the hope that it will be useful, 10 # Adblock Plus is distributed in the hope that it will be useful,
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 unit = 'GB' 163 unit = 'GB'
164 return '%.2f %s' % (value, unit) 164 return '%.2f %s' % (value, unit)
165 165
166 def getsum(iterable, attribute=None): 166 def getsum(iterable, attribute=None):
167 if attribute == None: 167 if attribute == None:
168 return sum(iterable) 168 return sum(iterable)
169 else: 169 else:
170 return sum(item[attribute] for item in iterable) 170 return sum(item[attribute] for item in iterable)
171 171
172 def getmax(iterable, attribute=None): 172 def getmax(iterable, attribute=None):
173 if attribute == None: 173 if len(iterable) == 0:
174 return 0
175 elif attribute == None:
174 return max(iterable) 176 return max(iterable)
175 else: 177 else:
176 return max(iterable, key=lambda item: item[attribute])[attribute] 178 return max(iterable, key=lambda item: item[attribute])[attribute]
177 179
180 def ensuremin(value, minvalue=1):
181 return max(value, minvalue)
182
178 def toJSON(value, **args): 183 def toJSON(value, **args):
179 return re.sub(r'</script>', r'<\/script>', json.dumps(value, **args)) 184 return re.sub(r'</script>', r'<\/script>', json.dumps(value, **args))
180 185
181 filters = { 186 filters = {
182 'formattime': formattime, 187 'formattime': formattime,
183 'timerelative': formatrelativetime, 188 'timerelative': formatrelativetime,
184 'url': formaturl, 189 'url': formaturl,
185 'keepnewlines': formatnewlines, 190 'keepnewlines': formatnewlines,
186 'filtercount': formatfiltercount, 191 'filtercount': formatfiltercount,
187 'buglinks': formatBugLinks, 192 'buglinks': formatBugLinks,
188 'urlencode': urlencode, 193 'urlencode': urlencode,
189 'subscriptionSort': subscriptionSort, 194 'subscriptionSort': subscriptionSort,
190 'mime': formatmime, 195 'mime': formatmime,
191 'ljust': ljust, 196 'ljust': ljust,
192 'rjust': rjust, 197 'rjust': rjust,
193 'ltruncate': ltruncate, 198 'ltruncate': ltruncate,
194 'weekday': formatweekday, 199 'weekday': formatweekday,
195 'monthname': formatmonthname, 200 'monthname': formatmonthname,
196 'bytes': formatbytes, 201 'bytes': formatbytes,
197 'sum': getsum, 202 'sum': getsum,
198 'max': getmax, 203 'max': getmax,
204 'ensuremin': ensuremin,
199 'json': toJSON, 205 'json': toJSON,
200 } 206 }
OLDNEW
« no previous file with comments | « sitescripts/logs/template/subscriptionsMain.html ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld