Coverage for cookbook/integration/melarecipes.py: 23%

56 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 gettext import gettext as _ 

6from cookbook.helper.ingredient_parser import IngredientParser 

7from cookbook.helper.recipe_url_import import parse_servings, parse_time 

8from cookbook.integration.integration import Integration 

9from cookbook.models import Ingredient, Keyword, Recipe, Step 

10 

11 

12class MelaRecipes(Integration): 

13 

14 def split_recipe_file(self, file): 

15 return [json.loads(file.getvalue().decode("utf-8"))] 

16 

17 def get_files_from_recipes(self, recipes, el, cookie): 

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

19 

20 def get_recipe_from_file(self, file): 

21 recipe_json = file 

22 

23 recipe = Recipe.objects.create( 

24 name=recipe_json['title'].strip(), 

25 created_by=self.request.user, internal=True, space=self.request.space) 

26 

27 if 'yield' in recipe_json: 

28 recipe.servings = parse_servings(recipe_json['yield']) 

29 

30 if 'cookTime' in recipe_json: 

31 recipe.waiting_time = parse_time(recipe_json['cookTime']) 

32 

33 if 'prepTime' in recipe_json: 

34 recipe.working_time = parse_time(recipe_json['prepTime']) 

35 

36 if 'favorite' in recipe_json and recipe_json['favorite']: 

37 recipe.keywords.add(Keyword.objects.get_or_create(space=self.request.space, name=_('Favorite'))[0]) 

38 

39 if 'categories' in recipe_json: 

40 try: 

41 for x in recipe_json['categories']: 

42 recipe.keywords.add(Keyword.objects.get_or_create(space=self.request.space, name=x)[0]) 

43 except Exception: 

44 pass 

45 

46 instruction = '' 

47 if 'text' in recipe_json: 

48 instruction += f'*{recipe_json["text"].strip()}* \n' 

49 

50 if 'instructions' in recipe_json: 

51 instruction += recipe_json["instructions"].strip() + ' \n' 

52 

53 if 'notes' in recipe_json: 

54 instruction += recipe_json["notes"].strip() + ' \n' 

55 

56 if 'link' in recipe_json: 

57 recipe.source_url = recipe_json['link'] 

58 

59 step = Step.objects.create( 

60 instruction=instruction, space=self.request.space, show_ingredients_table=self.request.user.userpreference.show_step_ingredients 

61 ) 

62 

63 ingredient_parser = IngredientParser(self.request, True) 

64 for ingredient in recipe_json['ingredients'].split('\n'): 

65 if ingredient.strip() != '': 

66 amount, unit, food, note = ingredient_parser.parse(ingredient) 

67 f = ingredient_parser.get_food(food) 

68 u = ingredient_parser.get_unit(unit) 

69 step.ingredients.add(Ingredient.objects.create( 

70 food=f, unit=u, amount=amount, note=note, original_text=ingredient, space=self.request.space, 

71 )) 

72 recipe.steps.add(step) 

73 

74 if recipe_json.get("images", None): 

75 try: 

76 self.import_recipe_image(recipe, BytesIO(base64.b64decode(recipe_json['images'][0])), filetype='.jpeg') 

77 except Exception: 

78 pass 

79 

80 return recipe 

81 

82 def get_file_from_recipe(self, recipe): 

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