Coverage for cookbook/integration/mealmaster.py: 15%

54 statements  

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

1import re 

2 

3from cookbook.helper.ingredient_parser import IngredientParser 

4from cookbook.integration.integration import Integration 

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

6 

7 

8class MealMaster(Integration): 

9 

10 def get_recipe_from_file(self, file): 

11 servings = 1 

12 ingredients = [] 

13 directions = [] 

14 for line in file.replace('\r', '').split('\n'): 

15 if not line.startswith('MMMMM') and line.strip != '': 

16 if 'Title:' in line: 

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

18 else: 

19 if 'Categories:' in line: 

20 tags = line.replace('Categories:', '').strip() 

21 else: 

22 if 'Yield:' in line: 

23 servings_text = line.replace('Yield:', '').strip() 

24 else: 

25 if re.match('\s{2,}([0-9])+', line): 

26 ingredients.append(line.strip()) 

27 else: 

28 directions.append(line.strip()) 

29 

30 try: 

31 servings = re.findall('([0-9])+', servings_text)[0] 

32 except Exception as e: 

33 print('failed parsing servings ', e) 

34 

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

36 

37 for k in tags.split(','): 

38 keyword, created = Keyword.objects.get_or_create(name=k.strip(), space=self.request.space) 

39 recipe.keywords.add(keyword) 

40 

41 step = Step.objects.create( 

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

43 ) 

44 

45 ingredient_parser = IngredientParser(self.request, True) 

46 for ingredient in ingredients: 

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

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

49 f = ingredient_parser.get_food(ingredient) 

50 u = ingredient_parser.get_unit(unit) 

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

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

53 )) 

54 recipe.steps.add(step) 

55 

56 return recipe 

57 

58 def get_file_from_recipe(self, recipe): 

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

60 

61 def split_recipe_file(self, file): 

62 recipe_list = [] 

63 current_recipe = '' 

64 

65 for fl in file.readlines(): 

66 line = fl.decode("windows-1250") 

67 if (line.startswith('MMMMM') or line.startswith('-----')) and 'meal-master' in line.lower(): 

68 if current_recipe != '': 

69 recipe_list.append(current_recipe) 

70 current_recipe = '' 

71 else: 

72 current_recipe = '' 

73 else: 

74 current_recipe += line + '\n' 

75 

76 if current_recipe != '': 

77 recipe_list.append(current_recipe) 

78 

79 return recipe_list