From a1b8f736c2b64c9e0860be7b45120619bdc2a590 Mon Sep 17 00:00:00 2001 From: Thomas Preece Date: Mon, 20 Mar 2023 18:18:11 +0000 Subject: [PATCH 1/4] Improve parsing of OpenEats imports --- cookbook/integration/openeats.py | 62 +++++++++++++++++++++++++++++--- docs/features/import_export.md | 4 ++- 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/cookbook/integration/openeats.py b/cookbook/integration/openeats.py index a8485f05..519d3f4f 100644 --- a/cookbook/integration/openeats.py +++ b/cookbook/integration/openeats.py @@ -2,24 +2,48 @@ import json from cookbook.helper.ingredient_parser import IngredientParser from cookbook.integration.integration import Integration -from cookbook.models import Ingredient, Recipe, Step +from cookbook.models import Ingredient, Recipe, Step, Keyword, Comment, CookLog class OpenEats(Integration): def get_recipe_from_file(self, file): - recipe = Recipe.objects.create(name=file['name'].strip(), created_by=self.request.user, internal=True, + recipe = Recipe.objects.create(name=file['name'].strip(), description=file['info'], created_by=self.request.user, internal=True, servings=file['servings'], space=self.request.space, waiting_time=file['cook_time'], working_time=file['prep_time']) instructions = '' - if file["info"] != '': - instructions += file["info"] if file["directions"] != '': instructions += file["directions"] if file["source"] != '': - instructions += file["source"] + instructions += f'\nRecipe source: [{file["source"]}]({file["source"]})' + + cuisine_keyword, created = Keyword.objects.get_or_create(name="Cuisine", space=self.request.space) + if file["cuisine"] != '': + keyword, created = Keyword.objects.get_or_create(name=file["cuisine"].strip(), space=self.request.space) + if created: + keyword.move(cuisine_keyword, pos="last-child") + recipe.keywords.add(keyword) + + course_keyword, created = Keyword.objects.get_or_create(name="Course", space=self.request.space) + if file["course"] != '': + keyword, created = Keyword.objects.get_or_create(name=file["course"].strip(), space=self.request.space) + if created: + keyword.move(course_keyword, pos="last-child") + recipe.keywords.add(keyword) + + for tag in file["tags"]: + keyword, created = Keyword.objects.get_or_create(name=tag.strip(), space=self.request.space) + recipe.keywords.add(keyword) + + for comment in file['comments']: + Comment.objects.create(recipe=recipe, text=comment['text'], created_by=self.request.user) + CookLog.objects.create(recipe=recipe, rating=comment['rating'], created_by=self.request.user, space=self.request.space) + + if file["photo"] != '': + recipe.image = f'recipes/openeats-import/{file["photo"]}' + recipe.save() step = Step.objects.create(instruction=instructions, space=self.request.space,) @@ -38,6 +62,9 @@ class OpenEats(Integration): recipe_json = json.loads(file.read()) recipe_dict = {} ingredient_group_dict = {} + cuisine_group_dict = {} + course_group_dict = {} + tag_group_dict = {} for o in recipe_json: if o['model'] == 'recipe.recipe': @@ -50,11 +77,27 @@ class OpenEats(Integration): 'cook_time': o['fields']['cook_time'], 'servings': o['fields']['servings'], 'ingredients': [], + 'photo': o['fields']['photo'], + 'cuisine': o['fields']['cuisine'], + 'course': o['fields']['course'], + 'tags': o['fields']['tags'], + 'comments': [], } if o['model'] == 'ingredient.ingredientgroup': ingredient_group_dict[o['pk']] = o['fields']['recipe'] + if o['model'] == 'recipe_groups.cuisine': + cuisine_group_dict[o['pk']] = o['fields']['title'] + if o['model'] == 'recipe_groups.course': + course_group_dict[o['pk']] = o['fields']['title'] + if o['model'] == 'recipe_groups.tag': + tag_group_dict[o['pk']] = o['fields']['title'] for o in recipe_json: + if o['model'] == 'rating.rating': + recipe_dict[o['fields']['recipe']]["comments"].append({ + "text": o['fields']['comment'], + "rating": o['fields']['rating'] + }) if o['model'] == 'ingredient.ingredient': ingredient = { 'food': o['fields']['title'], @@ -63,6 +106,15 @@ class OpenEats(Integration): } recipe_dict[ingredient_group_dict[o['fields']['ingredient_group']]]['ingredients'].append(ingredient) + for k, r in recipe_dict.items(): + if r["cuisine"] in cuisine_group_dict: + r["cuisine"] = cuisine_group_dict[r["cuisine"]] + if r["course"] in course_group_dict: + r["course"] = course_group_dict[r["course"]] + for index in range(len(r["tags"])): + if r["tags"][index] in tag_group_dict: + r["tags"][index] = tag_group_dict[r["tags"][index]] + return list(recipe_dict.values()) def get_file_from_recipe(self, recipe): diff --git a/docs/features/import_export.md b/docs/features/import_export.md index d775cf9f..a2fa3040 100644 --- a/docs/features/import_export.md +++ b/docs/features/import_export.md @@ -178,7 +178,7 @@ This zip file can simply be imported into Tandoor. OpenEats does not provide any way to export the data using the interface. Luckily it is relatively easy to export it from the command line. You need to run the command `python manage.py dumpdata recipe ingredient` inside of the application api container. If you followed the default installation method you can use the following command `docker-compose -f docker-prod.yml run --rm --entrypoint 'sh' api ./manage.py dumpdata recipe ingredient`. -This command might also work `docker exec -it openeats_api_1 ./manage.py dumpdata recipe ingredient > recipe_ingredients.json` +This command might also work `docker exec -it openeats_api_1 ./manage.py dumpdata recipe ingredient rating recipe_groups > recipe_ingredients.json` Store the outputted json string in a `.json` file and simply import it using the importer. The file should look something like this ```json @@ -216,6 +216,8 @@ Store the outputted json string in a `.json` file and simply import it using the ``` +To import your images you'll need to create the folder `openeats-import` in your Tandoor's `recipes` media folder (which is usually found inside `/opt/recipes/mediafiles`). After that you'll need to copy the `/code/site-media/upload` folder from the openeats API docker container to the `openeats` folder you created. You should now have the file path `/opt/recipes/mediafiles/recipes/openeats-import/upload/...` in Tandoor. + ## Plantoeat Plan to eat allows you to export a text file containing all your recipes. Simply upload that text file to Tandoor to import all recipes From 5a145d7f8eb91904abc4e98332f3117e10b3d308 Mon Sep 17 00:00:00 2001 From: Thomas Preece Date: Tue, 28 Mar 2023 17:29:38 +0100 Subject: [PATCH 2/4] PR feedback changes --- cookbook/integration/openeats.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/cookbook/integration/openeats.py b/cookbook/integration/openeats.py index 519d3f4f..9188ca8d 100644 --- a/cookbook/integration/openeats.py +++ b/cookbook/integration/openeats.py @@ -3,12 +3,18 @@ import json from cookbook.helper.ingredient_parser import IngredientParser from cookbook.integration.integration import Integration from cookbook.models import Ingredient, Recipe, Step, Keyword, Comment, CookLog - +from django.utils.translation import gettext as _ class OpenEats(Integration): def get_recipe_from_file(self, file): - recipe = Recipe.objects.create(name=file['name'].strip(), description=file['info'], created_by=self.request.user, internal=True, + + description = file['info'] + description_max_length = Recipe._meta.get_field('description').max_length + if len(description) > description_max_length: + description = description[0:description_max_length] + + recipe = Recipe.objects.create(name=file['name'].strip(), description=description, created_by=self.request.user, internal=True, servings=file['servings'], space=self.request.space, waiting_time=file['cook_time'], working_time=file['prep_time']) instructions = '' @@ -17,7 +23,7 @@ class OpenEats(Integration): instructions += file["directions"] if file["source"] != '': - instructions += f'\nRecipe source: [{file["source"]}]({file["source"]})' + instructions += '\n' + _('Recipe source:') + f'[{file["source"]}]({file["source"]})' cuisine_keyword, created = Keyword.objects.get_or_create(name="Cuisine", space=self.request.space) if file["cuisine"] != '': From 073ee7e96395ada0506196797487483bde354a4d Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Wed, 29 Mar 2023 16:03:08 +0200 Subject: [PATCH 3/4] Update openeats.py --- cookbook/integration/openeats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cookbook/integration/openeats.py b/cookbook/integration/openeats.py index 9188ca8d..794d3593 100644 --- a/cookbook/integration/openeats.py +++ b/cookbook/integration/openeats.py @@ -9,7 +9,7 @@ class OpenEats(Integration): def get_recipe_from_file(self, file): - description = file['info'] + description = file['info'][:512] description_max_length = Recipe._meta.get_field('description').max_length if len(description) > description_max_length: description = description[0:description_max_length] From 11620ba2b60a53236d62a92d21f444c5eac3e4ea Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Wed, 29 Mar 2023 16:03:33 +0200 Subject: [PATCH 4/4] Update openeats.py --- cookbook/integration/openeats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cookbook/integration/openeats.py b/cookbook/integration/openeats.py index 794d3593..9188ca8d 100644 --- a/cookbook/integration/openeats.py +++ b/cookbook/integration/openeats.py @@ -9,7 +9,7 @@ class OpenEats(Integration): def get_recipe_from_file(self, file): - description = file['info'][:512] + description = file['info'] description_max_length = Recipe._meta.get_field('description').max_length if len(description) > description_max_length: description = description[0:description_max_length]