Coverage for cookbook/integration/rezkonv.py: 12%
60 statements
« prev ^ index » next coverage.py v7.4.0, created at 2023-12-29 00:47 +0100
« 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, Keyword, Recipe, Step
6class RezKonv(Integration):
8 def get_recipe_from_file(self, file):
10 ingredient_mode = False
11 direction_mode = False
13 ingredients = []
14 directions = []
15 for line in file.replace('\r', '').replace('\n\n', '\n').split('\n'):
16 if 'Titel:' in line:
17 title = line.replace('Titel:', '').strip()
18 if 'Kategorien:' in line:
19 tags = line.replace('Kategorien:', '').strip()
20 if ingredient_mode and (
21 'quelle' in line.lower() or 'source' in line.lower() or (line == '' and len(ingredients) > 0)):
22 ingredient_mode = False
23 direction_mode = True
24 if ingredient_mode:
25 if line != '' and '===' not in line and 'Zubereitung' not in line:
26 ingredients.append(line.strip())
27 if direction_mode:
28 if line.strip() != '' and line.strip() != '=====':
29 directions.append(line.strip())
30 if 'Zutaten:' in line or 'Ingredients' in line or 'Menge:' in line:
31 ingredient_mode = True
33 recipe = Recipe.objects.create(name=title, created_by=self.request.user, internal=True,
34 space=self.request.space)
36 for k in tags.split(','):
37 keyword, created = Keyword.objects.get_or_create(name=k.strip(), space=self.request.space)
38 recipe.keywords.add(keyword)
40 step = Step.objects.create(
41 instruction=' \n'.join(directions) + '\n\n', space=self.request.space, show_ingredients_table=self.request.user.userpreference.show_step_ingredients,
42 )
44 ingredient_parser = IngredientParser(self.request, True)
45 for ingredient in ingredients:
46 if len(ingredient.strip()) > 0:
47 amount, unit, food, note = ingredient_parser.parse(ingredient)
48 f = ingredient_parser.get_food(food)
49 u = ingredient_parser.get_unit(unit)
50 step.ingredients.add(Ingredient.objects.create(
51 food=f, unit=u, amount=amount, note=note, original_text=ingredient, space=self.request.space,
52 ))
53 recipe.steps.add(step)
55 return recipe
57 def get_file_from_recipe(self, recipe):
58 raise NotImplementedError('Method not implemented in storage integration')
60 def split_recipe_file(self, file):
61 recipe_list = []
62 current_recipe = ''
63 # TODO build algorithm to try trough encodings and fail if none work, use for all importers
64 # encoding_list = ['windows-1250', 'latin-1']
65 encoding = 'windows-1250'
66 for fl in file.readlines():
67 try:
68 line = fl.decode(encoding)
69 except UnicodeDecodeError:
70 encoding = 'latin-1'
71 line = fl.decode(encoding)
72 if line.startswith('=====') and 'rezkonv' in line.lower():
73 if current_recipe != '':
74 recipe_list.append(current_recipe)
75 current_recipe = ''
76 else:
77 current_recipe = ''
78 else:
79 current_recipe += line + '\n'
81 if current_recipe != '':
82 recipe_list.append(current_recipe)
84 return recipe_list