From 717846813520b11c80d330d917470890835c9d0e Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Wed, 26 Dec 2018 17:17:24 +0100 Subject: [PATCH] admin: add support for extra links --- .vscode/settings.json | 1 + .../administration/provider/list.html | 5 ++++ .../templates/administration/source/list.html | 5 ++++ passbook/admin/templatetags/__init__.py | 0 .../admin/templatetags/admin_reflection.py | 26 +++++++++++++++++++ 5 files changed, 37 insertions(+) create mode 100644 passbook/admin/templatetags/__init__.py create mode 100644 passbook/admin/templatetags/admin_reflection.py diff --git a/.vscode/settings.json b/.vscode/settings.json index 707790c63..caeacfe05 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,6 +5,7 @@ "editor.tabSize": 2 }, "cSpell.words": [ + "SAML", "pyazo" ] } \ No newline at end of file diff --git a/passbook/admin/templates/administration/provider/list.html b/passbook/admin/templates/administration/provider/list.html index 02b3d3293..209d47bb6 100644 --- a/passbook/admin/templates/administration/provider/list.html +++ b/passbook/admin/templates/administration/provider/list.html @@ -2,6 +2,7 @@ {% load i18n %} {% load utils %} +{% load admin_reflection %} {% block title %} {% title %} @@ -38,6 +39,10 @@ {% trans 'Edit' %} {% trans 'Delete' %} + {% get_links provider as links %} + {% for name, href in links.items %} + {% trans name %} + {% endfor %} {% endfor %} diff --git a/passbook/admin/templates/administration/source/list.html b/passbook/admin/templates/administration/source/list.html index 4814fe243..66128fcb2 100644 --- a/passbook/admin/templates/administration/source/list.html +++ b/passbook/admin/templates/administration/source/list.html @@ -2,6 +2,7 @@ {% load i18n %} {% load utils %} +{% load admin_reflection %} {% block content %}
@@ -34,6 +35,10 @@ {% trans 'Edit' %} {% trans 'Delete' %} + {% get_links source as links %} + {% for name, href in links %} + {% trans name %} + {% endfor %} {% endfor %} diff --git a/passbook/admin/templatetags/__init__.py b/passbook/admin/templatetags/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/passbook/admin/templatetags/admin_reflection.py b/passbook/admin/templatetags/admin_reflection.py new file mode 100644 index 000000000..fbb9a4d35 --- /dev/null +++ b/passbook/admin/templatetags/admin_reflection.py @@ -0,0 +1,26 @@ +"""passbook admin templatetags""" +import inspect +from logging import getLogger + +from django import template +from django.db.models import Model + +register = template.Library() +LOGGER = getLogger(__name__) + +@register.simple_tag() +def get_links(model_instance): + """Find all link_ methods on an object instance, run them and return as dict""" + prefix = 'link_' + links = {} + + if not isinstance(model_instance, Model): + LOGGER.warning("Model %s is not instance of Model", model_instance) + return links + + for name, method in inspect.getmembers(model_instance, predicate=inspect.ismethod): + if name.startswith(prefix): + human_name = name.replace(prefix, '').replace('_', ' ').capitalize() + links[human_name] = method() + + return links