2024-03-25 21:46:47 +00:00
|
|
|
from django.contrib import messages
|
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
|
|
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
|
|
|
|
from django.utils.functional import cached_property
|
2024-03-26 19:26:03 +00:00
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
|
from django.utils.translation import gettext_lazy
|
2024-03-25 21:46:47 +00:00
|
|
|
|
|
|
|
|
from moku.forms.recipe import RecipeForm, RecipeStepForm
|
|
|
|
|
from moku.models.recipe import Recipe, RecipeStep
|
2024-03-25 22:52:53 +00:00
|
|
|
from moku.views.base import FormView, View
|
2024-03-25 21:46:47 +00:00
|
|
|
|
|
|
|
|
|
2024-03-25 22:52:53 +00:00
|
|
|
class DeleteRecipeView(LoginRequiredMixin, UserPassesTestMixin, View):
|
2024-03-26 12:44:19 +00:00
|
|
|
"""Deletes a recipe from the database if it belongs to the authenticated user."""
|
|
|
|
|
|
2024-03-25 21:46:47 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
self.recipe.delete()
|
|
|
|
|
messages.success(self.request, _("recipe deleted successfully!"))
|
|
|
|
|
return redirect("recipe.index")
|
|
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
def recipe(self):
|
2024-03-26 12:18:47 +00:00
|
|
|
return get_object_or_404(Recipe, uuid=self.kwargs.get("uuid"))
|
2024-03-25 21:46:47 +00:00
|
|
|
|
|
|
|
|
def test_func(self):
|
|
|
|
|
return self.request.user.id == self.recipe.created_by.id
|
|
|
|
|
|
|
|
|
|
|
2024-03-25 22:52:53 +00:00
|
|
|
class DeleteStepView(LoginRequiredMixin, UserPassesTestMixin, View):
|
2024-03-26 12:44:19 +00:00
|
|
|
"""
|
|
|
|
|
Deletes a recipe step from the database if it belongs to the authenticated user.
|
|
|
|
|
"""
|
|
|
|
|
|
2024-03-25 21:46:47 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
if self.step.order != (self.step.recipe.steps.count() - 1):
|
|
|
|
|
messages.error(self.request, _("sorry! you can only delete the last step."))
|
|
|
|
|
return redirect(self.step.recipe.get_absolute_url())
|
|
|
|
|
self.step.delete()
|
|
|
|
|
messages.success(self.request, _("step deleted!"))
|
|
|
|
|
return redirect(self.step.recipe.get_absolute_url())
|
|
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
def step(self):
|
|
|
|
|
return get_object_or_404(
|
|
|
|
|
RecipeStep,
|
|
|
|
|
recipe__uuid=self.kwargs.get("uuid"),
|
|
|
|
|
uuid=self.kwargs.get("step"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_func(self):
|
|
|
|
|
return self.request.user.id == self.step.recipe.created_by.id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class EditStepView(LoginRequiredMixin, UserPassesTestMixin, FormView):
|
2024-03-26 12:44:19 +00:00
|
|
|
"""Allows users to edit steps of a recipe they created."""
|
|
|
|
|
|
2024-03-25 21:46:47 +00:00
|
|
|
template_name = "moku/recipe/edit_step.jinja"
|
|
|
|
|
form_class = RecipeStepForm
|
|
|
|
|
|
2024-03-26 16:51:55 +00:00
|
|
|
@property
|
|
|
|
|
def page_title(self):
|
|
|
|
|
return f"edit: step #{self.step.order + 1} of {self.step.recipe.title}"
|
|
|
|
|
|
2024-03-25 21:46:47 +00:00
|
|
|
def form_valid(self, form):
|
|
|
|
|
form.save()
|
|
|
|
|
messages.success(self.request, _("step updated!"))
|
|
|
|
|
return redirect(self.step.recipe.get_absolute_url())
|
|
|
|
|
|
|
|
|
|
def get_form(self):
|
|
|
|
|
return self.form_class(instance=self.step, **self.get_form_kwargs())
|
|
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
|
def step(self):
|
|
|
|
|
return get_object_or_404(
|
|
|
|
|
RecipeStep,
|
|
|
|
|
recipe__uuid=self.kwargs.get("uuid"),
|
|
|
|
|
uuid=self.kwargs.get("step"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_func(self):
|
|
|
|
|
return self.request.user.id == self.step.recipe.created_by.id
|
|
|
|
|
|
|
|
|
|
|
2024-03-25 22:52:53 +00:00
|
|
|
class IndexRecipeView(LoginRequiredMixin, View):
|
2024-03-26 12:44:19 +00:00
|
|
|
"""Shows a list of recipes created by the authenticated user."""
|
|
|
|
|
|
2024-03-25 21:46:47 +00:00
|
|
|
template_name = "moku/recipe/index.jinja"
|
2024-03-26 19:26:03 +00:00
|
|
|
page_title = gettext_lazy("my recipes")
|
2024-03-25 21:46:47 +00:00
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
|
return {
|
|
|
|
|
**super().get_context_data(**kwargs),
|
2024-03-26 12:18:47 +00:00
|
|
|
"recipes": Recipe.objects.filter(created_by=self.request.user).order_by(
|
|
|
|
|
"-created_by"
|
|
|
|
|
),
|
2024-03-25 21:46:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NewRecipeView(LoginRequiredMixin, FormView):
|
2024-03-26 12:44:19 +00:00
|
|
|
"""Allows users to create a new recipe."""
|
|
|
|
|
|
2024-03-25 21:46:47 +00:00
|
|
|
template_name = "moku/recipe/form.jinja"
|
|
|
|
|
form_class = RecipeForm
|
2024-03-26 19:26:03 +00:00
|
|
|
page_title = gettext_lazy("new recipe")
|
2024-03-25 21:46:47 +00:00
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
|
form.instance.created_by = self.request.user
|
|
|
|
|
form.save()
|
2024-03-26 12:18:47 +00:00
|
|
|
messages.success(
|
|
|
|
|
self.request,
|
|
|
|
|
_("recipe made successfully! now you can start adding the steps."),
|
|
|
|
|
)
|
2024-03-25 21:46:47 +00:00
|
|
|
return redirect(form.instance.get_absolute_url())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ShowRecipeView(FormView):
|
2024-03-26 12:44:19 +00:00
|
|
|
"""
|
|
|
|
|
Shows users details about a recipe, and allows steps to be created for it if they
|
|
|
|
|
are logged in as the recipe creator.
|
|
|
|
|
"""
|
|
|
|
|
|
2024-03-25 21:46:47 +00:00
|
|
|
template_name = "moku/recipe/show.jinja"
|
|
|
|
|
form_class = RecipeStepForm
|
|
|
|
|
|
2024-03-26 16:51:55 +00:00
|
|
|
@property
|
|
|
|
|
def page_title(self):
|
|
|
|
|
return self.recipe.title
|
|
|
|
|
|
2024-03-25 21:46:47 +00:00
|
|
|
@cached_property
|
|
|
|
|
def recipe(self):
|
|
|
|
|
return Recipe.objects.get(uuid=self.kwargs.get("uuid"))
|
|
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2024-03-26 12:18:47 +00:00
|
|
|
if (
|
|
|
|
|
not self.request.user.is_authenticated
|
|
|
|
|
or self.request.user.id != self.recipe.created_by.id
|
|
|
|
|
):
|
2024-03-25 21:46:47 +00:00
|
|
|
messages.error(self.request, _("that's not yours!"))
|
|
|
|
|
return redirect(form.instance.get_absolute_url())
|
|
|
|
|
order = self.recipe.steps.count()
|
|
|
|
|
if order >= 16:
|
2024-03-26 12:18:47 +00:00
|
|
|
messages.error(
|
|
|
|
|
self.request, _("sorry! you can't add any more steps to this recipe.")
|
|
|
|
|
)
|
2024-03-25 21:46:47 +00:00
|
|
|
return redirect(self.recipe.get_absolute_url())
|
|
|
|
|
form.instance.recipe = self.recipe
|
|
|
|
|
form.instance.order = order
|
|
|
|
|
form.save()
|
|
|
|
|
messages.success(self.request, _("step added successfully!"))
|
|
|
|
|
return redirect(self.recipe.get_absolute_url())
|
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2024-03-26 12:18:47 +00:00
|
|
|
return {**super().get_context_data(**kwargs), "recipe": self.recipe}
|