From 0c1185f9f20dfa3e6b3a69465a5e318b1ddd6655 Mon Sep 17 00:00:00 2001 From: Birger Schacht Date: Mon, 2 Oct 2023 16:21:01 +0200 Subject: [PATCH] feat: introduce new templatetags This commit introduces a core templatetag `shared_url` to access the `SHARED_URL` setting (the templatetag defaults to `/static/`). It also updates the base template to use this templatetag. The commit also introduces an apis_entities templatetag `entities_list_links` that returns a list of links to the list views of all entities. It also updates the base template to use this templatetag. Closes: #293 --- .../templatetags/apis_templatetags.py | 17 +++++++++++++++++ apis_core/apis_metainfo/templates/base.html | 19 ++++++++++--------- apis_core/core/templatetags/apiscore.py | 10 ++++++++++ 3 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 apis_core/core/templatetags/apiscore.py diff --git a/apis_core/apis_entities/templatetags/apis_templatetags.py b/apis_core/apis_entities/templatetags/apis_templatetags.py index 9287a1621..a75709c18 100644 --- a/apis_core/apis_entities/templatetags/apis_templatetags.py +++ b/apis_core/apis_entities/templatetags/apis_templatetags.py @@ -1,4 +1,6 @@ +from operator import itemgetter from django import template +from apis_core.utils import caching register = template.Library() @@ -15,3 +17,18 @@ def url_replace(request, field, value): dict_[field] = value return dict_.urlencode() + + +@register.simple_tag +def entities_list_links(): + """ + 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 diff --git a/apis_core/apis_metainfo/templates/base.html b/apis_core/apis_metainfo/templates/base.html index 8684b3e72..1e07bf2d7 100644 --- a/apis_core/apis_metainfo/templates/base.html +++ b/apis_core/apis_metainfo/templates/base.html @@ -1,6 +1,8 @@ {% load static %} {% load apis_metainfo_extras %} +{% load apiscore %} +{% load apis_templatetags %} @@ -19,18 +21,18 @@ + href="{% shared_url %}favicon/apple-touch-icon.png" /> + href="{% shared_url %}favicon/favicon-32x32.png" /> - + href="{% shared_url %}favicon/favicon-16x16.png" /> + @@ -49,7 +51,7 @@ rel="stylesheet" /> + href="{% shared_url %}apis/libraries/scroll-to-top/css/ap-scroll-top.min.css" /> {% block scriptHeader %}{% endblock %} @@ -103,12 +105,11 @@ @@ -238,7 +239,7 @@ {% include "partials/bootstrap4_js.html" %} - + {% endblock %} {% block scripts2 %} diff --git a/apis_core/core/templatetags/apiscore.py b/apis_core/core/templatetags/apiscore.py new file mode 100644 index 000000000..54740d520 --- /dev/null +++ b/apis_core/core/templatetags/apiscore.py @@ -0,0 +1,10 @@ +from django import template +from django.conf import settings + + +register = template.Library() + + +@register.simple_tag +def shared_url(): + return getattr(settings, "SHARED_URL", "/static")