diff --git a/cookbook/templates/url_import.html b/cookbook/templates/url_import.html
index f61c2a82f..2b5e327cc 100644
--- a/cookbook/templates/url_import.html
+++ b/cookbook/templates/url_import.html
@@ -348,7 +348,9 @@
},
openUnitSelect: function (id) {
let index = id.replace('unit_', '')
- this.$set(app.$refs.unit[index].$data, 'search', this.recipe_data.recipeIngredient[index].unit.text)
+ if (this.recipe_data.recipeIngredient[index].unit !== null) {
+ this.$set(app.$refs.unit[index].$data, 'search', this.recipe_data.recipeIngredient[index].unit.text)
+ }
},
openIngredientSelect: function (id) {
let index = id.replace('ingredient_', '')
@@ -370,7 +372,7 @@
this.units = response.data.results;
if (this.recipe_data !== undefined) {
for (let x of Array.from(this.recipe_data.recipeIngredient)) {
- if (x.unit.text !== '') {
+ if (x.unit !== null && x.unit.text !== '') {
this.units = this.units.filter(item => item.text !== x.unit.text)
this.units.push(x.unit)
}
diff --git a/cookbook/views/data.py b/cookbook/views/data.py
index 102241d59..93ce5329b 100644
--- a/cookbook/views/data.py
+++ b/cookbook/views/data.py
@@ -6,6 +6,7 @@ import requests
from PIL import Image
from django.contrib import messages
from django.core.files import File
+from django.db.transaction import atomic
from django.utils.translation import gettext as _
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import redirect, render
@@ -94,6 +95,7 @@ def batch_edit(request):
@group_required('user')
+@atomic
def import_url(request):
if request.method == 'POST':
data = json.loads(request.body)
@@ -120,23 +122,26 @@ def import_url(request):
recipe.keywords.add(k)
for ing in data['recipeIngredient']:
- f, f_created = Food.objects.get_or_create(name=ing['ingredient']['text'])
- if ing['unit']:
- u, u_created = Unit.objects.get_or_create(name=ing['unit']['text'])
- else:
- u = Unit.objects.get(name=request.user.userpreference.default_unit)
+ ingredient = Ingredient()
- # TODO properly handl no_amount recipes
+ ingredient.food, f_created = Food.objects.get_or_create(name=ing['ingredient']['text'])
+ if ing['unit']:
+ ingredient.unit, u_created = Unit.objects.get_or_create(name=ing['unit']['text'])
+
+ # TODO properly handle no_amount recipes
if isinstance(ing['amount'], str):
try:
- ing['amount'] = float(ing['amount'].replace(',', '.'))
+ ingredient.amount = float(ing['amount'].replace(',', '.'))
except ValueError:
- # TODO return proper error
+ ingredient.no_amount = True
pass
+ elif isinstance(ing['amount'], float) or isinstance(ing['amount'], int):
+ ingredient.amount = ing['amount']
+ ingredient.note = ing['note'] if 'note' in ing else ''
- note = ing['note'] if 'note' in ing else ''
-
- step.ingredients.add(Ingredient.objects.create(food=f, unit=u, amount=ing['amount'], note=note))
+ ingredient.save()
+ step.ingredients.add(ingredient)
+ print(ingredient)
if data['image'] != '':
response = requests.get(data['image'])