Coverage for cookbook/integration/rezeptsuitede.py: 20%
54 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 base64
2from io import BytesIO
3from xml import etree
5from cookbook.helper.ingredient_parser import IngredientParser
6from cookbook.helper.recipe_url_import import parse_servings, parse_servings_text
7from cookbook.integration.integration import Integration
8from cookbook.models import Ingredient, Keyword, Recipe, Step
11class Rezeptsuitede(Integration):
13 def split_recipe_file(self, file):
14 return etree.parse(file).getroot().getchildren()
16 def get_recipe_from_file(self, file):
17 recipe_xml = file
19 recipe = Recipe.objects.create(
20 name=recipe_xml.find('head').attrib['title'].strip(),
21 created_by=self.request.user, internal=True, space=self.request.space)
23 try:
24 if recipe_xml.find('head').attrib['servingtype']:
25 recipe.servings = parse_servings(recipe_xml.find('head').attrib['servingtype'].strip())
26 recipe.servings_text = parse_servings_text(recipe_xml.find('head').attrib['servingtype'].strip())
27 except KeyError:
28 pass
30 if recipe_xml.find('remark') is not None: # description is a list of <li>'s with text
31 if recipe_xml.find('remark').find('line') is not None:
32 recipe.description = recipe_xml.find('remark').find('line').text[:512]
34 for prep in recipe_xml.findall('preparation'):
35 try:
36 if prep.find('step').text:
37 step = Step.objects.create(
38 instruction=prep.find('step').text.strip(), space=self.request.space, show_ingredients_table=self.request.user.userpreference.show_step_ingredients,
39 )
40 recipe.steps.add(step)
41 except Exception:
42 pass
44 ingredient_parser = IngredientParser(self.request, True)
46 if recipe_xml.find('part').find('ingredient') is not None:
47 ingredient_step = recipe.steps.first()
48 if ingredient_step is None:
49 ingredient_step = Step.objects.create(space=self.request.space, instruction='')
51 for ingredient in recipe_xml.find('part').findall('ingredient'):
52 f = ingredient_parser.get_food(ingredient.attrib['item'])
53 u = ingredient_parser.get_unit(ingredient.attrib['unit'])
54 amount = 0
55 if ingredient.attrib['qty'].strip() != '':
56 amount, unit, note = ingredient_parser.parse_amount(ingredient.attrib['qty'])
57 ingredient_step.ingredients.add(Ingredient.objects.create(food=f, unit=u, amount=amount, space=self.request.space, ))
59 try:
60 k, created = Keyword.objects.get_or_create(name=recipe_xml.find('head').find('cat').text.strip(), space=self.request.space)
61 recipe.keywords.add(k)
62 except Exception:
63 pass
65 recipe.save()
67 try:
68 self.import_recipe_image(recipe, BytesIO(base64.b64decode(recipe_xml.find('head').find('picbin').text)), filetype='.jpeg')
69 except BaseException:
70 pass
72 return recipe
74 def get_file_from_recipe(self, recipe):
75 raise NotImplementedError('Method not implemented in storage integration')