setup for preview mode with cookie to be picked by reverse proxy

This commit is contained in:
Gabriel Augendre 2023-10-09 08:56:13 +02:00
parent defd369e96
commit 2b7fbc6c79
5 changed files with 47 additions and 1 deletions

View file

@ -23,6 +23,7 @@
<excludeFolder url="file://$MODULE_DIR$/.direnv" /> <excludeFolder url="file://$MODULE_DIR$/.direnv" />
<excludeFolder url="file://$MODULE_DIR$/.pytest_cache" /> <excludeFolder url="file://$MODULE_DIR$/.pytest_cache" />
<excludeFolder url="file://$MODULE_DIR$/.ruff_cache" /> <excludeFolder url="file://$MODULE_DIR$/.ruff_cache" />
<excludeFolder url="file://$MODULE_DIR$/.venv" />
</content> </content>
<orderEntry type="jdk" jdkName="Python 3.11 (charasheet)" jdkType="Python SDK" /> <orderEntry type="jdk" jdkName="Python 3.11 (charasheet)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />

View file

@ -1,5 +1,12 @@
from django.conf import settings from django.conf import settings
from django.core.handlers.wsgi import WSGIRequest
def app(_): def app(_):
return settings.APP return settings.APP
def preview(request: WSGIRequest):
return {
"preview_enabled": request.session.get("preview", False),
}

View file

@ -1,4 +1,6 @@
from django.conf import settings from django.conf import settings
from django.core.handlers.wsgi import WSGIRequest
from django.http import HttpResponse
def debug_toolbar_bypass_internal_ips(_) -> bool: def debug_toolbar_bypass_internal_ips(_) -> bool:
@ -9,3 +11,31 @@ def debug_toolbar_bypass_internal_ips(_) -> bool:
This is impossible to predict in a docker/k8s environment so we bypass this check. This is impossible to predict in a docker/k8s environment so we bypass this check.
""" """
return settings.DEBUG_TOOLBAR return settings.DEBUG_TOOLBAR
class PreviewMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request: WSGIRequest):
enable_preview = "enable_preview" in request.GET
disable_preview = "disable_preview" in request.GET
new_value = None
if enable_preview:
new_value = True
elif disable_preview:
new_value = False
if new_value is not None:
request.session["preview"] = new_value
response: HttpResponse = self.get_response(request)
if new_value is True:
response.set_cookie("preview", "enabled")
elif new_value is False:
response.delete_cookie("preview")
return response

View file

@ -97,6 +97,7 @@ MIDDLEWARE = [
"django.contrib.messages.middleware.MessageMiddleware", "django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware",
"django_htmx.middleware.HtmxMiddleware", "django_htmx.middleware.HtmxMiddleware",
"charasheet.middleware.PreviewMiddleware",
] ]
if DEBUG_TOOLBAR: if DEBUG_TOOLBAR:
MIDDLEWARE.insert(0, "debug_toolbar.middleware.DebugToolbarMiddleware") MIDDLEWARE.insert(0, "debug_toolbar.middleware.DebugToolbarMiddleware")
@ -117,6 +118,7 @@ TEMPLATES = [
"django.contrib.auth.context_processors.auth", "django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages", "django.contrib.messages.context_processors.messages",
"charasheet.context_processors.app", "charasheet.context_processors.app",
"charasheet.context_processors.preview",
], ],
}, },
}, },

View file

@ -1,7 +1,13 @@
<footer class="bg-light mt-auto"> <footer class="bg-light mt-auto">
<div class="container text-muted pt-2 pb-2 d-flex justify-content-between"> <div class="container text-muted pt-2 pb-2 d-flex justify-content-between">
<span> <span>
Version : {{ build.describe }} &bull; {{ build.date }} Version : {{ build.describe }}
&bull; {{ build.date }}
{% if preview_enabled %}
&bull; <a class="text-muted" href="?disable_preview">Disable preview</a>
{% else %}
&bull; <a class="text-muted" href="?enable_preview">Enable preview</a>
{% endif %}
</span> </span>
<div> <div>
<a href="https://github.com/Crocmagnon/charasheet" class="text-muted"><i class="fa-brands fa-github"></i></a> <a href="https://github.com/Crocmagnon/charasheet" class="text-muted"><i class="fa-brands fa-github"></i></a>