Compare commits

..

2 commits

2 changed files with 15 additions and 2 deletions

View file

@ -1,4 +1,4 @@
from django.shortcuts import render, get_object_or_404
from django.shortcuts import render, get_object_or_404, redirect
from .models import Recipe, Version, Ingredient
from .forms import RecipeForm, VersionForm, IngredientFormSet
from django.contrib.auth.decorators import login_required
@ -72,6 +72,16 @@ def add_recipe(request):
def edit_recipe(request, slug):
recipe = get_object_or_404(Recipe, slug=slug)
# It is assumed every recipe has at least one version
if not request.user.is_superuser:
users = set()
for version in recipe.versions.all(): # type: ignore
users.add(version.user)
if len(users) > 1 or (len(users) == 1 and next(iter(users)) != request.user):
return redirect(f"/accounts/login/?next={request.path}")
if request.method == 'POST':
form = RecipeForm(request.POST, instance=recipe, prefix=RECIPE_FORM_PREFIX)
if form.is_valid():
@ -113,6 +123,9 @@ def edit_version(request, slug_recipe, slug_version):
recipe = get_object_or_404(Recipe, slug=slug_recipe)
version = get_object_or_404(Version, recipe=recipe, slug=slug_version)
if version.user != request.user and not request.user.is_superuser:
return redirect(f"/accounts/login/?next={request.path}")
if request.method == 'POST':
version_form = VersionForm(request.POST, prefix=VERSION_FORM_PREFIX, instance=version, author_placeholder=get_name_of_user(request.user))
ingredients_formset = IngredientFormSet(request.POST, queryset=version.ingredients.all(), prefix=INGREDIENTS_FORMSET_PREFIX) # type: ignore

View file

@ -11,7 +11,7 @@
{% if next %}
<section>
{% if user.is_authenticated %}
<p>You are not authorized to access this site. Please inform Benjamin to get the corresponding authorization or log in with an account with the necessary permissions.</p>
<p>You are not authorized to access this site. Please log in with an account with the necessary permissions.</p>
{% else %}
<p>Please log in to view this site.</p>
{% endif %}