From a23bc69bd4570715bd0b2af855029fc4347f37eb Mon Sep 17 00:00:00 2001 From: ToljaFrajer Date: Mon, 26 May 2025 18:23:13 +0200 Subject: [PATCH 1/5] Add support to collect the django messages that were added in a view --- project/{{project_name}}/middleware.py | 38 ++++++++++++++++++-------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/project/{{project_name}}/middleware.py b/project/{{project_name}}/middleware.py index 958d452..37c70c5 100644 --- a/project/{{project_name}}/middleware.py +++ b/project/{{project_name}}/middleware.py @@ -1,25 +1,41 @@ from django.contrib.messages import get_messages -from inertia import share +from inertia.http import InertiaResponse class DataShareMiddleware(object): def __init__(self, get_response): self.get_response = get_response - - def __call__(self, request): + + @staticmethod + def collect_messages(request) -> list: messages = [] - for message in get_messages(request): + for msg in get_messages(request): message = { - "message": message.message, - "level": message.level, - "tags": message.tags, - "extra_tags": message.extra_tags, - "level_tag": message.level_tag, - } + "message": msg.message, + "level": msg.level, + "tags": msg.tags, + "extra_tags": msg.extra_tags, + "level_tag": msg.level_tag, + } messages.append(message) + return messages - share(request, messages=messages) + def __call__(self, request): response = self.get_response(request) + if hasattr(response, 'request') and hasattr(response.request, 'inertia'): + messages = self.collect_messages(request) + if messages: + request.inertia.props['messages'] = messages + # for now doesn't work with headers, cuz headers are not stored + # as field in InertiaResponse + # created MR for this https://github.com/inertiajs/inertia-django/pull/79 + response = InertiaResponse( + request, + response.component, + response.props, + response.template_data + # response.headers - if the MR will be merged + ) return response From 77f51d5c895cdbf1c7f54b23f8bfff8cea49f594 Mon Sep 17 00:00:00 2001 From: ToljaFrajer Date: Tue, 27 May 2025 00:14:59 +0200 Subject: [PATCH 2/5] Instead of adding the messages manually to the requests props, use share function --- project/{{project_name}}/middleware.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/project/{{project_name}}/middleware.py b/project/{{project_name}}/middleware.py index 37c70c5..4f23f73 100644 --- a/project/{{project_name}}/middleware.py +++ b/project/{{project_name}}/middleware.py @@ -1,6 +1,6 @@ from django.contrib.messages import get_messages from inertia.http import InertiaResponse - +from inertia.share import share class DataShareMiddleware(object): def __init__(self, get_response): @@ -26,7 +26,7 @@ def __call__(self, request): if hasattr(response, 'request') and hasattr(response.request, 'inertia'): messages = self.collect_messages(request) if messages: - request.inertia.props['messages'] = messages + share(request, messages=messages) # for now doesn't work with headers, cuz headers are not stored # as field in InertiaResponse # created MR for this https://github.com/inertiajs/inertia-django/pull/79 From 519b296f9fd643b844db9895213f46ae4d226424 Mon Sep 17 00:00:00 2001 From: ToljaFrajer Date: Tue, 27 May 2025 00:16:53 +0200 Subject: [PATCH 3/5] Simplify imports --- project/{{project_name}}/middleware.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/project/{{project_name}}/middleware.py b/project/{{project_name}}/middleware.py index 4f23f73..095165a 100644 --- a/project/{{project_name}}/middleware.py +++ b/project/{{project_name}}/middleware.py @@ -1,6 +1,7 @@ from django.contrib.messages import get_messages from inertia.http import InertiaResponse -from inertia.share import share +from inertia import share, InertiaResponse + class DataShareMiddleware(object): def __init__(self, get_response): From eef6ebebb542dfc983057f4c66b0cf1374c347fa Mon Sep 17 00:00:00 2001 From: ToljaFrajer Date: Tue, 27 May 2025 00:18:03 +0200 Subject: [PATCH 4/5] Delete redundant import --- project/{{project_name}}/middleware.py | 1 - 1 file changed, 1 deletion(-) diff --git a/project/{{project_name}}/middleware.py b/project/{{project_name}}/middleware.py index 095165a..aa61ca4 100644 --- a/project/{{project_name}}/middleware.py +++ b/project/{{project_name}}/middleware.py @@ -1,5 +1,4 @@ from django.contrib.messages import get_messages -from inertia.http import InertiaResponse from inertia import share, InertiaResponse From 41c4308aa6cfe9535fa8a271be46ac9f8f3d02da Mon Sep 17 00:00:00 2001 From: ToljaFrajer Date: Tue, 27 May 2025 12:12:56 +0200 Subject: [PATCH 5/5] Use headers from old response (you can acess the headers field from django's HttpResponseBase) --- project/{{project_name}}/middleware.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/project/{{project_name}}/middleware.py b/project/{{project_name}}/middleware.py index aa61ca4..e390222 100644 --- a/project/{{project_name}}/middleware.py +++ b/project/{{project_name}}/middleware.py @@ -27,15 +27,12 @@ def __call__(self, request): messages = self.collect_messages(request) if messages: share(request, messages=messages) - # for now doesn't work with headers, cuz headers are not stored - # as field in InertiaResponse - # created MR for this https://github.com/inertiajs/inertia-django/pull/79 response = InertiaResponse( request, response.component, response.props, - response.template_data - # response.headers - if the MR will be merged + response.template_data, + response.headers ) return response