forked from bblanchon/django-htmx-messages-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.py
49 lines (38 loc) · 1.7 KB
/
middleware.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import json
from django.contrib.messages import get_messages
from django.http import HttpRequest, HttpResponse
from django.utils.deprecation import MiddlewareMixin
class HtmxMessageMiddleware(MiddlewareMixin):
"""
Middleware that moves messages into the HX-Trigger header when request is made with HTMX
"""
def process_response(self, request: HttpRequest, response: HttpResponse) -> HttpResponse:
# The HX-Request header indicates that the request was made with HTMX
if "HX-Request" not in request.headers:
return response
# Ignore redirections because HTMX cannot read the headers
if 300 <= response.status_code < 400:
return response
# Extract the messages
messages = [
{"message": message.message, "tags": message.tags}
for message in get_messages(request)
]
if not messages:
return response
# Get the existing HX-Trigger that could have been defined by the view
hx_trigger = response.headers.get("HX-Trigger")
if hx_trigger is None:
# If the HX-Trigger is not set, start with an empty object
hx_trigger = {}
elif hx_trigger.startswith("{"):
# If the HX-Trigger uses the object syntax, parse the object
hx_trigger = json.loads(hx_trigger)
else:
# If the HX-Trigger uses the string syntax, convert to the object syntax
hx_trigger = {hx_trigger: True}
# Add the messages array in the HX-Trigger object
hx_trigger["messages"] = messages
# Add or update the HX-Trigger
response.headers["HX-Trigger"] = json.dumps(hx_trigger)
return response