From 79d882740f719b84a302a3bdc93fb10df569e4cc Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Wed, 9 Nov 2022 23:41:42 +0000 Subject: [PATCH] Add reswap() and retarget() http functions (#286) --- HISTORY.rst | 4 ++++ docs/http.rst | 20 ++++++++++++++++++-- src/django_htmx/http.py | 22 ++++++++++++++++++++++ tests/test_http.py | 28 ++++++++++++++++++++++++++-- 4 files changed, 70 insertions(+), 4 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 0afc1a8..aaceaa9 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -10,6 +10,10 @@ History Thanks to Chris Tapper in `PR #264 `__. +* Add ``django_htmx.http.reswap()`` for setting the ``HX-Reswap`` header added in `htmx 1.8.0 `__. + +* Add ``django_htmx.http.retarget()`` for setting the ``HX-Retarget`` header added in `htmx 1.6.1 `__. + 1.12.2 (2022-08-31) ------------------- diff --git a/docs/http.rst b/docs/http.rst index a81de76..691f177 100644 --- a/docs/http.rst +++ b/docs/http.rst @@ -82,14 +82,30 @@ HTTP .. autofunction:: push_url - Modify the |HX-Push-Url header|__ of ``response`` to push a URL into the browser location history, and return the response. - ``url`` should be the URL to push, or ``False`` to prevent the location history from being updated. + Set the |HX-Push-Url header|__ of ``response`` and return the response. + This header makes htmx push the given URL into the browser location history, + ``url`` should be the (relative) URL to push, or ``False`` to prevent the location history from being updated. .. |HX-Push-Url header| replace:: ``HX-Push-Url`` header __ https://htmx.org/headers/hx-push-url/ Calling ``push_url`` multiple times for the same ``response`` will replace the value of the header. +.. autofunction:: reswap + + Set the |HX-Reswap header|__ of ``response``, and return the response. + This header overrides the `swap method `__ that htmx will use. + + .. |HX-Reswap header| replace:: ``HX-Reswap`` header + __ https://htmx.org/reference/#response_headers + +.. autofunction:: retarget + + Set the |HX-Retarget header|__ of ``response`` to override element that htmx will swap the content into, and return the response. + + .. |HX-Retarget header| replace:: ``HX-Retarget`` header + __ https://htmx.org/reference/#response_headers + .. autofunction:: trigger_client_event Modify the |HX-Trigger headers|__ of ``response`` to trigger client-side events, and return the response. diff --git a/src/django_htmx/http.py b/src/django_htmx/http.py index 2910c06..7ddf108 100644 --- a/src/django_htmx/http.py +++ b/src/django_htmx/http.py @@ -56,6 +56,28 @@ def push_url(response: _HttpResponse, url: str | Literal[False]) -> _HttpRespons return response +def reswap( + response: _HttpResponse, + method: Literal[ + "innerHTML", + "outerHTML", + "beforebegin", + "afterbegin", + "beforeend", + "afterend", + "delete", + "none", + ], +) -> _HttpResponse: + response["HX-Reswap"] = method + return response + + +def retarget(response: _HttpResponse, target: str) -> _HttpResponse: + response["HX-Retarget"] = target + return response + + def trigger_client_event( response: _HttpResponse, name: str, diff --git a/tests/test_http.py b/tests/test_http.py index b7805df..22eadda 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -11,6 +11,8 @@ from django_htmx.http import HttpResponseClientRefresh from django_htmx.http import HttpResponseStopPolling from django_htmx.http import push_url +from django_htmx.http import reswap +from django_htmx.http import retarget from django_htmx.http import trigger_client_event @@ -52,18 +54,40 @@ class PushUrlTests(SimpleTestCase): def test_success(self): response = HttpResponse() - push_url(response, "/index.html") + response2 = push_url(response, "/index.html") + assert response2 is response assert response["HX-Push-Url"] == "/index.html" def test_success_false(self): response = HttpResponse() - push_url(response, False) + response2 = push_url(response, False) + assert response2 is response assert response["HX-Push-Url"] == "false" +class ReswapTests(SimpleTestCase): + def test_success(self): + response = HttpResponse() + + response2 = reswap(response, "outerHTML") + + assert response2 is response + assert response["HX-Reswap"] == "outerHTML" + + +class RetargetTests(SimpleTestCase): + def test_success(self): + response = HttpResponse() + + response2 = retarget(response, "#heading") + + assert response2 is response + assert response["HX-Retarget"] == "#heading" + + class TriggerClientEventTests(SimpleTestCase): def test_fail_bad_after_value(self): response = HttpResponse()