temporary fix to merge ingredients without recipes

This commit is contained in:
vabene1111 2021-06-07 18:28:16 +02:00
parent b1db591e9f
commit ed313cbf9a

View File

@ -1,6 +1,7 @@
import os import os
from django.contrib import messages from django.contrib import messages
from django.db.models import Q
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render from django.shortcuts import get_object_or_404, render
from django.urls import reverse, reverse_lazy from django.urls import reverse, reverse_lazy
@ -45,7 +46,7 @@ def convert_recipe(request, pk):
@group_required('user') @group_required('user')
def internal_recipe_update(request, pk): def internal_recipe_update(request, pk):
if request.space.max_recipes != 0 and Recipe.objects.filter(space=request.space).count() > request.space.max_recipes: # TODO move to central helper function if request.space.max_recipes != 0 and Recipe.objects.filter(space=request.space).count() > request.space.max_recipes: # TODO move to central helper function
messages.add_message(request, messages.WARNING, _('You have reached the maximum number of recipes for your space.')) messages.add_message(request, messages.WARNING, _('You have reached the maximum number of recipes for your space.'))
return HttpResponseRedirect(reverse('view_recipe', args=[pk])) return HttpResponseRedirect(reverse('view_recipe', args=[pk]))
@ -287,14 +288,15 @@ def edit_ingredients(request):
new_unit = units_form.cleaned_data['new_unit'] new_unit = units_form.cleaned_data['new_unit']
old_unit = units_form.cleaned_data['old_unit'] old_unit = units_form.cleaned_data['old_unit']
if new_unit != old_unit: if new_unit != old_unit:
recipe_ingredients = Ingredient.objects.filter(unit=old_unit, step__recipe__space=request.space).all() with scopes_disabled():
for i in recipe_ingredients: recipe_ingredients = Ingredient.objects.filter(unit=old_unit).filter(Q(step__recipe__space=request.space) | Q(step__recipe__isnull=True)).all()
i.unit = new_unit for i in recipe_ingredients:
i.save() i.unit = new_unit
i.save()
old_unit.delete() old_unit.delete()
success = True success = True
messages.add_message(request, messages.SUCCESS, _('Units merged!')) messages.add_message(request, messages.SUCCESS, _('Units merged!'))
else: 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!'))
@ -303,7 +305,7 @@ def edit_ingredients(request):
new_food = food_form.cleaned_data['new_food'] new_food = food_form.cleaned_data['new_food']
old_food = food_form.cleaned_data['old_food'] old_food = food_form.cleaned_data['old_food']
if new_food != old_food: if new_food != old_food:
ingredients = Ingredient.objects.filter(food=old_food, step__recipe__space=request.space).all() ingredients = Ingredient.objects.filter(food=old_food).filter(Q(step__recipe__space=request.space) | Q(step__recipe__isnull=True)).all()
for i in ingredients: for i in ingredients:
i.food = new_food i.food = new_food
i.save() i.save()