From df1cb88abc5aeb4af8158be5f62b22b6a1549265 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sat, 16 May 2020 19:00:43 +0200 Subject: [PATCH] admin: fix slow load for templates using get_links --- .../templates/administration/policy/list.html | 4 +-- .../admin/templatetags/admin_reflection.py | 31 ++++++++++--------- passbook/admin/views/overview.py | 2 +- passbook/policies/models.py | 2 +- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/passbook/admin/templates/administration/policy/list.html b/passbook/admin/templates/administration/policy/list.html index 051ad5140..1979763a8 100644 --- a/passbook/admin/templates/administration/policy/list.html +++ b/passbook/admin/templates/administration/policy/list.html @@ -47,12 +47,12 @@
{{ policy.name }}
- {% if not policy.policymodel_set.exists %} + {% if not policy.bindings.exists %} {% trans 'Warning: Policy is not assigned.' %} {% else %} - {% blocktrans with object_count=policy.policymodel_set.all|length %}Assigned to {{ object_count }} objects.{% endblocktrans %} + {% blocktrans with object_count=policy.bindings.all|length %}Assigned to {{ object_count }} objects.{% endblocktrans %} {% endif %}
diff --git a/passbook/admin/templatetags/admin_reflection.py b/passbook/admin/templatetags/admin_reflection.py index de5118441..9faeaa393 100644 --- a/passbook/admin/templatetags/admin_reflection.py +++ b/passbook/admin/templatetags/admin_reflection.py @@ -1,6 +1,4 @@ """passbook admin templatetags""" -import inspect - from django import template from django.db.models import Model from django.utils.html import mark_safe @@ -21,14 +19,16 @@ def get_links(model_instance): return links try: - for name, method in inspect.getmembers( - model_instance, predicate=inspect.ismethod - ): - if name.startswith(prefix): - human_name = name.replace(prefix, "").replace("_", " ").capitalize() - link = method() - if link: - links[human_name] = link + for name in dir(model_instance): + if not name.startswith(prefix): + continue + value = getattr(model_instance, name) + if not callable(value): + continue + human_name = name.replace(prefix, "").replace("_", " ").capitalize() + link = value() + if link: + links[human_name] = link except NotImplementedError: pass @@ -46,11 +46,14 @@ def get_htmls(context, model_instance): return htmls try: - for name, method in inspect.getmembers( - model_instance, predicate=inspect.ismethod - ): + for name in dir(model_instance): + if not name.startswith(prefix): + continue + value = getattr(model_instance, name) + if not callable(value): + continue if name.startswith(prefix): - html = method(context.get("request")) + html = value(context.get("request")) if html: htmls.append(mark_safe(html)) except NotImplementedError: diff --git a/passbook/admin/views/overview.py b/passbook/admin/views/overview.py index 0436c71f9..83d0ea009 100644 --- a/passbook/admin/views/overview.py +++ b/passbook/admin/views/overview.py @@ -39,7 +39,7 @@ class AdministrationOverviewView(AdminRequiredMixin, TemplateView): application=None ) kwargs["policies_without_binding"] = len( - Policy.objects.filter(policymodel__isnull=True) + Policy.objects.filter(bindings__isnull=True) ) kwargs["cached_policies"] = len(cache.keys("policy_*")) return super().get_context_data(**kwargs) diff --git a/passbook/policies/models.py b/passbook/policies/models.py index ec61e4119..d44e1d39f 100644 --- a/passbook/policies/models.py +++ b/passbook/policies/models.py @@ -12,7 +12,7 @@ class PolicyBindingModel(models.Model): """Base Model for objects that have policies applied to them.""" policies = models.ManyToManyField( - "Policy", through="PolicyBinding", related_name="+", blank=True + "Policy", through="PolicyBinding", related_name="bindings", blank=True ) class Meta: