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 | |
| 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.
| -rw-r--r-- | postgresqleu/confreg/jinjafunc.py | 11 | ||||
| -rwxr-xr-x | tools/deploystatic/deploystatic.py | 23 |
2 files changed, 34 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)), diff --git a/tools/deploystatic/deploystatic.py b/tools/deploystatic/deploystatic.py index 549dafb4..6433de64 100755 --- a/tools/deploystatic/deploystatic.py +++ b/tools/deploystatic/deploystatic.py @@ -11,6 +11,8 @@ import filecmp import shutil import json import random +import re +import unicodedata import io import subprocess import tarfile @@ -18,6 +20,9 @@ import tarfile import jinja2 import jinja2.sandbox +from datetime import datetime, date, time +import dateutil.parser + # # Some useful filters. We include them inline in this file to make it # standalone useful. @@ -37,9 +42,27 @@ def filter_shuffle(l): except: return l +# 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) + +# Slugify a text +def filter_slugify(value): + if not value: + return '' + value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii') + value = re.sub(r'[^\w\s-]', '', value).strip().lower() + return re.sub(r'[-\s]+', '-', value) + global_filters = { 'groupby_sort': filter_groupby_sort, 'shuffle': filter_shuffle, + 'slugify': filter_slugify, + 'datetimeformat': filter_datetimeformat, } |
