From d11f4b9e46369a61ecc3ab797f61e8770dc9966a Mon Sep 17 00:00:00 2001 From: m5ka Date: Wed, 27 Mar 2024 14:07:12 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E2=9D=8C=20make=20recipe=20deletion=20m?= =?UTF-8?q?ulti-step?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- moku/forms/recipe.py | 6 +++++- moku/static/css/moku.css | 8 ++++++++ moku/templates/moku/delete.jinja | 14 ++++++++++++++ moku/views/recipe.py | 17 ++++++++++++++--- 4 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 moku/templates/moku/delete.jinja diff --git a/moku/forms/recipe.py b/moku/forms/recipe.py index 191f80e..5b292a5 100644 --- a/moku/forms/recipe.py +++ b/moku/forms/recipe.py @@ -1,9 +1,13 @@ -from django.forms import ModelForm +from django.forms import Form, ModelForm from django.utils.translation import gettext_lazy as _ from moku.models.recipe import Recipe, RecipeStep +class DeleteRecipeForm(Form): + pass + + class RecipeForm(ModelForm): """Form for creating and updating recipes.""" diff --git a/moku/static/css/moku.css b/moku/static/css/moku.css index 7c1b799..50d0583 100644 --- a/moku/static/css/moku.css +++ b/moku/static/css/moku.css @@ -46,6 +46,14 @@ body { margin-block-end: 1.6rem; } +.mx { + margin-inline: 1.2rem; +} + +.my { + margin-block: 1.6rem; +} + .small { font-size: 1.4rem; } diff --git a/moku/templates/moku/delete.jinja b/moku/templates/moku/delete.jinja new file mode 100644 index 0000000..476cf81 --- /dev/null +++ b/moku/templates/moku/delete.jinja @@ -0,0 +1,14 @@ +{% extends "moku/base.jinja" %} + +{% block content %} +
+

{% trans %}deleting {{ item }}{% endtrans %}

+

{% trans %}are you sure you want to delete {{ item }}?{% endtrans %}

+
+ {% csrf_token %} + 👈 no, take me back + + ❌ +
+
+{% endblock content %} \ No newline at end of file diff --git a/moku/views/recipe.py b/moku/views/recipe.py index b7cdcde..feb5dd1 100644 --- a/moku/views/recipe.py +++ b/moku/views/recipe.py @@ -1,23 +1,34 @@ from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin from django.shortcuts import get_object_or_404, redirect +from django.urls import reverse from django.utils.functional import cached_property from django.utils.translation import gettext as _ from django.utils.translation import gettext_lazy -from moku.forms.recipe import RecipeForm, RecipeStepForm +from moku.forms.recipe import DeleteRecipeForm, RecipeForm, RecipeStepForm from moku.models.recipe import Recipe, RecipeStep from moku.views.base import FormView, View -class DeleteRecipeView(LoginRequiredMixin, UserPassesTestMixin, View): +class DeleteRecipeView(LoginRequiredMixin, UserPassesTestMixin, FormView): """Deletes a recipe from the database if it belongs to the authenticated user.""" - def get(self, request, *args, **kwargs): + template_name = "moku/delete.jinja" + form_class = DeleteRecipeForm + + def form_valid(self, form): self.recipe.delete() messages.success(self.request, _("recipe deleted successfully!")) return redirect("recipe.index") + def get_context_data(self, **kwargs): + return { + **super().get_context_data(**kwargs), + "item": self.recipe.title, + "back_url": reverse("recipe.show", kwargs={"uuid": self.recipe.uuid}), + } + @cached_property def recipe(self): return get_object_or_404(Recipe, uuid=self.kwargs.get("uuid"))