Skip to content

Commit

Permalink
refactor(apis_entities): this commit refactors the entity list menu
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
b1rger committed Apr 11, 2024
1 parent ae78620 commit 5038132
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
25 changes: 15 additions & 10 deletions apis_core/apis_entities/templatetags/apis_templatetags.py
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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)
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 5038132

Please sign in to comment.