Coverage for cookbook/integration/cookmate.py: 21%
56 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 io import BytesIO
3import requests
4import validators
6from cookbook.helper.ingredient_parser import IngredientParser
7from cookbook.helper.recipe_url_import import parse_servings, parse_servings_text, parse_time
8from cookbook.integration.integration import Integration
9from cookbook.models import Ingredient, Recipe, Step
12class Cookmate(Integration):
14 def import_file_name_filter(self, zip_info_object):
15 return zip_info_object.filename.endswith('.xml')
17 def get_files_from_recipes(self, recipes, el, cookie):
18 raise NotImplementedError('Method not implemented in storage integration')
20 def get_recipe_from_file(self, file):
21 recipe_xml = file
23 recipe = Recipe.objects.create(
24 name=recipe_xml.find('title').text.strip(),
25 created_by=self.request.user, internal=True, space=self.request.space)
27 if recipe_xml.find('preptime') is not None and recipe_xml.find('preptime').text is not None:
28 recipe.working_time = parse_time(recipe_xml.find('preptime').text.strip())
30 if recipe_xml.find('cooktime') is not None and recipe_xml.find('cooktime').text is not None:
31 recipe.waiting_time = parse_time(recipe_xml.find('cooktime').text.strip())
33 if recipe_xml.find('quantity') is not None and recipe_xml.find('quantity').text is not None:
34 recipe.servings = parse_servings(recipe_xml.find('quantity').text.strip())
35 recipe.servings_text = parse_servings_text(recipe_xml.find('quantity').text.strip())
37 if recipe_xml.find('url') is not None and recipe_xml.find('url').text is not None:
38 recipe.source_url = recipe_xml.find('url').text.strip()
40 if recipe_xml.find('description') is not None: # description is a list of <li>'s with text
41 if len(recipe_xml.find('description')) > 0:
42 recipe.description = recipe_xml.find('description')[0].text[:512]
44 if recipe_text := recipe_xml.find('recipetext'):
45 for step in recipe_text.getchildren():
46 if step.text:
47 step = Step.objects.create(
48 instruction=step.text.strip(), space=self.request.space, show_ingredients_table=self.request.user.userpreference.show_step_ingredients,
49 )
50 recipe.steps.add(step)
52 ingredient_parser = IngredientParser(self.request, True)
54 if recipe_ingredients := recipe_xml.find('ingredient'):
55 ingredient_step = recipe.steps.first()
56 if ingredient_step is None:
57 ingredient_step = Step.objects.create(space=self.request.space, instruction='')
59 for ingredient in recipe_ingredients.getchildren():
60 if ingredient.text:
61 if ingredient.text.strip() != '':
62 amount, unit, food, note = ingredient_parser.parse(ingredient.text.strip())
63 f = ingredient_parser.get_food(food)
64 u = ingredient_parser.get_unit(unit)
65 ingredient_step.ingredients.add(Ingredient.objects.create(
66 food=f, unit=u, amount=amount, note=note, original_text=ingredient.text.strip(), space=self.request.space,
67 ))
69 if recipe_xml.find('imageurl') is not None:
70 try:
71 url = recipe_xml.find('imageurl').text.strip()
72 if validators.url(url, public=True):
73 response = requests.get(url)
74 self.import_recipe_image(recipe, BytesIO(response.content))
75 except Exception as e:
76 print('failed to import image ', str(e))
78 recipe.save()
80 return recipe
82 def get_file_from_recipe(self, recipe):
83 raise NotImplementedError('Method not implemented in storage integration')