From 01a53ad8ec52bb4375afe3c1aa3d5e8918fe6a02 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Mon, 5 Apr 2021 17:55:06 +0200 Subject: [PATCH] paprika improvements and importer fixes --- cookbook/integration/Pepperplate.py | 13 ++++---- cookbook/integration/cheftap.py | 13 ++++---- cookbook/integration/domestica.py | 13 ++++---- cookbook/integration/mealmaster.py | 13 ++++---- cookbook/integration/paprika.py | 50 ++++++++++++++++++++++++----- cookbook/integration/rezkonv.py | 13 ++++---- cookbook/views/import_export.py | 2 +- 7 files changed, 78 insertions(+), 39 deletions(-) diff --git a/cookbook/integration/Pepperplate.py b/cookbook/integration/Pepperplate.py index 57b4aa67f0..f5d463dd47 100644 --- a/cookbook/integration/Pepperplate.py +++ b/cookbook/integration/Pepperplate.py @@ -44,12 +44,13 @@ def get_recipe_from_file(self, file): ) for ingredient in ingredients: - amount, unit, ingredient, note = parse(ingredient) - f = get_food(ingredient, self.request.space) - u = get_unit(unit, self.request.space) - step.ingredients.add(Ingredient.objects.create( - food=f, unit=u, amount=amount, note=note - )) + if len(ingredient.strip()) > 0: + amount, unit, ingredient, note = parse(ingredient) + f = get_food(ingredient, self.request.space) + u = get_unit(unit, self.request.space) + step.ingredients.add(Ingredient.objects.create( + food=f, unit=u, amount=amount, note=note + )) recipe.steps.add(step) return recipe diff --git a/cookbook/integration/cheftap.py b/cookbook/integration/cheftap.py index b762528397..0e51c1e157 100644 --- a/cookbook/integration/cheftap.py +++ b/cookbook/integration/cheftap.py @@ -45,12 +45,13 @@ def get_recipe_from_file(self, file): step.save() for ingredient in ingredients: - amount, unit, ingredient, note = parse(ingredient) - f = get_food(ingredient, self.request.space) - u = get_unit(unit, self.request.space) - step.ingredients.add(Ingredient.objects.create( - food=f, unit=u, amount=amount, note=note - )) + if len(ingredient.strip()) > 0: + amount, unit, ingredient, note = parse(ingredient) + f = get_food(ingredient, self.request.space) + u = get_unit(unit, self.request.space) + step.ingredients.add(Ingredient.objects.create( + food=f, unit=u, amount=amount, note=note + )) recipe.steps.add(step) return recipe diff --git a/cookbook/integration/domestica.py b/cookbook/integration/domestica.py index 9f66a4cccb..ec9f5ce24f 100644 --- a/cookbook/integration/domestica.py +++ b/cookbook/integration/domestica.py @@ -35,12 +35,13 @@ def get_recipe_from_file(self, file): step.instruction += '\n' + file['source'] for ingredient in file['ingredients'].split('\n'): - amount, unit, ingredient, note = parse(ingredient) - f = get_food(ingredient, self.request.space) - u = get_unit(unit, self.request.space) - step.ingredients.add(Ingredient.objects.create( - food=f, unit=u, amount=amount, note=note - )) + if len(ingredient.strip()) > 0: + amount, unit, ingredient, note = parse(ingredient) + f = get_food(ingredient, self.request.space) + u = get_unit(unit, self.request.space) + step.ingredients.add(Ingredient.objects.create( + food=f, unit=u, amount=amount, note=note + )) recipe.steps.add(step) if file['image'] != '': diff --git a/cookbook/integration/mealmaster.py b/cookbook/integration/mealmaster.py index cb3ca61157..01f58331a9 100644 --- a/cookbook/integration/mealmaster.py +++ b/cookbook/integration/mealmaster.py @@ -48,12 +48,13 @@ def get_recipe_from_file(self, file): ) for ingredient in ingredients: - amount, unit, ingredient, note = parse(ingredient) - f = get_food(ingredient, self.request.space) - u = get_unit(unit, self.request.space) - step.ingredients.add(Ingredient.objects.create( - food=f, unit=u, amount=amount, note=note - )) + if len(ingredient.strip()) > 0: + amount, unit, ingredient, note = parse(ingredient) + f = get_food(ingredient, self.request.space) + u = get_unit(unit, self.request.space) + step.ingredients.add(Ingredient.objects.create( + food=f, unit=u, amount=amount, note=note + )) recipe.steps.add(step) return recipe diff --git a/cookbook/integration/paprika.py b/cookbook/integration/paprika.py index d4856ea82f..2e0b6d10d0 100644 --- a/cookbook/integration/paprika.py +++ b/cookbook/integration/paprika.py @@ -1,11 +1,13 @@ import base64 import gzip import json +import re from io import BytesIO from cookbook.helper.ingredient_parser import parse, get_food, get_unit from cookbook.integration.integration import Integration -from cookbook.models import Recipe, Step, Ingredient +from cookbook.models import Recipe, Step, Ingredient, Keyword +from gettext import gettext as _ class Paprika(Integration): @@ -21,17 +23,49 @@ def get_recipe_from_file(self, file): name=recipe_json['name'].strip(), description=recipe_json['description'].strip(), created_by=self.request.user, internal=True, space=self.request.space) + try: + if re.match(r'([0-9])+\s(.)*', recipe_json['servings'] ): + s = recipe_json['servings'].split(' ') + recipe.servings = s[0] + recipe.servings_text = s[1] + + if len(recipe_json['cook_time'].strip()) > 0: + recipe.waiting_time = re.findall(r'\d+', recipe_json['cook_time'])[0] + + if len(recipe_json['prep_time'].strip()) > 0: + recipe.working_time = re.findall(r'\d+', recipe_json['prep_time'])[0] + except Exception: + pass + + recipe.save() + + instructions = recipe_json['directions'] + if len(recipe_json['notes'].strip()) > 0: + instructions += '\n\n### ' + _('Notes') + ' \n' + recipe_json['notes'] + + if len(recipe_json['nutritional_info'].strip()) > 0: + instructions += '\n\n### ' + _('Nutritional Information') + ' \n' + recipe_json['nutritional_info'] + + if len(recipe_json['source'].strip()) > 0 or len(recipe_json['source_url'].strip()) > 0: + instructions += '\n\n### ' + _('Source') + ' \n' + recipe_json['source'].strip() + ' \n' + recipe_json['source_url'].strip() + step = Step.objects.create( - instruction=recipe_json['directions'] + '\n\n' + recipe_json['nutritional_info'] + instruction=instructions ) + if 'categories' in recipe_json: + for c in recipe_json['categories']: + keyword, created = Keyword.objects.get_or_create(name=c.strip(), space=self.request.space) + recipe.keywords.add(keyword) + for ingredient in recipe_json['ingredients'].split('\n'): - amount, unit, ingredient, note = parse(ingredient) - f = get_food(ingredient, self.request.space) - u = get_unit(unit, self.request.space) - step.ingredients.add(Ingredient.objects.create( - food=f, unit=u, amount=amount, note=note - )) + if len(ingredient.strip()) > 0: + amount, unit, ingredient, note = parse(ingredient) + f = get_food(ingredient, self.request.space) + u = get_unit(unit, self.request.space) + step.ingredients.add(Ingredient.objects.create( + food=f, unit=u, amount=amount, note=note + )) recipe.steps.add(step) diff --git a/cookbook/integration/rezkonv.py b/cookbook/integration/rezkonv.py index 4d89b62985..2a2bbe184b 100644 --- a/cookbook/integration/rezkonv.py +++ b/cookbook/integration/rezkonv.py @@ -47,12 +47,13 @@ def get_recipe_from_file(self, file): ) for ingredient in ingredients: - amount, unit, ingredient, note = parse(ingredient) - f = get_food(ingredient, self.request.space) - u = get_unit(unit, self.request.space) - step.ingredients.add(Ingredient.objects.create( - food=f, unit=u, amount=amount, note=note - )) + if len(ingredient.strip()) > 0: + amount, unit, ingredient, note = parse(ingredient) + f = get_food(ingredient, self.request.space) + u = get_unit(unit, self.request.space) + step.ingredients.add(Ingredient.objects.create( + food=f, unit=u, amount=amount, note=note + )) recipe.steps.add(step) return recipe diff --git a/cookbook/views/import_export.py b/cookbook/views/import_export.py index 6b57bb08f7..1276306769 100644 --- a/cookbook/views/import_export.py +++ b/cookbook/views/import_export.py @@ -64,7 +64,7 @@ def import_recipe(request): files = [] for f in request.FILES.getlist('files'): files.append({'file': BytesIO(f.read()), 'name': f.name}) - t = threading.Thread(target=integration.do_import, args=[files, il, form['duplicates']]) + t = threading.Thread(target=integration.do_import, args=[files, il, form.cleaned_data['duplicates']]) t.setDaemon(True) t.start()