fixed import and validation errors

This commit is contained in:
vabene1111
2020-09-01 13:26:58 +02:00
parent 62868cd2b2
commit 2a0a85018a
3 changed files with 22 additions and 15 deletions

View File

@ -95,8 +95,8 @@
<br/> <br/>
{% endif %} {% endif %}
<div class="row" v-if="recipe && has_ingredients"> <!-- TODO duplicate code remove --> <div class="row">
<div class="col-md-6 order-md-1 col-sm-12 order-sm-2 col-12 order-2"> <div class="col-md-6 order-md-1 col-sm-12 order-sm-2 col-12 order-2" v-if="recipe && has_ingredients"> <!-- TODO duplicate code remove -->
<div class="card border-primary"> <div class="card border-primary">
<div class="card-body"> <div class="card-body">
<div class="row"> <div class="row">

View File

@ -348,7 +348,9 @@
}, },
openUnitSelect: function (id) { openUnitSelect: function (id) {
let index = id.replace('unit_', '') 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) { openIngredientSelect: function (id) {
let index = id.replace('ingredient_', '') let index = id.replace('ingredient_', '')
@ -370,7 +372,7 @@
this.units = response.data.results; this.units = response.data.results;
if (this.recipe_data !== undefined) { if (this.recipe_data !== undefined) {
for (let x of Array.from(this.recipe_data.recipeIngredient)) { 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 = this.units.filter(item => item.text !== x.unit.text)
this.units.push(x.unit) this.units.push(x.unit)
} }

View File

@ -6,6 +6,7 @@ import requests
from PIL import Image from PIL import Image
from django.contrib import messages from django.contrib import messages
from django.core.files import File from django.core.files import File
from django.db.transaction import atomic
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from django.http import HttpResponseRedirect, HttpResponse from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import redirect, render from django.shortcuts import redirect, render
@ -94,6 +95,7 @@ def batch_edit(request):
@group_required('user') @group_required('user')
@atomic
def import_url(request): def import_url(request):
if request.method == 'POST': if request.method == 'POST':
data = json.loads(request.body) data = json.loads(request.body)
@ -120,23 +122,26 @@ def import_url(request):
recipe.keywords.add(k) recipe.keywords.add(k)
for ing in data['recipeIngredient']: for ing in data['recipeIngredient']:
f, f_created = Food.objects.get_or_create(name=ing['ingredient']['text']) ingredient = Ingredient()
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)
# 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): if isinstance(ing['amount'], str):
try: try:
ing['amount'] = float(ing['amount'].replace(',', '.')) ingredient.amount = float(ing['amount'].replace(',', '.'))
except ValueError: except ValueError:
# TODO return proper error ingredient.no_amount = True
pass 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 '' ingredient.save()
step.ingredients.add(ingredient)
step.ingredients.add(Ingredient.objects.create(food=f, unit=u, amount=ing['amount'], note=note)) print(ingredient)
if data['image'] != '': if data['image'] != '':
response = requests.get(data['image']) response = requests.get(data['image'])