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

Unified Diff: sitescripts/templateFilters.py

Issue 9503001: Subscription stats: Make sure there can be no division by zero errors (Closed)
Patch Set: Created March 1, 2013, 8:05 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sitescripts/logs/template/subscriptionsMain.html ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sitescripts/templateFilters.py
===================================================================
--- a/sitescripts/templateFilters.py
+++ b/sitescripts/templateFilters.py
@@ -165,21 +165,26 @@ def formatbytes(value):
def getsum(iterable, attribute=None):
if attribute == None:
return sum(iterable)
else:
return sum(item[attribute] for item in iterable)
def getmax(iterable, attribute=None):
- if attribute == None:
+ if len(iterable) == 0:
+ return 0
+ elif attribute == None:
return max(iterable)
else:
return max(iterable, key=lambda item: item[attribute])[attribute]
+def ensuremin(value, minvalue=1):
+ return max(value, minvalue)
+
def toJSON(value, **args):
return re.sub(r'</script>', r'<\/script>', json.dumps(value, **args))
filters = {
'formattime': formattime,
'timerelative': formatrelativetime,
'url': formaturl,
'keepnewlines': formatnewlines,
@@ -191,10 +196,11 @@ filters = {
'ljust': ljust,
'rjust': rjust,
'ltruncate': ltruncate,
'weekday': formatweekday,
'monthname': formatmonthname,
'bytes': formatbytes,
'sum': getsum,
'max': getmax,
+ 'ensuremin': ensuremin,
'json': toJSON,
}
« 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