fix: ❌ make recipe deletion multi-step
This commit is contained in:
parent
cecfb62e89
commit
d11f4b9e46
4 changed files with 41 additions and 4 deletions
|
|
@ -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."""
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
14
moku/templates/moku/delete.jinja
Normal file
14
moku/templates/moku/delete.jinja
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
{% extends "moku/base.jinja" %}
|
||||
|
||||
{% block content %}
|
||||
<div class="content block">
|
||||
<h2>{% trans %}deleting {{ item }}{% endtrans %}</h2>
|
||||
<p>{% trans %}are you sure you want to delete {{ item }}?{% endtrans %}</p>
|
||||
<form action="" method="POST" class="logout">
|
||||
{% csrf_token %}
|
||||
👈 <a href="{{ back_url }}">no, take me back</a>
|
||||
<span class="mx">•</span>
|
||||
❌ <button type="submit" class="logout">yes, delete it</button>
|
||||
</form>
|
||||
</div>
|
||||
{% endblock content %}
|
||||
|
|
@ -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"))
|
||||
|
|
|
|||
Loading…
Reference in a new issue