updated cookbookapp importer to handle multi-step recipes

This commit is contained in:
smilerz 2022-07-11 17:21:56 -05:00
parent 38219a22ca
commit b03fa4fdf2
No known key found for this signature in database
GPG Key ID: 39444C7606D47126

View File

@ -10,7 +10,8 @@ import validators
import yaml import yaml
from cookbook.helper.ingredient_parser import IngredientParser from cookbook.helper.ingredient_parser import IngredientParser
from cookbook.helper.recipe_url_import import get_images_from_soup, iso_duration_to_minutes, get_from_scraper from cookbook.helper.recipe_url_import import (get_from_scraper, get_images_from_soup,
iso_duration_to_minutes)
from cookbook.helper.scrapers.scrapers import text_scraper from cookbook.helper.scrapers.scrapers import text_scraper
from cookbook.integration.integration import Integration from cookbook.integration.integration import Integration
from cookbook.models import Ingredient, Keyword, Recipe, Step from cookbook.models import Ingredient, Keyword, Recipe, Step
@ -45,8 +46,8 @@ class CookBookApp(Integration):
except Exception: except Exception:
pass pass
# assuming import files only contain single step
step = Step.objects.create(instruction=recipe_json['recipeInstructions'], space=self.request.space, ) step = Step.objects.create(instruction=recipe_json['steps'][0]['instruction'], space=self.request.space, )
if 'nutrition' in recipe_json: if 'nutrition' in recipe_json:
step.instruction = step.instruction + '\n\n' + recipe_json['nutrition'] step.instruction = step.instruction + '\n\n' + recipe_json['nutrition']
@ -55,11 +56,13 @@ class CookBookApp(Integration):
recipe.steps.add(step) recipe.steps.add(step)
ingredient_parser = IngredientParser(self.request, True) ingredient_parser = IngredientParser(self.request, True)
for ingredient in recipe_json['recipeIngredient']: for ingredient in recipe_json['steps'][0]['ingredients']:
f = ingredient_parser.get_food(ingredient['ingredient']['text']) f = ingredient_parser.get_food(ingredient['food']['name'])
u = ingredient_parser.get_unit(ingredient['unit']['text']) u = None
if unit := ingredient.get('unit', None):
u = ingredient_parser.get_unit(unit.get('name', None))
step.ingredients.add(Ingredient.objects.create( step.ingredients.add(Ingredient.objects.create(
food=f, unit=u, amount=ingredient['amount'], note=ingredient['note'], space=self.request.space, food=f, unit=u, amount=ingredient.get('amount', None), note=ingredient.get('note', None), original_text=ingredient.get('original_text', None), space=self.request.space,
)) ))
if len(images) > 0: if len(images) > 0: