Skip to content

Commit

Permalink
refactor(apis_entities): use verbose_name_plural in the entity list menu
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
b1rger committed Apr 15, 2024
1 parent 8b334c7 commit 54bf5ff
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
22 changes: 22 additions & 0 deletions apis_core/apis_entities/templatetags/apis_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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 ContentType.
"""
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"]
Expand Down
6 changes: 3 additions & 3 deletions apis_core/core/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@
<div class="dropdown-menu" aria-labelledby="navbarDropdown">

{% block entities-menu-items %}
{% entities_list_links as entities_links %}
{% for ent in entities_links %}
{% entities_content_types as entities %}
{% for content_type in entities %}
<a class="dropdown-item"
href="{% url 'apis:apis_entities:generic_entities_list' ent.0 %}">{{ ent.1 }}</a>
href="{% url 'apis:generic:list' content_type %}">{{ content_type|model_meta:"verbose_name_plural" }}</a>
{% endfor %}
{% endblock entities-menu-items %}

Expand Down

0 comments on commit 54bf5ff

Please sign in to comment.