fixed importer empty units
This commit is contained in:
parent
bdd004518c
commit
29e1d1286c
@ -125,7 +125,7 @@ def import_url(request):
|
||||
ingredient = Ingredient()
|
||||
|
||||
ingredient.food, f_created = Food.objects.get_or_create(name=ing['ingredient']['text'])
|
||||
if ing['unit']:
|
||||
if ing['unit'] and ing['unit']['text'] != '':
|
||||
ingredient.unit, u_created = Unit.objects.get_or_create(name=ing['unit']['text'])
|
||||
|
||||
# TODO properly handle no_amount recipes
|
||||
@ -143,7 +143,7 @@ def import_url(request):
|
||||
step.ingredients.add(ingredient)
|
||||
print(ingredient)
|
||||
|
||||
if data['image'] != '':
|
||||
if 'image' in data and data['image'] != '':
|
||||
try:
|
||||
response = requests.get(data['image'])
|
||||
img = Image.open(BytesIO(response.content))
|
||||
|
@ -239,27 +239,33 @@ def edit_ingredients(request):
|
||||
if units_form.is_valid():
|
||||
new_unit = units_form.cleaned_data['new_unit']
|
||||
old_unit = units_form.cleaned_data['old_unit']
|
||||
recipe_ingredients = Ingredient.objects.filter(unit=old_unit).all()
|
||||
for i in recipe_ingredients:
|
||||
i.unit = new_unit
|
||||
i.save()
|
||||
if new_unit != old_unit:
|
||||
recipe_ingredients = Ingredient.objects.filter(unit=old_unit).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!'))
|
||||
old_unit.delete()
|
||||
success = True
|
||||
messages.add_message(request, messages.SUCCESS, _('Units merged!'))
|
||||
else:
|
||||
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']
|
||||
ingredients = Ingredient.objects.filter(food=old_food).all()
|
||||
for i in ingredients:
|
||||
i.food = new_food
|
||||
i.save()
|
||||
if new_food != old_food:
|
||||
ingredients = Ingredient.objects.filter(food=old_food).all()
|
||||
for i in ingredients:
|
||||
i.food = new_food
|
||||
i.save()
|
||||
|
||||
old_food.delete()
|
||||
success = True
|
||||
messages.add_message(request, messages.SUCCESS, _('Foods merged!'))
|
||||
old_food.delete()
|
||||
success = True
|
||||
messages.add_message(request, messages.SUCCESS, _('Foods merged!'))
|
||||
else:
|
||||
messages.add_message(request, messages.ERROR, _('Cannot merge with the same object!'))
|
||||
|
||||
if success:
|
||||
units_form = UnitMergeForm()
|
||||
|
Loading…
Reference in New Issue
Block a user