admin: cleanup templates, cleanup templatetags

This commit is contained in:
Jens Langhammer 2020-05-19 20:29:02 +02:00
parent 6104b7c9ba
commit 5ba55356a9
8 changed files with 91 additions and 96 deletions

View File

@ -24,7 +24,7 @@
<ul class="pf-c-nav__list">
<li class="pf-c-nav__item">
<a href="{% url 'passbook_admin:overview' %}"
class="pf-c-nav__link {% is_active_url 'passbook_admin:overview' %}">
class="pf-c-nav__link {% is_active 'passbook_admin:overview' %}">
{% trans 'System Status' %}
</a>
</li>
@ -52,7 +52,7 @@
{% trans 'Property Mappings' %}
</a>
</li>
<li class="pf-c-nav__item pf-m-expandable pf-m-expanded pf-m-current">
<li class="pf-c-nav__item pf-m-expandable pf-m-current">
<a href="#" class="pf-c-nav__link" aria-expanded="true">{% trans 'Flows' %}
<span class="pf-c-nav__toggle">
<i class="fas fa-angle-right" aria-hidden="true"></i>
@ -93,7 +93,7 @@
</ul>
</section>
</li>
<li class="pf-c-nav__item pf-m-expandable pf-m-expanded pf-m-current">
<li class="pf-c-nav__item pf-m-expandable pf-m-current">
<a href="#" class="pf-c-nav__link" aria-expanded="true">{% trans 'Policies and Bindings' %}
<span class="pf-c-nav__toggle">
<i class="fas fa-angle-right" aria-hidden="true"></i>

View File

@ -26,31 +26,24 @@
<table class="pf-c-table pf-m-compact pf-m-grid-xl" role="grid">
<thead>
<tr role="row">
<th role="columnheader" scope="col">{% trans 'Name' %}</th>
<th role="columnheader" scope="col">{% trans 'Type' %}</th>
<th role="columnheader" scope="col">{% trans 'Enabled' %}</th>
<th role="columnheader" scope="col">{% trans 'Policy' %}</th>
<th role="columnheader" scope="col">{% trans 'Target' %}</th>
<th role="cell"></th>
</tr>
</thead>
<tbody role="rowgroup">
<tbody role="rowgroup" class="pf-m-expanded">
{% for binding in object_list %}
<tr role="row">
<th role="columnheader">
<div>
<div>{{ binding.name }}</div>
{% if not binding.bindings.exists %}
<i class="pf-icon pf-icon-warning-triangle"></i>
<small>{% trans 'Warning: Policy is not assigned.' %}</small>
{% else %}
<i class="pf-icon pf-icon-ok"></i>
<small>{% blocktrans with object_count=binding.bindings.all|length %}Assigned to {{ object_count }} objects.{% endblocktrans %}</small>
{% endif %}
</div>
<tr role="row pf-c-table__expandable-row pf-m-expanded">
<th role="cell">
<div>{{ binding.enabled }}</div>
</th>
<th role="cell">
<div>{{ binding.policy }}</div>
</th>
<th role="cell">
<div>{{ binding.target|verbose_name }}</div>
</th>
<td role="cell">
<span>
{{ binding|verbose_name }}
</span>
</td>
<td>
<a class="pf-c-button pf-m-secondary" href="{% url 'passbook_admin:policy-binding-update' pk=binding.pk %}?back={{ request.get_full_path }}">{% trans 'Edit' %}</a>
<a class="pf-c-button pf-m-danger" href="{% url 'passbook_admin:policy-binding-delete' pk=binding.pk %}?back={{ request.get_full_path }}">{% trans 'Delete' %}</a>

View File

@ -33,33 +33,49 @@
</tr>
</thead>
<tbody role="rowgroup">
{% for binding in object_list %}
<tr role="row">
<td role="cell">
<span>
{{ binding.order }}
</span>
</td>
<th role="columnheader">
<div>
<div>{{ binding.stage.name }}</div>
<small>
{% blocktrans with flow=binding.flow %}
Bound to {{ flow }}.
{% endblocktrans %}
</small>
</div>
</th>
<td role="cell">
<span>
{{ binding.stage }}
</span>
</td>
<td>
<a class="pf-c-button pf-m-secondary" href="{% url 'passbook_admin:stage-binding-update' pk=binding.pk %}?back={{ request.get_full_path }}">{% trans 'Edit' %}</a>
<a class="pf-c-button pf-m-danger" href="{% url 'passbook_admin:stage-binding-delete' pk=binding.pk %}?back={{ request.get_full_path }}">{% trans 'Delete' %}</a>
</td>
</tr>
{% regroup object_list by flow as grouped_bindings %}
{% for flow in grouped_bindings %}
<tr role="role">
<td>
{% blocktrans with name=flow.grouper.name %}
Flow {{ name }}
{% endblocktrans %}
</td>
<td></td>
<td></td>
<td></td>
</tr>
{% for binding in flow.list %}
<tr class="pf-c-table__expandable-row pf-m-expanded" role="row">
<td role="cell">
<span>
{{ binding.order }}
</span>
</td>
<th role="columnheader">
<div>
<div>{{ binding.flow.name }}</div>
<small>
{{ binding.flow }}
</small>
</div>
</th>
<td role="cell">
<div>
<div>
{{ binding.stage.name }}
</div>
<small>
{{ binding.stage }}
</small>
</div>
</td>
<td>
<a class="pf-c-button pf-m-secondary" href="{% url 'passbook_admin:stage-binding-update' pk=binding.pk %}?back={{ request.get_full_path }}">{% trans 'Edit' %}</a>
<a class="pf-c-button pf-m-danger" href="{% url 'passbook_admin:stage-binding-delete' pk=binding.pk %}?back={{ request.get_full_path }}">{% trans 'Delete' %}</a>
</td>
</tr>
{% endfor %}
{% endfor %}
</tbody>
</table>

View File

@ -10,7 +10,6 @@ from django.utils.translation import ugettext as _
from django.views.generic import DeleteView, ListView, UpdateView
from guardian.mixins import PermissionListMixin, PermissionRequiredMixin
from passbook.lib.utils.reflection import path_to_class
from passbook.lib.views import CreateAssignPermView
from passbook.policies.forms import PolicyBindingForm
from passbook.policies.models import PolicyBinding
@ -22,7 +21,7 @@ class PolicyBindingListView(LoginRequiredMixin, PermissionListMixin, ListView):
model = PolicyBinding
permission_required = "passbook_policies.view_policybinding"
paginate_by = 10
ordering = "order"
ordering = ["order", "target"]
template_name = "administration/policybinding/list.html"
@ -42,13 +41,6 @@ class PolicyBindingCreateView(
success_url = reverse_lazy("passbook_admin:policies")
success_message = _("Successfully created PolicyBinding")
def get_context_data(self, **kwargs):
kwargs = super().get_context_data(**kwargs)
form_cls = self.get_form_class()
if hasattr(form_cls, "template_name"):
kwargs["base_template"] = form_cls.template_name
return kwargs
class PolicyBindingUpdateView(
SuccessMessageMixin, LoginRequiredMixin, PermissionRequiredMixin, UpdateView
@ -70,18 +62,6 @@ class PolicyBindingUpdateView(
kwargs["base_template"] = form_cls.template_name
return kwargs
def get_form_class(self):
form_class_path = self.get_object().form
form_class = path_to_class(form_class_path)
return form_class
def get_object(self, queryset=None):
return (
PolicyBinding.objects.filter(pk=self.kwargs.get("pk"))
.select_subclasses()
.first()
)
class PolicyBindingDeleteView(
SuccessMessageMixin, LoginRequiredMixin, PermissionRequiredMixin, DeleteView

View File

@ -21,8 +21,8 @@ class StageBindingListView(LoginRequiredMixin, PermissionListMixin, ListView):
model = FlowStageBinding
permission_required = "passbook_flows.view_flowstagebinding"
paginate_by = 10
ordering = "order"
template_name = "administration/flowstagebinding/list.html"
ordering = ["order", "flow"]
template_name = "administration/stage_binding/list.html"
class StageBindingCreateView(

View File

@ -30,7 +30,7 @@
<li class="pf-c-nav__item"><a class="pf-c-nav__link {% is_active_url 'passbook_core:overview' %}"
href="{% url 'passbook_core:overview' %}">{% trans 'Access' %}</a></li>
{% if user.is_superuser %}
<li class="pf-c-nav__item"><a class="pf-c-nav__link {% is_active_url 'passbook_admin:overview' %}"
<li class="pf-c-nav__item"><a class="pf-c-nav__link {% is_active_app 'passbook_admin' %}"
href="{% url 'passbook_admin:overview' %}">{% trans 'Administrate' %}</a></li>
<li class="pf-c-nav__item"><a class="pf-c-nav__link {% is_active_url 'passbook_admin:audit-log' %}"
href="{% url 'passbook_admin:audit-log' %}">{% trans 'Monitor' %}</a></li>

View File

@ -1,5 +1,6 @@
"""passbook lib navbar Templatetag"""
from django import template
from django.http import HttpRequest
from structlog import get_logger
register = template.Library()
@ -9,47 +10,43 @@ ACTIVE_STRING = "pf-m-current"
@register.simple_tag(takes_context=True)
def is_active(context, *args, **kwargs):
def is_active(context, *args: str, **_) -> str:
"""Return whether a navbar link is active or not."""
request = context.get("request")
app_name = kwargs.get("app_name", None)
request: HttpRequest = context.get("request")
if not request.resolver_match:
return ""
match = request.resolver_match
for url in args:
short_url = url.split(":")[1] if ":" in url else url
# Check if resolver_match matches
if request.resolver_match.url_name.startswith(
url
) or request.resolver_match.url_name.startswith(short_url):
# Monkeypatch app_name: urls from core have app_name == ''
# since the root urlpatterns have no namespace
if app_name and request.resolver_match.app_name == app_name:
if ":" in url:
app_name, url = url.split(":")
if match.app_name == app_name and match.url_name == url:
return ACTIVE_STRING
if app_name is None:
else:
if match.url_name == url:
return ACTIVE_STRING
return ""
@register.simple_tag(takes_context=True)
def is_active_url(context, view):
def is_active_url(context, view: str) -> str:
"""Return whether a navbar link is active or not."""
request = context.get("request")
current_full_url = (
f"{request.resolver_match.app_name}:{request.resolver_match.url_name}"
)
request: HttpRequest = context.get("request")
if not request.resolver_match:
return ""
match = request.resolver_match
current_full_url = f"{match.app_name}:{match.url_name}"
if current_full_url == view:
return ACTIVE_STRING
return ""
@register.simple_tag(takes_context=True)
def is_active_app(context, *args):
def is_active_app(context, *args: str) -> str:
"""Return True if current link is from app"""
request = context.get("request")
request: HttpRequest = context.get("request")
if not request.resolver_match:
return ""
for app_name in args:

View File

@ -6,11 +6,13 @@ from django import template
from django.db.models import Model
from django.template import Context
from django.utils.html import escape
from structlog import get_logger
from passbook.lib.config import CONFIG
from passbook.lib.utils.urls import is_url_absolute
register = template.Library()
LOGGER = get_logger()
@register.simple_tag(takes_context=True)
@ -78,7 +80,7 @@ def gravatar(email, size=None, rating=None):
@register.filter
def verbose_name(obj):
def verbose_name(obj) -> str:
"""Return Object's Verbose Name"""
if not obj:
return ""
@ -86,8 +88,15 @@ def verbose_name(obj):
@register.filter
def form_verbose_name(obj):
def form_verbose_name(obj) -> str:
"""Return ModelForm's Object's Verbose Name"""
if not obj:
return ""
return obj._meta.model._meta.verbose_name
@register.filter
def debug(obj) -> str:
"""Output object to logger"""
LOGGER.debug(obj)
return ""