Skip to content

Commit

Permalink
feat(generic): introduce generic menu entry
Browse files Browse the repository at this point in the history
This commit introduces a template tag in the generic module that lists
all content types that inherit from the Generic Model but are not
Collections, Entities, Relations or History models.
This templatetag is then used to create a menu entry to manage those
types.

Closes: #1582
  • Loading branch information
b1rger committed Feb 7, 2025
1 parent 5186708 commit 7d12fd8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
24 changes: 24 additions & 0 deletions apis_core/generic/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends "base.html" %}
{% load generic %}

{% block main-menu %}
{{ block.super }}
<li class="nav-item dropdown">
<a href="#"
class="nav-link dropdown-toggle"
data-bs-toggle="dropdown"
role="button"
aria-haspopup="true"
aria-expanded="false">
Other
<span class="caret" />
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
{% pure_genericmodel_content_types as content_types %}
{% for content_type in content_types %}
<a class="dropdown-item"
href="{{ content_type.model_class.get_listview_url }}">{{ content_type.name }}</a>
{% endfor %}
</div>
</li>
{% endblock main-menu %}
29 changes: 29 additions & 0 deletions apis_core/generic/templatetags/generic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django import template
from django.apps import apps
from django.contrib.contenttypes.models import ContentType

from apis_core.core.templatetags.core import get_model_fields
Expand Down Expand Up @@ -61,6 +62,34 @@ def genericmodel_content_types():
return genericmodels


@register.simple_tag
def pure_genericmodel_content_types():
"""
Retrieve all models which inherit from GenericModel class
but are not Collections, Entities, Relations or History models
"""
genericmodels = set(genericmodel_content_types())
parents = []
if apps.is_installed("apis_core.collections"):
collections = apps.get_app_config("collections")
parents.append(collections.models_module.SkosCollection)
parents.append(collections.models_module.SkosCollectionContentObject)
if apps.is_installed("apis_core.relations"):
relations = apps.get_app_config("relations")
parents.append(relations.models_module.Relation)
if apps.is_installed("apis_core.history"):
history = apps.get_app_config("history")
parents.append(history.models_module.APISHistoryTableBase)
if apps.is_installed("apis_core.apis_entities"):
entities = apps.get_app_config("apis_entities")
parents.append(entities.models_module.AbstractEntity)
genericmodels = filter(
lambda content_type: not issubclass(content_type.model_class(), tuple(parents)),
genericmodels,
)
return genericmodels


@register.filter
def get_attribute(obj, attribute):
return getattr(obj, attribute, None)
Expand Down

0 comments on commit 7d12fd8

Please sign in to comment.