admin: add support for extra links

This commit is contained in:
Jens Langhammer 2018-12-26 17:17:24 +01:00
parent d307d142dc
commit 7178468135
No known key found for this signature in database
GPG key ID: BEBC05297D92821B
5 changed files with 37 additions and 0 deletions

View file

@ -5,6 +5,7 @@
"editor.tabSize": 2
},
"cSpell.words": [
"SAML",
"pyazo"
]
}

View file

@ -2,6 +2,7 @@
{% load i18n %}
{% load utils %}
{% load admin_reflection %}
{% block title %}
{% title %}
@ -38,6 +39,10 @@
<td>
<a class="btn btn-default btn-sm" href="{% url 'passbook_admin:provider-update' pk=provider.pk %}?back={{ request.get_full_path }}">{% trans 'Edit' %}</a>
<a class="btn btn-default btn-sm" href="{% url 'passbook_admin:provider-delete' pk=provider.pk %}?back={{ request.get_full_path }}">{% trans 'Delete' %}</a>
{% get_links provider as links %}
{% for name, href in links.items %}
<a class="btn btn-default btn-sm" href="{{ href }}?back={{ request.get_full_path }}">{% trans name %}</a>
{% endfor %}
</td>
</tr>
{% endfor %}

View file

@ -2,6 +2,7 @@
{% load i18n %}
{% load utils %}
{% load admin_reflection %}
{% block content %}
<div class="container">
@ -34,6 +35,10 @@
<td>
<a class="btn btn-default btn-sm" href="{% url 'passbook_admin:source-update' pk=source.uuid %}?back={{ request.get_full_path }}">{% trans 'Edit' %}</a>
<a class="btn btn-default btn-sm" href="{% url 'passbook_admin:source-delete' pk=source.uuid %}?back={{ request.get_full_path }}">{% trans 'Delete' %}</a>
{% get_links source as links %}
{% for name, href in links %}
<a class="btn btn-default btn-sm" href="{{ href }}?back={{ request.get_full_path }}">{% trans name %}</a>
{% endfor %}
</td>
</tr>
{% endfor %}

View file

View file

@ -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