updated edit and added space find methods

This commit is contained in:
vabene1111
2021-02-19 18:09:42 +01:00
parent fb58d35029
commit ad163509b4
5 changed files with 96 additions and 84 deletions

View File

@ -3,9 +3,10 @@ import os
from django.contrib import messages
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from django.urls import reverse, reverse_lazy
from django.utils.translation import gettext as _
from django.views.generic import UpdateView
from django_scopes import scopes_disabled
from cookbook.forms import (CommentForm, ExternalRecipeForm, FoodForm,
FoodMergeForm, KeywordForm, MealPlanForm,
@ -24,7 +25,7 @@ from cookbook.provider.nextcloud import Nextcloud
@group_required('guest')
def switch_recipe(request, pk):
recipe = get_object_or_404(Recipe, pk=pk)
recipe = get_object_or_404(Recipe, pk=pk, space=request.space)
if recipe.internal:
return HttpResponseRedirect(reverse('edit_internal_recipe', args=[pk]))
else:
@ -33,7 +34,7 @@ def switch_recipe(request, pk):
@group_required('user')
def convert_recipe(request, pk):
recipe = get_object_or_404(Recipe, pk=pk)
recipe = get_object_or_404(Recipe, pk=pk, space=request.space)
if not recipe.internal:
recipe.internal = True
recipe.save()
@ -43,7 +44,7 @@ def convert_recipe(request, pk):
@group_required('user')
def internal_recipe_update(request, pk):
recipe_instance = get_object_or_404(Recipe, pk=pk)
recipe_instance = get_object_or_404(Recipe, pk=pk, space=request.space)
return render(
request, 'forms/edit_internal_recipe.html', {'recipe': recipe_instance}
@ -62,7 +63,7 @@ class SyncUpdate(GroupRequiredMixin, UpdateView):
return reverse('edit_sync', kwargs={'pk': self.object.pk})
def get_context_data(self, **kwargs):
context = super(SyncUpdate, self).get_context_data(**kwargs)
context = super().get_context_data(**kwargs)
context['title'] = _("Sync")
return context
@ -79,7 +80,7 @@ class KeywordUpdate(GroupRequiredMixin, UpdateView):
return reverse('edit_keyword', kwargs={'pk': self.object.pk})
def get_context_data(self, **kwargs):
context = super(KeywordUpdate, self).get_context_data(**kwargs)
context = super().get_context_data(**kwargs)
context['title'] = _("Keyword")
return context
@ -103,12 +104,10 @@ class FoodUpdate(GroupRequiredMixin, UpdateView):
@group_required('admin')
def edit_storage(request, pk):
instance = get_object_or_404(Storage, pk=pk)
instance = get_object_or_404(Storage, pk=pk, space=request.space)
if not (instance.created_by == request.user or request.user.is_superuser):
messages.add_message(
request, messages.ERROR, _('You cannot edit this storage!')
)
messages.add_message(request, messages.ERROR, _('You cannot edit this storage!'))
return HttpResponseRedirect(reverse('list_storage'))
if request.method == "POST":
@ -225,7 +224,7 @@ class ExternalRecipeUpdate(GroupRequiredMixin, UpdateView):
def form_valid(self, form):
self.object = form.save(commit=False)
old_recipe = Recipe.objects.get(pk=self.object.pk)
old_recipe = Recipe.objects.get(pk=self.object.pk, space=self.request.space)
if not old_recipe.name == self.object.name:
if self.object.storage.method == Storage.DROPBOX:
# TODO central location to handle storage type switches
@ -277,45 +276,32 @@ def edit_ingredients(request):
new_unit = units_form.cleaned_data['new_unit']
old_unit = units_form.cleaned_data['old_unit']
if new_unit != old_unit:
recipe_ingredients = Ingredient.objects \
.filter(unit=old_unit).all()
recipe_ingredients = Ingredient.objects.filter(unit=old_unit, space=request.space).all()
for i in recipe_ingredients:
i.unit = new_unit
i.save()
old_unit.delete()
success = True
messages.add_message(
request, messages.SUCCESS, _('Units merged!')
)
messages.add_message(request, messages.SUCCESS, _('Units merged!'))
else:
messages.add_message(
request,
messages.ERROR,
_('Cannot merge with the same object!')
)
messages.add_message(request, messages.ERROR, _('Cannot merge with the same object!'))
food_form = FoodMergeForm(request.POST, prefix=FoodMergeForm.prefix)
if food_form.is_valid():
new_food = food_form.cleaned_data['new_food']
old_food = food_form.cleaned_data['old_food']
if new_food != old_food:
ingredients = Ingredient.objects.filter(food=old_food).all()
ingredients = Ingredient.objects.filter(food=old_food, space=request.space).all()
for i in ingredients:
i.food = new_food
i.save()
old_food.delete()
success = True
messages.add_message(
request, messages.SUCCESS, _('Foods merged!')
)
messages.add_message(request, messages.SUCCESS, _('Foods merged!'))
else:
messages.add_message(
request,
messages.ERROR,
_('Cannot merge with the same object!')
)
messages.add_message(request, messages.ERROR, _('Cannot merge with the same object!'))
if success:
units_form = UnitMergeForm()
@ -324,8 +310,4 @@ def edit_ingredients(request):
units_form = UnitMergeForm()
food_form = FoodMergeForm()
return render(
request,
'forms/ingredients.html',
{'units_form': units_form, 'food_form': food_form}
)
return render(request, 'forms/ingredients.html', {'units_form': units_form, 'food_form': food_form})