hotpocket/services/backend/hotpocket_backend/apps/htmx/messages.py
Tomek Wójcik b4338e2769
Some checks failed
CI / Checks (push) Failing after 13m2s
Release v1.0.0
2025-08-20 21:00:50 +02:00

41 lines
1.1 KiB
Python

# -*- coding: utf-8 -*-
from __future__ import annotations
from django.contrib.messages.constants import ( # noqa: F401
DEBUG,
DEFAULT_TAGS,
ERROR,
INFO,
SUCCESS,
WARNING,
)
from django.http import HttpRequest, HttpResponse
from django_htmx.http import trigger_client_event
def add_htmx_message(*,
request: HttpRequest,
response: HttpResponse,
level: str,
message: str,
extra_tags: str = '',
fail_silently: bool = False,
):
if not request.htmx:
if fail_silently is False:
raise RuntimeError(
"This doesn't look like an HTMX request: request=`%s`",
request,
)
else:
trigger_client_event(
response,
'HotPocket:UI:Messages:addMessage',
{
'level': DEFAULT_TAGS.get(level, DEFAULT_TAGS[INFO]),
'message': message,
'extra_tags': extra_tags,
},
after='swap',
)