From c38105a76e6a8b8d4663456399aaa7ccfd865ada Mon Sep 17 00:00:00 2001 From: FreneticScribbler Date: Mon, 28 Sep 2020 10:31:43 +0100 Subject: [PATCH] Use template filter for consistent removal of 'None links' Plus cleaner template markup! More HTML-in-Python tho, which always feels a bit CSS-in-JS --- RIGS/templates/event_detail.html | 18 +++++++----------- RIGS/templatetags/filters.py | 8 ++++++++ templates/partials/generic_list.html | 5 +++-- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/RIGS/templates/event_detail.html b/RIGS/templates/event_detail.html index e3653224..4e38a532 100644 --- a/RIGS/templates/event_detail.html +++ b/RIGS/templates/event_detail.html @@ -1,4 +1,6 @@ {% extends request.is_ajax|yesno:"base_ajax.html,base_rigs.html" %} +{% load linkornone from filters %} + {% block title %}{% if object.is_rig %}N{{ object.pk|stringformat:"05d" }}{% else %}{{ object.pk }}{% endif %} | {{object.name}}{% endblock %} {% block content %} @@ -32,13 +34,9 @@ {% endif %}
Email
-
- - {{ object.person.email }} - -
+
{{ object.person.email|linkornone:'mailto' }}
Phone Number
-
{{ object.person.phone }}
+
{{ object.person.phone|linkornone:'tel' }}
@@ -55,12 +53,10 @@ {% endif %} +
Email
+
{{ object.organisation.email|linkornone:'mailto' }}
Phone Number
-
- - {{ object.organisation.phone }} - -
+
{{ object.organisation.phone|linkornone:'tel' }}
Has SU Account
{{ event.organisation.union_account|yesno|capfirst }}
diff --git a/RIGS/templatetags/filters.py b/RIGS/templatetags/filters.py index 23af7a0e..1fd8ee48 100644 --- a/RIGS/templatetags/filters.py +++ b/RIGS/templatetags/filters.py @@ -178,3 +178,11 @@ def namewithnotes(obj, url, autoescape=True): return mark_safe(obj.name + " ".format(reverse_lazy(url, kwargs={'pk': obj.pk}))) else: return obj.name + + +@register.filter(needs_autoescape=True) +def linkornone(attr, namespace, autoescape=True): + if attr is not None: + return mark_safe("{}".format(namespace, attr, str(attr))) + else: + return "None" diff --git a/templates/partials/generic_list.html b/templates/partials/generic_list.html index f46f37cf..fd4b916e 100644 --- a/templates/partials/generic_list.html +++ b/templates/partials/generic_list.html @@ -1,6 +1,7 @@ {% load url_replace from filters %} {% load orderby from filters %} {% load paginator from filters %} +{% load linkornone from filters %}
{% csrf_token %} @@ -34,8 +35,8 @@ {% for object in object_list %} {{ object.name }} - {{ object.email }} - {% if object.phone %}{%endif%}{{ object.phone }}{% if object.phone %}{%endif%} + {{ object.email|linkornone:'mailto' }} + {{ object.phone|linkornone:'tel' }} {{ object.notes|yesno|capfirst }} {% if union_account %} {{ object.union_account|yesno|capfirst }}