From 5ba55356a90917226ea7a43ba54b62358df2f7ed Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 19 May 2020 20:29:02 +0200 Subject: [PATCH] admin: cleanup templates, cleanup templatetags --- .../admin/templates/administration/base.html | 6 +- .../administration/policybinding/list.html | 33 ++++----- .../list.html | 70 ++++++++++++------- passbook/admin/views/policies_bindings.py | 22 +----- passbook/admin/views/stages_bindings.py | 4 +- passbook/core/templates/base/page.html | 2 +- .../lib/templatetags/passbook_is_active.py | 37 +++++----- passbook/lib/templatetags/passbook_utils.py | 13 +++- 8 files changed, 91 insertions(+), 96 deletions(-) rename passbook/admin/templates/administration/{flowstagebinding => stage_binding}/list.html (52%) diff --git a/passbook/admin/templates/administration/base.html b/passbook/admin/templates/administration/base.html index 823f39c0b..e32edf477 100644 --- a/passbook/admin/templates/administration/base.html +++ b/passbook/admin/templates/administration/base.html @@ -24,7 +24,7 @@ -
  • +
  • {% trans 'Policies and Bindings' %} diff --git a/passbook/admin/templates/administration/policybinding/list.html b/passbook/admin/templates/administration/policybinding/list.html index 98e3a571a..173c56349 100644 --- a/passbook/admin/templates/administration/policybinding/list.html +++ b/passbook/admin/templates/administration/policybinding/list.html @@ -26,31 +26,24 @@ - - + + + - + {% for binding in object_list %} - - + + + - - {% for binding in object_list %} - - - - - - + {% regroup object_list by flow as grouped_bindings %} + {% for flow in grouped_bindings %} + + + + + + + {% for binding in flow.list %} + + + + + + + {% endfor %} {% endfor %}
    {% trans 'Name' %}{% trans 'Type' %}{% trans 'Enabled' %}{% trans 'Policy' %}{% trans 'Target' %}
    -
    -
    {{ binding.name }}
    - {% if not binding.bindings.exists %} - - {% trans 'Warning: Policy is not assigned.' %} - {% else %} - - {% blocktrans with object_count=binding.bindings.all|length %}Assigned to {{ object_count }} objects.{% endblocktrans %} - {% endif %} -
    +
    +
    {{ binding.enabled }}
    +
    +
    {{ binding.policy }}
    +
    +
    {{ binding.target|verbose_name }}
    - - {{ binding|verbose_name }} - - {% trans 'Edit' %} {% trans 'Delete' %} diff --git a/passbook/admin/templates/administration/flowstagebinding/list.html b/passbook/admin/templates/administration/stage_binding/list.html similarity index 52% rename from passbook/admin/templates/administration/flowstagebinding/list.html rename to passbook/admin/templates/administration/stage_binding/list.html index 68defbd75..7c1d94f79 100644 --- a/passbook/admin/templates/administration/flowstagebinding/list.html +++ b/passbook/admin/templates/administration/stage_binding/list.html @@ -33,33 +33,49 @@
    - - {{ binding.order }} - - -
    -
    {{ binding.stage.name }}
    - - {% blocktrans with flow=binding.flow %} - Bound to {{ flow }}. - {% endblocktrans %} - -
    -
    - - {{ binding.stage }} - - - {% trans 'Edit' %} - {% trans 'Delete' %} -
    + {% blocktrans with name=flow.grouper.name %} + Flow {{ name }} + {% endblocktrans %} +
    + + {{ binding.order }} + + +
    +
    {{ binding.flow.name }}
    + + {{ binding.flow }} + +
    +
    +
    +
    + {{ binding.stage.name }} +
    + + {{ binding.stage }} + +
    +
    + {% trans 'Edit' %} + {% trans 'Delete' %} +
    diff --git a/passbook/admin/views/policies_bindings.py b/passbook/admin/views/policies_bindings.py index 139f6513a..abb7bc951 100644 --- a/passbook/admin/views/policies_bindings.py +++ b/passbook/admin/views/policies_bindings.py @@ -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 diff --git a/passbook/admin/views/stages_bindings.py b/passbook/admin/views/stages_bindings.py index 1a12f8870..8aae9844c 100644 --- a/passbook/admin/views/stages_bindings.py +++ b/passbook/admin/views/stages_bindings.py @@ -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( diff --git a/passbook/core/templates/base/page.html b/passbook/core/templates/base/page.html index 4ffa46c20..a7bdcefd5 100644 --- a/passbook/core/templates/base/page.html +++ b/passbook/core/templates/base/page.html @@ -30,7 +30,7 @@
  • {% trans 'Access' %}
  • {% if user.is_superuser %} -
  • {% trans 'Administrate' %}
  • {% trans 'Monitor' %}
  • diff --git a/passbook/lib/templatetags/passbook_is_active.py b/passbook/lib/templatetags/passbook_is_active.py index 47acabc8d..f595d4350 100644 --- a/passbook/lib/templatetags/passbook_is_active.py +++ b/passbook/lib/templatetags/passbook_is_active.py @@ -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: diff --git a/passbook/lib/templatetags/passbook_utils.py b/passbook/lib/templatetags/passbook_utils.py index cebd24252..9826c54b6 100644 --- a/passbook/lib/templatetags/passbook_utils.py +++ b/passbook/lib/templatetags/passbook_utils.py @@ -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 ""