From 5038132987fccd9bf8d8109cb80d36fae26e9e4b Mon Sep 17 00:00:00 2001 From: Birger Schacht Date: Thu, 11 Apr 2024 12:30:32 +0200 Subject: [PATCH] refactor(apis_entities): this commit refactors the entity list menu The `entities_list_links` templatetag was replaced by a `entities_content_types` templatetag which simply returns a list of ContentType objects for existing entities (everything that inherits from AbstractEntity). The template then uses this list and the `model_meta` filter to create the menu items. --- .../templatetags/apis_templatetags.py | 25 +++++++++++-------- apis_core/core/templates/base.html | 6 ++--- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/apis_core/apis_entities/templatetags/apis_templatetags.py b/apis_core/apis_entities/templatetags/apis_templatetags.py index ad4d5c3d5..b30220fbd 100644 --- a/apis_core/apis_entities/templatetags/apis_templatetags.py +++ b/apis_core/apis_entities/templatetags/apis_templatetags.py @@ -1,7 +1,7 @@ -from operator import itemgetter from django import template -from apis_core.utils import caching from apis_core.utils.helpers import triple_sidebar +from django.contrib.contenttypes.models import ContentType +from apis_core.apis_entities.models import AbstractEntity register = template.Library() @@ -14,19 +14,24 @@ def url_replace(request, field, value): return dict_.urlencode() +def is_entity(content_type: ContentType): + model_class = content_type.model_class() + return model_class is not None and issubclass(model_class, AbstractEntity) + + @register.simple_tag -def entities_list_links(): +def entities_content_types(): """ Retrieve all models which inherit from AbstractEntity class and return their class name and verbose name. """ - entities_classes = caching.get_all_entity_classes() or [] - entities_links = [ - (e.__name__.lower(), e._meta.verbose_name.title()) for e in entities_classes - ] - entities_links.sort(key=itemgetter(1)) - - return entities_links + entities = list( + filter( + lambda content_type: is_entity(content_type), + ContentType.objects.all(), + ) + ) + return entities @register.simple_tag(takes_context=True) diff --git a/apis_core/core/templates/base.html b/apis_core/core/templates/base.html index 7ad69be8e..baed57021 100644 --- a/apis_core/core/templates/base.html +++ b/apis_core/core/templates/base.html @@ -101,10 +101,10 @@