small fixes
This commit is contained in:
parent
5b7ec37637
commit
c45472689e
@ -143,20 +143,20 @@ class UnitMergeForm(forms.Form):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class IngredientMergeForm(forms.Form):
|
class FoodMergeForm(forms.Form):
|
||||||
prefix = 'ingredient'
|
prefix = 'food'
|
||||||
|
|
||||||
new_ingredient = forms.ModelChoiceField(
|
new_food = forms.ModelChoiceField(
|
||||||
queryset=Food.objects.all(),
|
queryset=Food.objects.all(),
|
||||||
widget=SelectWidget,
|
widget=SelectWidget,
|
||||||
label=_('New Ingredient'),
|
label=_('New Food'),
|
||||||
help_text=_('New ingredient that other gets replaced by.'),
|
help_text=_('New food that other gets replaced by.'),
|
||||||
)
|
)
|
||||||
old_ingredient = forms.ModelChoiceField(
|
old_food = forms.ModelChoiceField(
|
||||||
queryset=Food.objects.all(),
|
queryset=Food.objects.all(),
|
||||||
widget=SelectWidget,
|
widget=SelectWidget,
|
||||||
label=_('Old Ingredient'),
|
label=_('Old Food'),
|
||||||
help_text=_('Ingredient that should be replaced.'),
|
help_text=_('Food that should be replaced.'),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@
|
|||||||
<form action="{% url 'edit_food' %}" method="post"
|
<form action="{% url 'edit_food' %}" method="post"
|
||||||
onsubmit="return confirm('{% trans 'Are you sure that you want to merge these two ingredients ?' %}')">
|
onsubmit="return confirm('{% trans 'Are you sure that you want to merge these two ingredients ?' %}')">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
{{ ingredients_form|crispy }}
|
{{ food_form|crispy }}
|
||||||
<button class="btn btn-danger" type="submit">
|
<button class="btn btn-danger" type="submit">
|
||||||
<i class="fas fa-sync-alt"></i> {% trans 'Merge' %}</button>
|
<i class="fas fa-sync-alt"></i> {% trans 'Merge' %}</button>
|
||||||
</form>
|
</form>
|
||||||
|
@ -1,26 +1,18 @@
|
|||||||
import os
|
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 import messages
|
||||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
||||||
from django.core.files import File
|
|
||||||
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
|
from django.urls import reverse
|
||||||
from django.utils.translation import gettext as _
|
from django.utils.translation import gettext as _
|
||||||
from django.views.generic import UpdateView
|
from django.views.generic import UpdateView
|
||||||
|
|
||||||
from cookbook.forms import ExternalRecipeForm, KeywordForm, StorageForm, SyncForm, InternalRecipeForm, CommentForm, \
|
from cookbook.forms import ExternalRecipeForm, KeywordForm, StorageForm, SyncForm, CommentForm, \
|
||||||
MealPlanForm, UnitMergeForm, IngredientMergeForm, RecipeBookForm, FoodForm
|
MealPlanForm, UnitMergeForm, RecipeBookForm, FoodForm, FoodMergeForm
|
||||||
from cookbook.helper.permission_helper import group_required, GroupRequiredMixin
|
|
||||||
|
|
||||||
from cookbook.helper.permission_helper import OwnerRequiredMixin
|
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, \
|
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.dropbox import Dropbox
|
||||||
from cookbook.provider.nextcloud import Nextcloud
|
from cookbook.provider.nextcloud import Nextcloud
|
||||||
|
|
||||||
@ -256,24 +248,24 @@ def edit_ingredients(request):
|
|||||||
success = True
|
success = True
|
||||||
messages.add_message(request, messages.SUCCESS, _('Units merged!'))
|
messages.add_message(request, messages.SUCCESS, _('Units merged!'))
|
||||||
|
|
||||||
ingredients_form = IngredientMergeForm(request.POST, prefix=IngredientMergeForm.prefix)
|
food_form = FoodMergeForm(request.POST, prefix=FoodMergeForm.prefix)
|
||||||
if ingredients_form.is_valid():
|
if food_form.is_valid():
|
||||||
new_ingredient = ingredients_form.cleaned_data['new_food']
|
new_food = food_form.cleaned_data['new_food']
|
||||||
old_ingredient = ingredients_form.cleaned_data['old_food']
|
old_food = food_form.cleaned_data['old_food']
|
||||||
recipe_ingredients = Ingredient.objects.filter(food=old_ingredient).all()
|
ingredients = Ingredient.objects.filter(food=old_food).all()
|
||||||
for i in recipe_ingredients:
|
for i in ingredients:
|
||||||
i.ingredient = new_ingredient
|
i.food = new_food
|
||||||
i.save()
|
i.save()
|
||||||
|
|
||||||
old_ingredient.delete()
|
old_food.delete()
|
||||||
success = True
|
success = True
|
||||||
messages.add_message(request, messages.SUCCESS, _('Ingredients merged!'))
|
messages.add_message(request, messages.SUCCESS, _('Foods merged!'))
|
||||||
|
|
||||||
if success:
|
if success:
|
||||||
units_form = UnitMergeForm()
|
units_form = UnitMergeForm()
|
||||||
ingredients_form = IngredientMergeForm()
|
food_form = FoodMergeForm()
|
||||||
else:
|
else:
|
||||||
units_form = UnitMergeForm()
|
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})
|
||||||
|
@ -31,10 +31,13 @@ def import_recipe(request):
|
|||||||
recipe = sr.save()
|
recipe = sr.save()
|
||||||
|
|
||||||
if data['image']:
|
if data['image']:
|
||||||
fmt, img = data['image'].split(';base64,')
|
try:
|
||||||
ext = fmt.split('/')[-1]
|
fmt, img = data['image'].split(';base64,')
|
||||||
recipe.image = ContentFile(base64.b64decode(img), name=f'{recipe.pk}.{ext}') # TODO possible security risk, maybe some checks needed
|
ext = fmt.split('/')[-1]
|
||||||
recipe.save()
|
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!'))
|
messages.add_message(request, messages.SUCCESS, _('Recipe imported successfully!'))
|
||||||
return HttpResponseRedirect(reverse_lazy('view_recipe', args=[recipe.pk]))
|
return HttpResponseRedirect(reverse_lazy('view_recipe', args=[recipe.pk]))
|
||||||
|
Loading…
Reference in New Issue
Block a user