Coverage for cookbook/integration/pepperplate.py: 14%

42 statements  

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

1from cookbook.helper.ingredient_parser import IngredientParser 

2from cookbook.integration.integration import Integration 

3from cookbook.models import Ingredient, Recipe, Step 

4 

5 

6class Pepperplate(Integration): 

7 

8 def get_recipe_from_file(self, file): 

9 ingredient_mode = False 

10 direction_mode = False 

11 

12 ingredients = [] 

13 directions = [] 

14 for fl in file.readlines(): 

15 line = fl.decode("utf-8") 

16 if 'Title:' in line: 

17 title = line.replace('Title:', '').replace('"', '').strip() 

18 if 'Description:' in line: 

19 description = line.replace('Description:', '').strip() 

20 if 'Original URL:' in line or 'Source:' in line or 'Yield:' in line or 'Total:' in line: 

21 if len(line.strip().split(':')[1]) > 0: 

22 directions.append(line.strip() + '\n') 

23 if ingredient_mode: 

24 if len(line) > 2 and 'Instructions:' not in line: 

25 ingredients.append(line.strip()) 

26 if direction_mode: 

27 if len(line) > 2: 

28 directions.append(line.strip() + '\n') 

29 if 'Ingredients:' in line: 

30 ingredient_mode = True 

31 if 'Instructions:' in line: 

32 ingredient_mode = False 

33 direction_mode = True 

34 

35 recipe = Recipe.objects.create(name=title, description=description, created_by=self.request.user, internal=True, space=self.request.space) 

36 

37 step = Step.objects.create( 

38 instruction='\n'.join(directions) + '\n\n', space=self.request.space, show_ingredients_table=self.request.user.userpreference.show_step_ingredients, 

39 ) 

40 

41 ingredient_parser = IngredientParser(self.request, True) 

42 for ingredient in ingredients: 

43 if len(ingredient.strip()) > 0: 

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

45 f = ingredient_parser.get_food(food) 

46 u = ingredient_parser.get_unit(unit) 

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

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

49 )) 

50 recipe.steps.add(step) 

51 

52 return recipe 

53 

54 def get_file_from_recipe(self, recipe): 

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