Coverage for cookbook/integration/openeats.py: 11%

81 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2023-12-29 00:47 +0100

1import json 

2 

3from django.utils.translation import gettext as _ 

4 

5from cookbook.helper.ingredient_parser import IngredientParser 

6from cookbook.integration.integration import Integration 

7from cookbook.models import Comment, CookLog, Ingredient, Keyword, Recipe, Step 

8 

9 

10class OpenEats(Integration): 

11 

12 def get_recipe_from_file(self, file): 

13 

14 description = file['info'] 

15 description_max_length = Recipe._meta.get_field('description').max_length 

16 if len(description) > description_max_length: 

17 description = description[0:description_max_length] 

18 

19 recipe = Recipe.objects.create(name=file['name'].strip(), description=description, created_by=self.request.user, internal=True, 

20 servings=file['servings'], space=self.request.space, waiting_time=file['cook_time'], working_time=file['prep_time']) 

21 

22 instructions = '' 

23 

24 if file["directions"] != '': 

25 instructions += file["directions"] 

26 

27 if file["source"] != '': 

28 instructions += '\n' + _('Recipe source:') + f'[{file["source"]}]({file["source"]})' 

29 

30 cuisine_keyword, created = Keyword.objects.get_or_create(name="Cuisine", space=self.request.space) 

31 if file["cuisine"] != '': 

32 keyword, created = Keyword.objects.get_or_create(name=file["cuisine"].strip(), space=self.request.space) 

33 if created: 

34 keyword.move(cuisine_keyword, pos="last-child") 

35 recipe.keywords.add(keyword) 

36 

37 course_keyword, created = Keyword.objects.get_or_create(name="Course", space=self.request.space) 

38 if file["course"] != '': 

39 keyword, created = Keyword.objects.get_or_create(name=file["course"].strip(), space=self.request.space) 

40 if created: 

41 keyword.move(course_keyword, pos="last-child") 

42 recipe.keywords.add(keyword) 

43 

44 for tag in file["tags"]: 

45 keyword, created = Keyword.objects.get_or_create(name=tag.strip(), space=self.request.space) 

46 recipe.keywords.add(keyword) 

47 

48 for comment in file['comments']: 

49 Comment.objects.create(recipe=recipe, text=comment['text'], created_by=self.request.user) 

50 CookLog.objects.create(recipe=recipe, rating=comment['rating'], created_by=self.request.user, space=self.request.space) 

51 

52 if file["photo"] != '': 

53 recipe.image = f'recipes/openeats-import/{file["photo"]}' 

54 recipe.save() 

55 

56 step = Step.objects.create(instruction=instructions, space=self.request.space, show_ingredients_table=self.request.user.userpreference.show_step_ingredients,) 

57 

58 ingredient_parser = IngredientParser(self.request, True) 

59 for ingredient in file['ingredients']: 

60 f = ingredient_parser.get_food(ingredient['food']) 

61 u = ingredient_parser.get_unit(ingredient['unit']) 

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

63 food=f, unit=u, amount=ingredient['amount'], space=self.request.space, 

64 )) 

65 recipe.steps.add(step) 

66 

67 return recipe 

68 

69 def split_recipe_file(self, file): 

70 recipe_json = json.loads(file.read()) 

71 recipe_dict = {} 

72 ingredient_group_dict = {} 

73 cuisine_group_dict = {} 

74 course_group_dict = {} 

75 tag_group_dict = {} 

76 

77 for o in recipe_json: 

78 if o['model'] == 'recipe.recipe': 

79 recipe_dict[o['pk']] = { 

80 'name': o['fields']['title'], 

81 'info': o['fields']['info'], 

82 'directions': o['fields']['directions'], 

83 'source': o['fields']['source'], 

84 'prep_time': o['fields']['prep_time'], 

85 'cook_time': o['fields']['cook_time'], 

86 'servings': o['fields']['servings'], 

87 'ingredients': [], 

88 'photo': o['fields']['photo'], 

89 'cuisine': o['fields']['cuisine'], 

90 'course': o['fields']['course'], 

91 'tags': o['fields']['tags'], 

92 'comments': [], 

93 } 

94 if o['model'] == 'ingredient.ingredientgroup': 

95 ingredient_group_dict[o['pk']] = o['fields']['recipe'] 

96 if o['model'] == 'recipe_groups.cuisine': 

97 cuisine_group_dict[o['pk']] = o['fields']['title'] 

98 if o['model'] == 'recipe_groups.course': 

99 course_group_dict[o['pk']] = o['fields']['title'] 

100 if o['model'] == 'recipe_groups.tag': 

101 tag_group_dict[o['pk']] = o['fields']['title'] 

102 

103 for o in recipe_json: 

104 if o['model'] == 'rating.rating': 

105 recipe_dict[o['fields']['recipe']]["comments"].append({ 

106 "text": o['fields']['comment'], 

107 "rating": o['fields']['rating'] 

108 }) 

109 if o['model'] == 'ingredient.ingredient': 

110 ingredient = { 

111 'food': o['fields']['title'], 

112 'unit': o['fields']['measurement'], 

113 'amount': round(o['fields']['numerator'] / o['fields']['denominator'], 2), 

114 } 

115 recipe_dict[ingredient_group_dict[o['fields']['ingredient_group']]]['ingredients'].append(ingredient) 

116 

117 for k, r in recipe_dict.items(): 

118 if r["cuisine"] in cuisine_group_dict: 

119 r["cuisine"] = cuisine_group_dict[r["cuisine"]] 

120 if r["course"] in course_group_dict: 

121 r["course"] = course_group_dict[r["course"]] 

122 for index in range(len(r["tags"])): 

123 if r["tags"][index] in tag_group_dict: 

124 r["tags"][index] = tag_group_dict[r["tags"][index]] 

125 

126 return list(recipe_dict.values()) 

127 

128 def get_file_from_recipe(self, recipe): 

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