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

1import base64 

2import json 

3from io import BytesIO 

4 

5from cookbook.helper.ingredient_parser import IngredientParser 

6from cookbook.integration.integration import Integration 

7from cookbook.models import Ingredient, Recipe, Step 

8 

9 

10class Domestica(Integration): 

11 

12 def get_recipe_from_file(self, file): 

13 

14 recipe = Recipe.objects.create( 

15 name=file['name'].strip(), 

16 created_by=self.request.user, internal=True, 

17 space=self.request.space) 

18 

19 if file['servings'] != '': 

20 recipe.servings = file['servings'] 

21 

22 if file['timeCook'] != '': 

23 recipe.waiting_time = file['timeCook'] 

24 

25 if file['timePrep'] != '': 

26 recipe.working_time = file['timePrep'] 

27 

28 recipe.save() 

29 

30 step = Step.objects.create( 

31 instruction=file['directions'], space=self.request.space, show_ingredients_table=self.request.user.userpreference.show_step_ingredients, 

32 ) 

33 

34 if file['source'] != '': 

35 step.instruction += '\n' + file['source'] 

36 

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) 

47 

48 if file['image'] != '': 

49 self.import_recipe_image(recipe, BytesIO(base64.b64decode(file['image'].replace('data:image/jpeg;base64,', ''))), filetype='.jpeg') 

50 

51 return recipe 

52 

53 def get_file_from_recipe(self, recipe): 

54 raise NotImplementedError('Method not implemented in storage integration') 

55 

56 def split_recipe_file(self, file): 

57 return json.loads(file.read().decode("utf-8"))