From 422091117bef29f29735147799f2a92323fc61e9 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 19 Dec 2024 12:10:11 +0700 Subject: [PATCH] did: disable cache, consider LOGIN_URL settings, allow submit-buttons to use ajax --- basxbread/layout/__init__.py | 4 +++- basxbread/layout/components/button.py | 14 +++++++++++--- basxbread/middleware.py | 4 +++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/basxbread/layout/__init__.py b/basxbread/layout/__init__.py index db9bbfec..aa2c64c7 100644 --- a/basxbread/layout/__init__.py +++ b/basxbread/layout/__init__.py @@ -67,7 +67,9 @@ def render(request, layout, context=None, **response_kwargs): return HttpResponse(hg.prerender(layout).render(defaultcontext), **response_kwargs) """ - return HttpResponse(layout.render(defaultcontext), **response_kwargs) + response = HttpResponse(layout.render(defaultcontext), **response_kwargs) + response["Cache-Control"] = "no-cache" + return response render.CONTEXT_PROCESSORS = None # type: ignore diff --git a/basxbread/layout/components/button.py b/basxbread/layout/components/button.py index 343e4c04..2e3039ca 100644 --- a/basxbread/layout/components/button.py +++ b/basxbread/layout/components/button.py @@ -73,7 +73,9 @@ def from_link(link, dialog=True, **kwargs): def as_href(self, href): return hg.A(*self, **{**self.attributes, "href": href}) - def as_submit(self, href, formfields={}, confirm_text=None, dialog=True, **kwargs): + def as_submit( + self, href, formfields={}, confirm_text=None, dialog=True, ajax=False, **kwargs + ): from django.forms import Form as DjangoForm from ..utils import slugify @@ -108,8 +110,14 @@ def as_submit(self, href, formfields={}, confirm_text=None, dialog=True, **kwarg for name, value in formfields.items() ], confirm_dialog if dialog else None, - action=href, - **hg.merge_html_attrs(kwargs, {"style": "display: inline"}), + **hg.merge_html_attrs( + kwargs, + ( + {"style": "display: inline", "hx-post": href} + if ajax + else {"style": "display: inline", "action": href} + ), + ), ) diff --git a/basxbread/middleware.py b/basxbread/middleware.py index 6068da71..28d2c2dd 100644 --- a/basxbread/middleware.py +++ b/basxbread/middleware.py @@ -69,4 +69,6 @@ def process_view(self, request, view_func, view_args, view_kwargs): request.user = user return None - return HttpResponseRedirect(reverse("login") + "?next=" + request.path) + return HttpResponseRedirect( + reverse(settings.LOGIN_URL) + "?next=" + request.path + )