From ae6ab72536b4fcdc389ac18904b8bfc59e9bf9ea Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 14 Jan 2020 15:10:47 -0800 Subject: [PATCH] Add jinja_filters.make_attributes. Actually it's a function but not a filter. For creating html attributes out of a dict. --- frontends/etiquette_flask/backend/common.py | 3 ++- frontends/etiquette_flask/backend/jinja_filters.py | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/frontends/etiquette_flask/backend/common.py b/frontends/etiquette_flask/backend/common.py index cc0f8b8..688a116 100644 --- a/frontends/etiquette_flask/backend/common.py +++ b/frontends/etiquette_flask/backend/common.py @@ -36,8 +36,9 @@ site.jinja_env.lstrip_blocks = True site.jinja_env.filters['bytestring'] = jinja_filters.bytestring site.jinja_env.filters['file_link'] = jinja_filters.file_link site.jinja_env.filters['sort_tags'] = jinja_filters.sort_tags -site.jinja_env.filters['timestamp_to_naturaldate'] = jinja_filters.timestamp_to_naturaldate site.jinja_env.filters['timestamp_to_8601'] = jinja_filters.timestamp_to_8601 +site.jinja_env.filters['timestamp_to_naturaldate'] = jinja_filters.timestamp_to_naturaldate +site.jinja_env.globals['make_attributes'] = jinja_filters.make_attributes site.debug = True P = etiquette.photodb.PhotoDB() diff --git a/frontends/etiquette_flask/backend/jinja_filters.py b/frontends/etiquette_flask/backend/jinja_filters.py index 077075a..2a9451e 100644 --- a/frontends/etiquette_flask/backend/jinja_filters.py +++ b/frontends/etiquette_flask/backend/jinja_filters.py @@ -18,6 +18,13 @@ def file_link(photo, short=False): basename = jinja2.filters.do_urlencode(photo.basename) return f'/file/{photo.id}/{basename}' +def make_attributes(*booleans, **keyvalues): + keyvalues = {key: value for (key, value) in keyvalues.items() if value is not None} + attributes = [f'{key}="{jinja2.filters.escape(value)}"' for (key, value) in keyvalues.items()] + attributes.extend(booleans) + attributes = ' '.join(attributes) + return attributes + def sort_tags(tags): tags = sorted(tags, key=lambda x: x.name) return tags