From b697c6ffe4aed5f869be8f797ab29036cc92127e Mon Sep 17 00:00:00 2001 From: Simon Oliver Tveit Date: Fri, 10 Jan 2025 10:05:22 +0100 Subject: [PATCH 1/2] Update only related list when deleting destination --- changelog.d/1128.fixed.md | 1 + src/argus/htmx/destination/views.py | 27 ++++++++++++++----- .../destination/_collapse_with_forms.html | 12 +++++++++ .../htmx/destination/_delete_form.html | 3 ++- .../htmx/destination/_form_list.html | 12 +-------- 5 files changed, 37 insertions(+), 18 deletions(-) create mode 100644 changelog.d/1128.fixed.md create mode 100644 src/argus/htmx/templates/htmx/destination/_collapse_with_forms.html diff --git a/changelog.d/1128.fixed.md b/changelog.d/1128.fixed.md new file mode 100644 index 000000000..5d3a01eeb --- /dev/null +++ b/changelog.d/1128.fixed.md @@ -0,0 +1 @@ +Only update the related media list when deleting a destination. diff --git a/src/argus/htmx/destination/views.py b/src/argus/htmx/destination/views.py index b66300f6a..9d4e500bc 100644 --- a/src/argus/htmx/destination/views.py +++ b/src/argus/htmx/destination/views.py @@ -29,16 +29,24 @@ def create_htmx(request) -> HttpResponse: @require_http_methods(["POST"]) def delete_htmx(request, pk: int) -> HttpResponse: destination = get_object_or_404(request.user.destinations.all(), pk=pk) - template = "htmx/destination/_form_list.html" + media = destination.media + error_msg = None try: medium = api_safely_get_medium_object(destination.media.slug) medium.raise_if_not_deletable(destination) except NotificationMedium.NotDeletableError: - error_msg = "This destination cannot be deleted." - return _render_destination_list(request, errors=[error_msg], template=template) + error_msg = "That destination cannot be deleted." else: destination.delete() - return _render_destination_list(request, template=template) + + forms = _get_update_forms(request.user, media=media) + + context = { + "error_msg": error_msg, + "forms": forms, + "media": media, + } + return render(request, "htmx/destination/_collapse_with_forms.html", context=context) @require_http_methods(["POST"]) @@ -91,9 +99,16 @@ def _render_destination_list( return render(request, template, context=context) -def _get_update_forms(user) -> list[DestinationFormUpdate]: +def _get_update_forms(user, media: Media = None) -> list[DestinationFormUpdate]: + """Get a list of update forms for the user's destinations. + :param media: if provided, only return destinations for this media. + """ + if media: + destinations = user.destinations.filter(media=media) + else: + destinations = user.destinations.all() # Sort by oldest first - destinations = user.destinations.all().order_by("pk") + destinations = destinations.order_by("pk") return [DestinationFormUpdate(instance=destination) for destination in destinations] diff --git a/src/argus/htmx/templates/htmx/destination/_collapse_with_forms.html b/src/argus/htmx/templates/htmx/destination/_collapse_with_forms.html new file mode 100644 index 000000000..03f7610e2 --- /dev/null +++ b/src/argus/htmx/templates/htmx/destination/_collapse_with_forms.html @@ -0,0 +1,12 @@ +
+ {{ media.name }} ({{ forms|length }}) +
+ {% if error_msg %}

{{ error_msg }}

{% endif %} + {% for form in forms %} +
+ {% include "htmx/destination/_edit_form.html" %} + {% include "htmx/destination/_delete_form.html" %} +
+ {% endfor %} +
+
diff --git a/src/argus/htmx/templates/htmx/destination/_delete_form.html b/src/argus/htmx/templates/htmx/destination/_delete_form.html index cea67b243..d5a0a6115 100644 --- a/src/argus/htmx/templates/htmx/destination/_delete_form.html +++ b/src/argus/htmx/templates/htmx/destination/_delete_form.html @@ -1,6 +1,7 @@
+ hx-target="closest details" + hx-swap="outerHTML"> {% csrf_token %}