From 692d92412d368ad504c6c0d98e75d755192d2add Mon Sep 17 00:00:00 2001 From: Birger Schacht Date: Thu, 11 Apr 2024 12:30:32 +0200 Subject: [PATCH] refactor(apis_entities): use verbose_name_plural in the entity list menu A new `entities_content_types` templatetag is being introduces, which lists all content types that inherit from `AbstractEntity`. This is now being used in the base template to get a list of entities for the entities menu item. --- .../templatetags/apis_templatetags.py | 22 +++++++++++++++++++ apis_core/core/templates/base.html | 6 ++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/apis_core/apis_entities/templatetags/apis_templatetags.py b/apis_core/apis_entities/templatetags/apis_templatetags.py index ad4d5c3d5..8db8e5bb0 100644 --- a/apis_core/apis_entities/templatetags/apis_templatetags.py +++ b/apis_core/apis_entities/templatetags/apis_templatetags.py @@ -2,6 +2,8 @@ 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() @@ -29,6 +31,26 @@ def entities_list_links(): return entities_links +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_content_types(): + """ + Retrieve all models which inherit from AbstractEntity class + and return their class name and verbose name. + """ + entities = list( + filter( + lambda content_type: is_entity(content_type), + ContentType.objects.all(), + ) + ) + return entities + + @register.simple_tag(takes_context=True) def object_relations(context, detail=True): obj = context["object"] 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 @@