diff --git a/cookbook/forms.py b/cookbook/forms.py index b8f6eab6..395209b7 100644 --- a/cookbook/forms.py +++ b/cookbook/forms.py @@ -143,20 +143,20 @@ class UnitMergeForm(forms.Form): ) -class IngredientMergeForm(forms.Form): - prefix = 'ingredient' +class FoodMergeForm(forms.Form): + prefix = 'food' - new_ingredient = forms.ModelChoiceField( + new_food = forms.ModelChoiceField( queryset=Food.objects.all(), widget=SelectWidget, - label=_('New Ingredient'), - help_text=_('New ingredient that other gets replaced by.'), + label=_('New Food'), + help_text=_('New food that other gets replaced by.'), ) - old_ingredient = forms.ModelChoiceField( + old_food = forms.ModelChoiceField( queryset=Food.objects.all(), widget=SelectWidget, - label=_('Old Ingredient'), - help_text=_('Ingredient that should be replaced.'), + label=_('Old Food'), + help_text=_('Food that should be replaced.'), ) diff --git a/cookbook/templates/forms/ingredients.html b/cookbook/templates/forms/ingredients.html index 9869829c..403a9b3f 100644 --- a/cookbook/templates/forms/ingredients.html +++ b/cookbook/templates/forms/ingredients.html @@ -35,7 +35,7 @@
diff --git a/cookbook/views/edit.py b/cookbook/views/edit.py index 1ee24a6d..17e3af2e 100644 --- a/cookbook/views/edit.py +++ b/cookbook/views/edit.py @@ -1,26 +1,18 @@ import os -import uuid -from io import BytesIO -import simplejson -import simplejson as json -from PIL import Image from django.contrib import messages -from django.contrib.auth.mixins import LoginRequiredMixin -from django.core.files import File from django.http import HttpResponseRedirect from django.shortcuts import get_object_or_404, render from django.urls import reverse from django.utils.translation import gettext as _ from django.views.generic import UpdateView -from cookbook.forms import ExternalRecipeForm, KeywordForm, StorageForm, SyncForm, InternalRecipeForm, CommentForm, \ - MealPlanForm, UnitMergeForm, IngredientMergeForm, RecipeBookForm, FoodForm -from cookbook.helper.permission_helper import group_required, GroupRequiredMixin - +from cookbook.forms import ExternalRecipeForm, KeywordForm, StorageForm, SyncForm, CommentForm, \ + MealPlanForm, UnitMergeForm, RecipeBookForm, FoodForm, FoodMergeForm from cookbook.helper.permission_helper import OwnerRequiredMixin +from cookbook.helper.permission_helper import group_required, GroupRequiredMixin from cookbook.models import Recipe, Sync, Keyword, RecipeImport, Storage, Comment, Ingredient, RecipeBook, \ - MealPlan, Unit, Food, MealType + MealPlan, Food, MealType from cookbook.provider.dropbox import Dropbox from cookbook.provider.nextcloud import Nextcloud @@ -256,24 +248,24 @@ def edit_ingredients(request): success = True messages.add_message(request, messages.SUCCESS, _('Units merged!')) - ingredients_form = IngredientMergeForm(request.POST, prefix=IngredientMergeForm.prefix) - if ingredients_form.is_valid(): - new_ingredient = ingredients_form.cleaned_data['new_food'] - old_ingredient = ingredients_form.cleaned_data['old_food'] - recipe_ingredients = Ingredient.objects.filter(food=old_ingredient).all() - for i in recipe_ingredients: - i.ingredient = new_ingredient + 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'] + ingredients = Ingredient.objects.filter(food=old_food).all() + for i in ingredients: + i.food = new_food i.save() - old_ingredient.delete() + old_food.delete() success = True - messages.add_message(request, messages.SUCCESS, _('Ingredients merged!')) + messages.add_message(request, messages.SUCCESS, _('Foods merged!')) if success: units_form = UnitMergeForm() - ingredients_form = IngredientMergeForm() + food_form = FoodMergeForm() else: units_form = UnitMergeForm() - ingredients_form = IngredientMergeForm() + food_form = FoodMergeForm() - return render(request, 'forms/ingredients.html', {'units_form': units_form, 'ingredients_form': ingredients_form}) + return render(request, 'forms/ingredients.html', {'units_form': units_form, 'food_form': food_form}) diff --git a/cookbook/views/import_export.py b/cookbook/views/import_export.py index c2eacb3c..ad7f1a86 100644 --- a/cookbook/views/import_export.py +++ b/cookbook/views/import_export.py @@ -31,10 +31,13 @@ def import_recipe(request): recipe = sr.save() if data['image']: - fmt, img = data['image'].split(';base64,') - ext = fmt.split('/')[-1] - recipe.image = ContentFile(base64.b64decode(img), name=f'{recipe.pk}.{ext}') # TODO possible security risk, maybe some checks needed - recipe.save() + try: + fmt, img = data['image'].split(';base64,') + ext = fmt.split('/')[-1] + recipe.image = ContentFile(base64.b64decode(img), name=f'{recipe.pk}.{ext}') # TODO possible security risk, maybe some checks needed + recipe.save() + except ValueError: + pass messages.add_message(request, messages.SUCCESS, _('Recipe imported successfully!')) return HttpResponseRedirect(reverse_lazy('view_recipe', args=[recipe.pk]))