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 "editor.tabSize": 2
}, },
"cSpell.words": [ "cSpell.words": [
"SAML",
"pyazo" "pyazo"
] ]
} }

View File

@ -2,6 +2,7 @@
{% load i18n %} {% load i18n %}
{% load utils %} {% load utils %}
{% load admin_reflection %}
{% block title %} {% block title %}
{% title %} {% title %}
@ -38,6 +39,10 @@
<td> <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-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> <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> </td>
</tr> </tr>
{% endfor %} {% endfor %}

View File

@ -2,6 +2,7 @@
{% load i18n %} {% load i18n %}
{% load utils %} {% load utils %}
{% load admin_reflection %}
{% block content %} {% block content %}
<div class="container"> <div class="container">
@ -34,6 +35,10 @@
<td> <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-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> <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> </td>
</tr> </tr>
{% endfor %} {% 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