diff options
author | Magnus Hagander | 2017-01-23 17:15:03 +0000 |
---|---|---|
committer | Magnus Hagander | 2017-01-23 17:17:41 +0000 |
commit | ecdca50fd95ef5bdfbbf8084b0afffe08f565265 (patch) | |
tree | 8c9ee99882fbfd5017451990bd6f346870dcdac4 /postgresqleu | |
parent | 3c0974e8e6b149ad93898774f57120faaa7dee0d (diff) |
Add datetimeformat and slugify filters
slugify already existed in the backend but was missing in deploystatic.
datetimeformat performs strftime() if the passed value is a date or time
object, and converts it to such an object to do strftime() on it if it
is not.
Diffstat (limited to 'postgresqleu')
-rw-r--r-- | postgresqleu/confreg/jinjafunc.py | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/postgresqleu/confreg/jinjafunc.py b/postgresqleu/confreg/jinjafunc.py index 9ad9fe3b..dcad285b 100644 --- a/postgresqleu/confreg/jinjafunc.py +++ b/postgresqleu/confreg/jinjafunc.py @@ -8,6 +8,8 @@ import json import os.path import random from itertools import groupby +from datetime import datetime, date, time +import dateutil.parser import jinja2 import jinja2.sandbox @@ -164,6 +166,14 @@ def filter_currency_format(v): def filter_float_str(f, n): return '{{0:.{0}f}}'.format(int(n)).format(f) +# Format a datetime. If it'sa datetime, call strftime. If it's a +# string, assume it's iso format and convert it to a date first. +def filter_datetimeformat(value, fmt): + if isinstance(value, date) or isinstance(value, datetime) or isinstance(value,time): + return value.strftime(fmt) + else: + return dateutil.parser.parse(value).strftime(fmt) + # Render a conference response based on jinja2 templates configured for the conference. # Returns the appropriate django HttpResponse object. @@ -178,6 +188,7 @@ def render_jinja_conference_response(request, conference, pagemagic, templatenam 'currency_format': filter_currency_format, 'escapejs': defaultfilters.escapejs_filter, 'floatstr': filter_float_str, + 'datetimeformat': filter_datetimeformat, 'groupby_sort': filter_groupby_sort, 'leadingnbsp': leadingnbsp, 'markdown': lambda t: jinja2.Markup(markdown.markdown(t)), |