From f318dd62fe07cf06b43e843565ef5027af9815ad Mon Sep 17 00:00:00 2001 From: Tobias Mieves <tobias.mieves@tu-dortmund.de> Date: Tue, 19 Nov 2024 23:10:37 +0100 Subject: [PATCH] feat: Add delete confirm view Also refactor the delete view as class based view. The wish template now has a readonly mode --- docker-compose.yml | 1 + templates/wishlist/wish_confirm_delete.html | 17 +++++++++ templates/wishlist/wish_template.html | 38 +++++++++++---------- urls.py | 2 +- views.py | 9 ++++- 5 files changed, 47 insertions(+), 20 deletions(-) create mode 100644 templates/wishlist/wish_confirm_delete.html diff --git a/docker-compose.yml b/docker-compose.yml index 9fefe53..d7ea7d1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,3 +14,4 @@ services: - ./wishlist-db/:/app/db/ environment: - DEBUG=True + - SECRET_KEY=Test diff --git a/templates/wishlist/wish_confirm_delete.html b/templates/wishlist/wish_confirm_delete.html new file mode 100644 index 0000000..45efd4b --- /dev/null +++ b/templates/wishlist/wish_confirm_delete.html @@ -0,0 +1,17 @@ +{% extends 'wishlist/wishlist-base.html' %} + +{% block content %} + <form action="" method="post" class="m-auto w-3/4"> + {% csrf_token %} + <div class="card bg-base-100 shadow-xl"> + <div class="card-body"> + <h2 class="card-title text-2xl">Wunsch wirklich löschen?</h2> + {% include "wishlist/wish_template.html" with wish=wish readonly=True %} + <div class="card-actions justify-end mt-4"> + <input type="submit" class="btn btn-secondary" value="Cancel" name="cancel" /> + <input type="submit" class="btn btn-error" value="Confirm deletion" name="confirm_delete" /> + </div> + </div> + </div> + </form> +{% endblock %} diff --git a/templates/wishlist/wish_template.html b/templates/wishlist/wish_template.html index d06619d..7a8840f 100644 --- a/templates/wishlist/wish_template.html +++ b/templates/wishlist/wish_template.html @@ -47,30 +47,32 @@ rel="noopener noreferrer">Link</a> </div> {% endif %} - <div class="card-actions"> - {% if wish.owner == user %} - <a class="btn btn-sm" - href="{% url 'deleteWish' wish.id %}?list_owner={{ list_owner.id }}">Löschen</a> - <a class="btn btn-sm" - href="{% url 'editWish' wish.id %}?list_owner={{ list_owner.id }}">Bearbeiten</a> - {% endif %} - {% if wish.wish_for != user and wish.is_reservation_possible %} - <a class="btn btn-sm" - href="{% url 'reserveWish' wish.id %}?list_owner={{ list_owner.id }}">Reservieren</a>{% endif %} - {% if wish.wish_for != user and user in wish.reserved_by.all %} - <a class="btn btn-sm" - href="{% url 'cancelReserveWish' wish.id %}?list_owner={{ list_owner.id }}"> - Nicht mehr reservieren - </a> - {% endif %} - </div> + {% if not readonly %} + <div class="card-actions"> + {% if wish.owner == user %} + <a class="btn btn-sm" + href="{% url 'deleteWish' wish.id %}?list_owner={{ list_owner.id }}">Löschen</a> + <a class="btn btn-sm" + href="{% url 'editWish' wish.id %}?list_owner={{ list_owner.id }}">Bearbeiten</a> + {% endif %} + {% if wish.wish_for != user and wish.is_reservation_possible %} + <a class="btn btn-sm" + href="{% url 'reserveWish' wish.id %}?list_owner={{ list_owner.id }}">Reservieren</a>{% endif %} + {% if wish.wish_for != user and user in wish.reserved_by.all %} + <a class="btn btn-sm" + href="{% url 'cancelReserveWish' wish.id %}?list_owner={{ list_owner.id }}"> + Nicht mehr reservieren + </a> + {% endif %} + </div> + {% endif %} {% if wish.dependent_wishes.all %} <div class="collapse collapse-arrow {% if wish.reserved_by.all|length > 0 %}collapse-open{% endif %}"> <input type="checkbox" /> <div class="collapse-title text-xl font-medium">Abhängige Wünsche</div> <div class="collapse-content"> {% for dep_wish in wish.dependent_wishes.all %} - {% include "wishlist/wish_template.html" with wish=dep_wish %} + {% include "wishlist/wish_template.html" with wish=dep_wish readonly=readonly %} {% endfor %} </div> </div> diff --git a/urls.py b/urls.py index 045151b..77679f8 100644 --- a/urls.py +++ b/urls.py @@ -8,7 +8,7 @@ urlpatterns = [ path( "wish/add/<int:list_owner>", views.CreateWishView.as_view(), name="createWish" ), - path("wish/delete/<int:wish_id>", views.deleteWishView, name="deleteWish"), + path("wish/delete/<int:pk>", views.DeleteWishView.as_view(), name="deleteWish"), path( "wish/favorite/<int:wish_id>", views.toggleFavoriteView, name="toggleFavorite" ), diff --git a/views.py b/views.py index bb09b8d..9b32c3f 100644 --- a/views.py +++ b/views.py @@ -7,7 +7,7 @@ from django.forms import modelform_factory from django.http import HttpResponseRedirect, HttpRequest from django.shortcuts import render, get_object_or_404 from django.urls import reverse -from django.views.generic import CreateView, UpdateView, ListView +from django.views.generic import CreateView, UpdateView, ListView, DeleteView from wishlist.mixins import IsWishOwnerMixin from wishlist.models import Wish, Reservation, Group @@ -114,6 +114,13 @@ class CreateWishView(LoginRequiredMixin, CreateView): return context +class DeleteWishView(LoginRequiredMixin, IsWishOwnerMixin, DeleteView): + model = Wish + + def get_success_url(self): + return reverse("wishList", kwargs={"list_owner": self.request.GET["list_owner"]}) + + @login_required def deleteWishView(request, wish_id): wish = get_object_or_404(Wish, pk=wish_id) -- GitLab