Skip to content

Commit

Permalink
[#21] refactor context processor
Browse files Browse the repository at this point in the history
  • Loading branch information
SonnyBA committed Jun 21, 2024
1 parent 76250a1 commit 319d8c8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion open_api_framework/conf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"open_api_framework.context_processors.project",
"open_api_framework.context_processors.admin_settings",
f"{PROJECT_DIRNAME}.utils.context_processors.settings",
],
"loaders": TEMPLATE_LOADERS,
Expand Down
33 changes: 26 additions & 7 deletions open_api_framework/context_processors.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
from django.conf import settings
from django.http.request import HttpRequest


def project(request):
django_settings = (
"ENVIRONMENT_SHOWN_IN_ADMIN",
"RELEASE",
"GIT_SHA",
)
def admin_settings(request: HttpRequest) -> dict:
resolver = request.resolver_match
show_version = False

return {"project": {k: getattr(settings, k, None) for k in django_settings}}
if resolver:
if resolver.app_name:
url_name = f"{resolver.app_name}:{resolver.url_name}"
else:
url_name = resolver.url_name

# Note that only views that use the open-api-framework's
# `base_site.html` template are listed here
excluded_version_urls = [
"admin_password_reset",
"password_reset_done",
]

show_version = bool(url_name not in excluded_version_urls)

return {
"show_environment": getattr(settings, "ENVIRONMENT_SHOWN_IN_ADMIN", None),
"show_version": show_version,
"git_sha": getattr(settings, "GIT_SHA", None),
"version": getattr(settings, "RELEASE", None),
"environment": getattr(settings, "ENVIRONMENT", None),
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@
{% endblock %}

{% block messages %}
{% if project.ENVIRONMENT_SHOWN_IN_ADMIN %}
{% if show_environment %}
{% environment %}
{% endif %}

{{ block.super }}
{% endblock %}

{% block footer %}
{% url 'admin:login' as admin_login_url %}

<div id="footer">
{% if request.path != admin_login_url %}
{% if show_version %}
{% version %}
{% endif %}
</div>
Expand Down

0 comments on commit 319d8c8

Please sign in to comment.