Coverage for cookbook/integration/saffron.py: 10%

78 statements  

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

1from django.utils.translation import gettext as _ 

2 

3from cookbook.helper.ingredient_parser import IngredientParser 

4from cookbook.integration.integration import Integration 

5from cookbook.models import Ingredient, Recipe, Step 

6 

7 

8class Saffron(Integration): 

9 

10 def get_recipe_from_file(self, file): 

11 ingredient_mode = False 

12 direction_mode = False 

13 

14 ingredients = [] 

15 directions = [] 

16 for fl in file.readlines(): 

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

18 if 'Title:' in line: 

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

20 if 'Description:' in line: 

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

22 if 'Yield:' in line: 

23 directions.append(_('Servings') + ' ' + line.replace('Yield:', '').strip() + '\n') 

24 if 'Cook:' in line: 

25 directions.append(_('Waiting time') + ' ' + line.replace('Cook:', '').strip() + '\n') 

26 if 'Prep:' in line: 

27 directions.append(_('Preparation Time') + ' ' + line.replace('Prep:', '').strip() + '\n') 

28 if 'Cookbook:' in line: 

29 directions.append(_('Cookbook') + ' ' + line.replace('Cookbook:', '').strip() + '\n') 

30 if 'Section:' in line: 

31 directions.append(_('Section') + ' ' + line.replace('Section:', '').strip() + '\n') 

32 if ingredient_mode: 

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

34 ingredients.append(line.strip()) 

35 if direction_mode: 

36 if len(line) > 2: 

37 directions.append(line.strip()) 

38 if 'Ingredients:' in line: 

39 ingredient_mode = True 

40 if 'Instructions:' in line: 

41 ingredient_mode = False 

42 direction_mode = True 

43 

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

45 

46 step = Step.objects.create(instruction='\n'.join(directions), space=self.request.space, show_ingredients_table=self.request.user.userpreference.show_step_ingredients, ) 

47 

48 ingredient_parser = IngredientParser(self.request, True) 

49 for ingredient in ingredients: 

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

51 f = ingredient_parser.get_food(food) 

52 u = ingredient_parser.get_unit(unit) 

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

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

55 )) 

56 recipe.steps.add(step) 

57 

58 return recipe 

59 

60 def get_file_from_recipe(self, recipe): 

61 

62 data = "Title: " + recipe.name if recipe.name else "" + "\n" 

63 data += "Description: " + recipe.description if recipe.description else "" + "\n" 

64 data += "Source: \n" 

65 data += "Original URL: \n" 

66 data += "Yield: " + str(recipe.servings) + "\n" 

67 data += "Cookbook: \n" 

68 data += "Section: \n" 

69 data += "Image: \n" 

70 

71 recipeInstructions = [] 

72 recipeIngredient = [] 

73 for s in recipe.steps.all(): 

74 recipeInstructions.append(s.instruction) 

75 

76 for i in s.ingredients.all(): 

77 recipeIngredient.append(f'{float(i.amount)} {i.unit} {i.food}') 

78 

79 data += "Ingredients: \n" 

80 for ingredient in recipeIngredient: 

81 data += ingredient + "\n" 

82 

83 data += "Instructions: \n" 

84 for instruction in recipeInstructions: 

85 data += instruction + "\n" 

86 

87 return recipe.name + '.txt', data 

88 

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

90 files = [] 

91 for r in recipes: 

92 filename, data = self.get_file_from_recipe(r) 

93 files.append([filename, data]) 

94 

95 el.exported_recipes += 1 

96 el.msg += self.get_recipe_processed_msg(r) 

97 el.save() 

98 

99 return files