From 4499e9ef10624748467fb60a4bfd0ba3372bde4d Mon Sep 17 00:00:00 2001 From: Hendrik Huyskens Date: Tue, 26 Nov 2024 11:57:15 +0100 Subject: [PATCH] Fix wrong swapping of info or reset responses --- building_dialouge_webapp/heat/flows.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/building_dialouge_webapp/heat/flows.py b/building_dialouge_webapp/heat/flows.py index 03152c2..74246ca 100644 --- a/building_dialouge_webapp/heat/flows.py +++ b/building_dialouge_webapp/heat/flows.py @@ -43,11 +43,8 @@ def __init__(self, html: str) -> None: super().__init__(html) -class HTMLResetStateResponse(StateResponse): - """State response holding HTML in order to reset page content.""" - - def __init__(self, html: str) -> None: - super().__init__(html) +class SwapHTMLStateResponse(HTMLStateResponse): + """State response holding HTML to be swapped into other target.""" class RedirectStateResponse(StateResponse): @@ -239,7 +236,7 @@ def response(self) -> dict[str, StateResponse]: def reset_response(self) -> dict[str, StateResponse]: """Return HTML including HTMX swap-oob in order to remove/reset HTML for current target.""" return { - self.name: HTMLResetStateResponse( + self.name: SwapHTMLStateResponse( f'
', ), } @@ -355,7 +352,7 @@ def __init__( # noqa: PLR0913 @property def response(self) -> dict[str, StateResponse]: form_response = { - "info": HTMLStateResponse( + "_info": SwapHTMLStateResponse( f'
{self.info_text}
', ), } @@ -365,7 +362,7 @@ def response(self) -> dict[str, StateResponse]: @property def reset_response(self) -> dict[str, StateResponse]: form_response = { - "info": HTMLStateResponse('
'), + "_info": SwapHTMLStateResponse('
'), } form_response.update(**super().reset_response) return form_response @@ -457,9 +454,15 @@ def dispatch(self, request, *args, **kwargs) -> HttpResponse: # Merge reset responses target = None html_response = "" - for name, state_response in state_partials.items(): + ordered_partials = dict( + sorted(state_partials.items(), key=lambda item: isinstance(item[1], SwapHTMLStateResponse)), + ) + for name, state_response in ordered_partials.items(): html_response += state_response.content - if isinstance(state_response, HTMLStateResponse): + if isinstance(state_response, HTMLStateResponse) and not isinstance( + state_response, + SwapHTMLStateResponse, + ): target = name response = HttpResponse(html_response, content_type="text/html") return retarget(response, f"#{target}") @@ -470,7 +473,7 @@ def dispatch(self, request, *args, **kwargs) -> HttpResponse: state_partials = { name: response for name, response in state_partials.items() - if not isinstance(response, (HTMLResetStateResponse, RedirectStateResponse)) + if not isinstance(response, (SwapHTMLStateResponse, RedirectStateResponse)) } context.update(state_partials) # Fill template with state partials by adding them with their target_id