From 99fbc5e97c65337af660464cd01c095f71f96e08 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Wed, 9 Jun 2021 15:52:36 +0200 Subject: [PATCH] added openeats importer --- cookbook/integration/openeats.py | 56 +++++++++++++++++++++++++++++++- docs/features/import_export.md | 44 ++++++++++++++++++++++++- 2 files changed, 98 insertions(+), 2 deletions(-) diff --git a/cookbook/integration/openeats.py b/cookbook/integration/openeats.py index ca4c75ba..09edef68 100644 --- a/cookbook/integration/openeats.py +++ b/cookbook/integration/openeats.py @@ -1,3 +1,4 @@ +import json import re from django.utils.translation import gettext as _ @@ -10,8 +11,61 @@ from cookbook.models import Recipe, Step, Food, Unit, Ingredient 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, + servings=file['servings'], space=self.request.space, waiting_time=file['cook_time'], working_time=file['prep_time']) - return None + instructions = '' + if file["info"] != '': + instructions += file["info"] + + if file["directions"] != '': + instructions += file["directions"] + + if file["source"] != '': + instructions += file["source"] + + step = Step.objects.create(instruction=instructions) + + for ingredient in file['ingredients']: + f = get_food(ingredient['food'], self.request.space) + u = get_unit(ingredient['unit'], self.request.space) + step.ingredients.add(Ingredient.objects.create( + food=f, unit=u, amount=ingredient['amount'] + )) + recipe.steps.add(step) + + return recipe + + def split_recipe_file(self, file): + recipe_json = json.loads(file.read()) + recipe_dict = {} + ingredient_group_dict = {} + + for o in recipe_json: + if o['model'] == 'recipe.recipe': + recipe_dict[o['pk']] = { + 'name': o['fields']['title'], + 'info': o['fields']['info'], + 'directions': o['fields']['directions'], + 'source': o['fields']['source'], + 'prep_time': o['fields']['prep_time'], + 'cook_time': o['fields']['cook_time'], + 'servings': o['fields']['servings'], + 'ingredients': [], + } + if o['model'] == 'ingredient.ingredientgroup': + ingredient_group_dict[o['pk']] = o['fields']['recipe'] + + for o in recipe_json: + if o['model'] == 'ingredient.ingredient': + ingredient = { + 'food': o['fields']['title'], + 'unit': o['fields']['measurement'], + 'amount': round(o['fields']['numerator'] / o['fields']['denominator'], 2), + } + recipe_dict[ingredient_group_dict[o['fields']['ingredient_group']]]['ingredients'].append(ingredient) + + return list(recipe_dict.values()) def get_file_from_recipe(self, recipe): raise NotImplementedError('Method not implemented in storage integration') diff --git a/docs/features/import_export.md b/docs/features/import_export.md index c4497010..d3ea5d04 100644 --- a/docs/features/import_export.md +++ b/docs/features/import_export.md @@ -34,6 +34,7 @@ Overview of the capabilities of the different integrations. | Domestica | ✔️ | ⌚ | ✔️ | | MealMaster | ✔️ | ❌ | ❌ | | RezKonv | ✔️ | ❌ | ❌ | +| OpenEats | ✔️ | ❌ | ⌚ | ✔ = implemented, ❌ = not implemented and not possible/planned, ⌚ = not yet implemented @@ -160,4 +161,45 @@ The RezKonv format is primarily used in the german recipe manager RezKonv Suite. To migrate from RezKonv Suite to Tandoor select `Export > Gesamtes Kochbuch exportieren` (the last option in the export menu). The generated file can simply be imported into Tandoor. -As i only had limited sample data feel free to open an issue if your RezKonv export cannot be imported. \ No newline at end of file +As i only had limited sample data feel free to open an issue if your RezKonv export cannot be imported. + +## OpenEats +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`. + +Store the outputted json string in a `.json` file and simply import it using the importer. The file should look something like this +```json +[ + { + "model":"recipe.recipe", + "pk":1, + "fields":{ + "title":"Tasty Chili", + ... + } + }, + ... + { + "model":"ingredient.ingredientgroup", + "pk":1, + "fields":{ + "title":"Veges", + "recipe":1 + } + }, + ... + { + "model":"ingredient.ingredient", + "pk":1, + "fields":{ + "title":"black pepper", + "numerator":1.0, + "denominator":1.0, + "measurement":"dash", + "ingredient_group":1 + } + } +] + +``` \ No newline at end of file