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/>
{% endif %}
<div class="row" v-if="recipe && has_ingredients"> <!-- TODO duplicate code remove -->
<div class="col-md-6 order-md-1 col-sm-12 order-sm-2 col-12 order-2">
<div class="row">
<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-body">
<div class="row">

View File

@ -348,7 +348,9 @@
},
openUnitSelect: function (id) {
let index = id.replace('unit_', '')
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)
}

View File

@ -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'])