Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(generic): list all generic models on overview page #921

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions apis_core/generic/templates/generic/overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
<div class="card">
<div class="card-header">Overview</div>
<div class="card-body text-center">
{% contenttypes "apis_ontology" as contenttypes %}
{% for contenttype in contenttypes %}
<a href="{% url 'apis_core:generic:list' contenttype %}">
<button type="button" class="btn btn-outline-dark m-2">{{ contenttype }}</button>
</a>
{% endfor %}
<hr>
{% contenttypes "collections" as contenttypes %}
{% for contenttype in contenttypes %}
<a href="{% url 'apis_core:generic:list' contenttype %}">
<button type="button" class="btn btn-outline-dark m-2">{{ contenttype }}</button>
</a>
{% genericmodel_content_types as contenttypes %}
{% regroup contenttypes|dictsort:"app_label" by app_label as contenttypes_by_app %}
{% for app in contenttypes_by_app %}
<div class="mb-5">
<h3>{{ app.grouper }}</h3>
{% for contenttype in app.list %}
<a href="{% url 'apis_core:generic:list' contenttype %}">
<button type="button" class="btn btn-outline-dark m-2">{{ contenttype.name|capfirst }}</button>
</a>
{% endfor %}
</div>
{% endfor %}
</div>
</div>
Expand Down
21 changes: 21 additions & 0 deletions apis_core/generic/templatetags/apisgeneric.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django import template
from django.contrib.contenttypes.models import ContentType
from apis_core.generic.abc import GenericModel


register = template.Library()
Expand Down Expand Up @@ -39,3 +40,23 @@ def contenttypes(app_labels=None):
app_labels = app_labels.split(",")
return ContentType.objects.filter(app_label__in=app_labels)
return ContentType.objects.all()


def is_genericmodel(content_type: ContentType):
model_class = content_type.model_class()
return model_class is not None and issubclass(model_class, GenericModel)


@register.simple_tag
def genericmodel_content_types():
"""
Retrieve all models which inherit from GenericModel class
and return their ContentType.
"""
genericmodels = list(
filter(
lambda content_type: is_genericmodel(content_type),
ContentType.objects.all(),
)
)
return genericmodels
Loading