From 5bad5078450c6fde495cf10bd0314495e80e18b7 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Sun, 5 Jul 2020 21:11:25 +0200 Subject: [PATCH] fixed and improved URL import --- cookbook/helper/recipe_url_import.py | 18 ++++++++++++------ cookbook/views/data.py | 11 ++++++++--- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/cookbook/helper/recipe_url_import.py b/cookbook/helper/recipe_url_import.py index c91967855..b7593631a 100644 --- a/cookbook/helper/recipe_url_import.py +++ b/cookbook/helper/recipe_url_import.py @@ -164,16 +164,22 @@ def find_recipe_json(ld_json, url): ld_json['image'] = '' if 'cookTime' in ld_json: - if type(ld_json['cookTime']) == list and len(ld_json['cookTime']) > 0: - ld_json['cookTime'] = ld_json['cookTime'][0] - ld_json['cookTime'] = round(parse_duration(ld_json['cookTime']).seconds / 60) + try: + if type(ld_json['cookTime']) == list and len(ld_json['cookTime']) > 0: + ld_json['cookTime'] = ld_json['cookTime'][0] + ld_json['cookTime'] = round(parse_duration(ld_json['cookTime']).seconds / 60) + except TypeError: + ld_json['cookTime'] = 0 else: ld_json['cookTime'] = 0 if 'prepTime' in ld_json: - if type(ld_json['prepTime']) == list and len(ld_json['prepTime']) > 0: - ld_json['prepTime'] = ld_json['prepTime'][0] - ld_json['prepTime'] = round(parse_duration(ld_json['prepTime']).seconds / 60) + try: + if type(ld_json['prepTime']) == list and len(ld_json['prepTime']) > 0: + ld_json['prepTime'] = ld_json['prepTime'][0] + ld_json['prepTime'] = round(parse_duration(ld_json['prepTime']).seconds / 60) + except TypeError: + ld_json['prepTime'] = 0 else: ld_json['prepTime'] = 0 diff --git a/cookbook/views/data.py b/cookbook/views/data.py index 15e0f7a6f..17879ecf1 100644 --- a/cookbook/views/data.py +++ b/cookbook/views/data.py @@ -100,13 +100,18 @@ def import_url(request): recipe = Recipe.objects.create( name=data['name'], - instructions=data['recipeInstructions'], waiting_time=data['cookTime'], working_time=data['prepTime'], internal=True, created_by=request.user, ) + step = Step.objects.create( + instruction=data['recipeInstructions'], + ) + + recipe.steps.add(step) + for kw in data['keywords']: if kw['id'] != "null" and (k := Keyword.objects.filter(id=kw['id']).first()): recipe.keywords.add(k) @@ -115,7 +120,7 @@ def import_url(request): recipe.keywords.add(k) for ing in data['recipeIngredient']: - i, i_created = Food.objects.get_or_create(name=ing['ingredient']['text']) + 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: @@ -128,7 +133,7 @@ def import_url(request): # TODO return proper error pass - Ingredient.objects.create(recipe=recipe, ingredient=i, unit=u, amount=ing['amount']) + step.ingredients.add(Ingredient.objects.create(food=f, unit=u, amount=ing['amount'])) if data['image'] != '': response = requests.get(data['image'])