[RFC PATCH 03/19] templatetags: Add 'site_admins' tag

Stephen Finucane stephen at that.guru
Thu Aug 12 07:36:49 AEST 2021


This lets us avoid complex templating.

Signed-off-by: Stephen Finucane <stephen at that.guru>
---
 .../templates/patchwork/optin-request.html    |  8 ++---
 .../templates/patchwork/optout-request.html   |  8 ++---
 patchwork/templatetags/admins.py              | 29 +++++++++++++++++++
 3 files changed, 35 insertions(+), 10 deletions(-)
 create mode 100644 patchwork/templatetags/admins.py

diff --git patchwork/templates/patchwork/optin-request.html patchwork/templates/patchwork/optin-request.html
index 3384c462..36744c26 100644
--- patchwork/templates/patchwork/optin-request.html
+++ patchwork/templates/patchwork/optin-request.html
@@ -1,5 +1,7 @@
 {% extends "base.html" %}
 
+{% load admins %}
+
 {% block title %}Opt-in{% endblock %}
 {% block heading %}Opt-in{% endblock %}
 
@@ -40,11 +42,7 @@
 
 {% if error and admins %}
 <p>
-  If you are having trouble opting in, please email
-{% for admin in admins %}
-{% if admins|length > 1 and forloop.last %} or {% endif %}
-{{ admin.0 }} <<a href="mailto:{{ admin.1 }}">{{ admin.1 }}</a>>{% if admins|length > 2 and not forloop.last %}, {% endif %}
-{% endfor %}
+  If you are having trouble opting in, please email {% site_admins %}.
 </p>
 {% endif %}
 {% endif %}
diff --git patchwork/templates/patchwork/optout-request.html patchwork/templates/patchwork/optout-request.html
index 7396fd36..a89f72bb 100644
--- patchwork/templates/patchwork/optout-request.html
+++ patchwork/templates/patchwork/optout-request.html
@@ -1,5 +1,7 @@
 {% extends "base.html" %}
 
+{% load admins %}
+
 {% block title %}Opt-out{% endblock %}
 {% block heading %}Opt-out{% endblock %}
 
@@ -42,11 +44,7 @@
 
 {% if error and admins %}
 <p>
-  If you are having trouble opting out, please email
-{% for admin in admins %}
-{% if admins|length > 1 and forloop.last %} or {% endif %}
-{{ admin.0 }} <<a href="mailto:{{ admin.1 }}">{{ admin.1 }}</a>>{% if admins|length > 2 and not forloop.last %}, {% endif %}
-{% endfor %}
+  If you are having trouble opting out, please email {% site_admins %}.
 </p>
 {% endif %}
 {% endif %}
diff --git patchwork/templatetags/admins.py patchwork/templatetags/admins.py
new file mode 100644
index 00000000..3809655c
--- /dev/null
+++ patchwork/templatetags/admins.py
@@ -0,0 +1,29 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2020 Stephen Finucane <stephen at that.guru>
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+from django.conf import settings
+from django import template
+from django.utils.safestring import mark_safe
+
+
+register = template.Library()
+
+
+ at register.simple_tag()
+def site_admins():
+    admins = [
+        f'{admin[0]} <<a href="mailto:{admin[1]}">{admin[1]}</a>>'
+        for admin in settings.ADMINS
+    ]
+
+    if not admins:
+        return ''
+
+    if len(admins) == 1:
+        return mark_safe(admins[0])
+
+    return mark_safe(
+        ', '.join(admins[:-2]) + admins[-2] + ' or ' + admins[-1]
+    )
-- 
2.31.1



More information about the Patchwork mailing list