admin: fix slow load for templates using get_links

This commit is contained in:
Jens Langhammer 2020-05-16 19:00:43 +02:00
parent 5596caedbc
commit df1cb88abc
4 changed files with 21 additions and 18 deletions

View File

@ -47,12 +47,12 @@
<th role="columnheader">
<div>
<div>{{ policy.name }}</div>
{% if not policy.policymodel_set.exists %}
{% if not policy.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=policy.policymodel_set.all|length %}Assigned to {{ object_count }} objects.{% endblocktrans %}</small>
<small>{% blocktrans with object_count=policy.bindings.all|length %}Assigned to {{ object_count }} objects.{% endblocktrans %}</small>
{% endif %}
</div>
</th>

View File

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

View File

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

View File

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