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"> <th role="columnheader">
<div> <div>
<div>{{ policy.name }}</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> <i class="pf-icon pf-icon-warning-triangle"></i>
<small>{% trans 'Warning: Policy is not assigned.' %}</small> <small>{% trans 'Warning: Policy is not assigned.' %}</small>
{% else %} {% else %}
<i class="pf-icon pf-icon-ok"></i> <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 %} {% endif %}
</div> </div>
</th> </th>

View File

@ -1,6 +1,4 @@
"""passbook admin templatetags""" """passbook admin templatetags"""
import inspect
from django import template from django import template
from django.db.models import Model from django.db.models import Model
from django.utils.html import mark_safe from django.utils.html import mark_safe
@ -21,12 +19,14 @@ def get_links(model_instance):
return links return links
try: try:
for name, method in inspect.getmembers( for name in dir(model_instance):
model_instance, predicate=inspect.ismethod if not name.startswith(prefix):
): continue
if name.startswith(prefix): value = getattr(model_instance, name)
if not callable(value):
continue
human_name = name.replace(prefix, "").replace("_", " ").capitalize() human_name = name.replace(prefix, "").replace("_", " ").capitalize()
link = method() link = value()
if link: if link:
links[human_name] = link links[human_name] = link
except NotImplementedError: except NotImplementedError:
@ -46,11 +46,14 @@ def get_htmls(context, model_instance):
return htmls return htmls
try: try:
for name, method in inspect.getmembers( for name in dir(model_instance):
model_instance, predicate=inspect.ismethod if not name.startswith(prefix):
): continue
value = getattr(model_instance, name)
if not callable(value):
continue
if name.startswith(prefix): if name.startswith(prefix):
html = method(context.get("request")) html = value(context.get("request"))
if html: if html:
htmls.append(mark_safe(html)) htmls.append(mark_safe(html))
except NotImplementedError: except NotImplementedError:

View File

@ -39,7 +39,7 @@ class AdministrationOverviewView(AdminRequiredMixin, TemplateView):
application=None application=None
) )
kwargs["policies_without_binding"] = len( kwargs["policies_without_binding"] = len(
Policy.objects.filter(policymodel__isnull=True) Policy.objects.filter(bindings__isnull=True)
) )
kwargs["cached_policies"] = len(cache.keys("policy_*")) kwargs["cached_policies"] = len(cache.keys("policy_*"))
return super().get_context_data(**kwargs) 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.""" """Base Model for objects that have policies applied to them."""
policies = models.ManyToManyField( policies = models.ManyToManyField(
"Policy", through="PolicyBinding", related_name="+", blank=True "Policy", through="PolicyBinding", related_name="bindings", blank=True
) )
class Meta: class Meta: