Skip to content

Commit

Permalink
Keep django messages in queue on htmx redirects or refreshes
Browse files Browse the repository at this point in the history
  • Loading branch information
elfjes authored Dec 11, 2024
1 parent 9aa7f15 commit dc51a6c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.d/1071.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
argus.htmx: Keep django messages in queue on htmx redirects or refreshes
4 changes: 4 additions & 0 deletions src/argus/htmx/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ def process_response(self, request: HtmxHttpRequest, response: HttpResponse) ->
if 300 <= response.status_code < 400:
return response

# do not add messages to hx redirects/refreshes
if response.headers.get("HX-Refresh") == "true" or {"HX-Redirect", "HX-Location"} & response.headers.keys():
return response

if not response.writable():
return response

Expand Down
51 changes: 49 additions & 2 deletions tests/htmx/test_middleware.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
from unittest.mock import Mock

from django import test
from django.http import HttpResponseRedirect
from django.contrib import messages
from django.contrib.messages.middleware import MessageMiddleware
from django.contrib.sessions.middleware import SessionMiddleware
from django.http import HttpResponse, HttpResponseRedirect
from django.test.client import RequestFactory
from django_htmx.http import (
HttpResponseClientRedirect,
HttpResponseClientRefresh,
HttpResponseLocation,
)

from argus.htmx.middleware import LoginRequiredMiddleware
from argus.htmx.middleware import HtmxMessageMiddleware, LoginRequiredMiddleware


class TestLoginRequiredMiddleware(test.TestCase):
Expand Down Expand Up @@ -48,3 +56,42 @@ def view_func():
result = LoginRequiredMiddleware(lambda x: x).process_view(self.request, view_func, None, {})
self.assertIsNotNone(result)
self.assertIsInstance(result, HttpResponseRedirect)


class TestHtmxMessageMiddleware(test.TestCase):
def setUp(self):
request = RequestFactory().get("/foo")
request.htmx = True
request.user = Mock()
self.request = request

SessionMiddleware(lambda x: x).process_request(self.request)
MessageMiddleware(lambda x: x).process_request(self.request)
messages.info(self.request, "a message")

self.middleware = HtmxMessageMiddleware(lambda x: x)

def process_response(self, response: HttpResponse):
return self.middleware.process_response(self.request, response).content.decode()

def tearDown(self):
# expire current messages
messages.get_messages(self.request)

def test_adds_message_to_response(self):
self.assertIn("a message", self.process_response(HttpResponse()))

def test_doesnt_add_message_to_response_if_not_htmx(self):
self.request.htmx = False
self.assertNotIn("a message", self.process_response(HttpResponse()))

def test_doesnt_add_message_on_redirect_response(self):
responses = [
("redirect", HttpResponseRedirect("/")),
("hx-redirect", HttpResponseClientRedirect("/")),
("hx-location", HttpResponseLocation("/")),
("hx-refresh", HttpResponseClientRefresh()),
]
for name, response in responses:
with self.subTest(name):
self.assertNotIn("a message", self.process_response(response))

0 comments on commit dc51a6c

Please sign in to comment.