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, |
} |