Coverage for cookbook/integration/cheftap.py: 20%
41 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
1import re
3from cookbook.helper.ingredient_parser import IngredientParser
4from cookbook.integration.integration import Integration
5from cookbook.models import Ingredient, Recipe, Step
8class ChefTap(Integration):
10 def import_file_name_filter(self, zip_info_object):
11 print("testing", zip_info_object.filename)
12 return re.match(r'^cheftap_export/([A-Za-z\d\s\-_()\[\]\u00C0-\u017F])+.txt$', zip_info_object.filename) or re.match(r'^([A-Za-z\d\s\-_()\[\]\u00C0-\u017F])+.txt$', zip_info_object.filename)
14 def get_recipe_from_file(self, file):
15 source_url = ''
17 ingredient_mode = 0
19 ingredients = []
20 directions = []
21 for i, fl in enumerate(file.readlines(), start=0):
22 line = fl.decode("utf-8")
23 if i == 0:
24 title = line.strip()
25 else:
26 if line.startswith('https:') or line.startswith('http:'):
27 source_url = line.strip()
28 else:
29 if ingredient_mode == 1 and len(line.strip()) == 0:
30 ingredient_mode = 2
31 if re.match(r'^([0-9])[^.](.)*$', line) and ingredient_mode < 2:
32 ingredient_mode = 1
33 ingredients.append(line.strip())
34 else:
35 directions.append(line.strip())
37 recipe = Recipe.objects.create(name=title, created_by=self.request.user, internal=True, space=self.request.space, )
39 step = Step.objects.create(instruction='\n'.join(directions), space=self.request.space, show_ingredients_table=self.request.user.userpreference.show_step_ingredients,)
41 if source_url != '':
42 step.instruction += '\n' + source_url
43 step.save()
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(food)
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)
56 return recipe
58 def get_file_from_recipe(self, recipe):
59 raise NotImplementedError('Method not implemented in storage integration')