From 16695d337ba812c74c100302fb426bae66c41055 Mon Sep 17 00:00:00 2001 From: Pelle Koster Date: Wed, 27 Nov 2024 13:12:44 +0100 Subject: [PATCH 1/2] return user to login page on unauthenticated htmx request --- changelog.d/+unauthenticated-htmx-full-redirect.changed.md | 1 + src/argus/htmx/middleware.py | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 changelog.d/+unauthenticated-htmx-full-redirect.changed.md diff --git a/changelog.d/+unauthenticated-htmx-full-redirect.changed.md b/changelog.d/+unauthenticated-htmx-full-redirect.changed.md new file mode 100644 index 000000000..64b29d676 --- /dev/null +++ b/changelog.d/+unauthenticated-htmx-full-redirect.changed.md @@ -0,0 +1 @@ +Return user to login page on unauthenticated HTMX request \ No newline at end of file diff --git a/src/argus/htmx/middleware.py b/src/argus/htmx/middleware.py index ad352fb10..b38f19758 100644 --- a/src/argus/htmx/middleware.py +++ b/src/argus/htmx/middleware.py @@ -4,6 +4,7 @@ from django.template import loader from django.utils.deprecation import MiddlewareMixin from django.utils.encoding import force_str +from django_htmx.http import HttpResponseClientRedirect class LoginRequiredMiddleware: @@ -43,7 +44,11 @@ def process_view(self, request, view_func, _view_args, _view_kwargs): return None # Redirect unauthenticated users to login page - return redirect_to_login(request.get_full_path(), self.login_url, "next") + response = redirect_to_login(request.get_full_path(), self.login_url, "next") + if request.htmx: + response = HttpResponseClientRedirect(response.url) + + return response class HtmxMessageMiddleware(MiddlewareMixin): From c41a41793014e8d99724049f2c0c72049dd91d79 Mon Sep 17 00:00:00 2001 From: Pelle Koster Date: Thu, 28 Nov 2024 14:17:28 +0100 Subject: [PATCH 2/2] do not require htmxmiddleware --- src/argus/htmx/middleware.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/argus/htmx/middleware.py b/src/argus/htmx/middleware.py index b38f19758..772cae27d 100644 --- a/src/argus/htmx/middleware.py +++ b/src/argus/htmx/middleware.py @@ -45,7 +45,7 @@ def process_view(self, request, view_func, _view_args, _view_kwargs): # Redirect unauthenticated users to login page response = redirect_to_login(request.get_full_path(), self.login_url, "next") - if request.htmx: + if getattr(request, "htmx", False): response = HttpResponseClientRedirect(response.url) return response