Coverage for cookbook/integration/domestica.py: 29%
34 statements
« prev ^ index » next coverage.py v7.4.0, created at 2023-12-29 00:47 +0100
« prev ^ index » next coverage.py v7.4.0, created at 2023-12-29 00:47 +0100
1import base64
2import json
3from io import BytesIO
5from cookbook.helper.ingredient_parser import IngredientParser
6from cookbook.integration.integration import Integration
7from cookbook.models import Ingredient, Recipe, Step
10class Domestica(Integration):
12 def get_recipe_from_file(self, file):
14 recipe = Recipe.objects.create(
15 name=file['name'].strip(),
16 created_by=self.request.user, internal=True,
17 space=self.request.space)
19 if file['servings'] != '':
20 recipe.servings = file['servings']
22 if file['timeCook'] != '':
23 recipe.waiting_time = file['timeCook']
25 if file['timePrep'] != '':
26 recipe.working_time = file['timePrep']
28 recipe.save()
30 step = Step.objects.create(
31 instruction=file['directions'], space=self.request.space, show_ingredients_table=self.request.user.userpreference.show_step_ingredients,
32 )
34 if file['source'] != '':
35 step.instruction += '\n' + file['source']
37 ingredient_parser = IngredientParser(self.request, True)
38 for ingredient in file['ingredients'].split('\n'):
39 if len(ingredient.strip()) > 0:
40 amount, unit, food, note = ingredient_parser.parse(ingredient)
41 f = ingredient_parser.get_food(food)
42 u = ingredient_parser.get_unit(unit)
43 step.ingredients.add(Ingredient.objects.create(
44 food=f, unit=u, amount=amount, note=note, original_text=ingredient, space=self.request.space,
45 ))
46 recipe.steps.add(step)
48 if file['image'] != '':
49 self.import_recipe_image(recipe, BytesIO(base64.b64decode(file['image'].replace('data:image/jpeg;base64,', ''))), filetype='.jpeg')
51 return recipe
53 def get_file_from_recipe(self, recipe):
54 raise NotImplementedError('Method not implemented in storage integration')
56 def split_recipe_file(self, file):
57 return json.loads(file.read().decode("utf-8"))