Coverage for opt/mealie/lib/python3.12/site-packages/mealie/services/exporter/recipe_exporter.py: 74%

19 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-11-25 15:48 +0000

1from collections.abc import Iterator 1a

2from uuid import UUID 1a

3 

4from mealie.repos.all_repositories import AllRepositories 1a

5from mealie.schema.recipe import Recipe 1a

6 

7from ._abc_exporter import ABCExporter, ExportedItem 1a

8 

9 

10class RecipeExporter(ABCExporter): 1a

11 def __init__(self, db: AllRepositories, group_id: UUID, recipes: list[str]) -> None: 1a

12 """ 

13 RecipeExporter is used to export a list of recipes to a zip file. The zip 

14 file is then saved to a temporary directory and then available for a one-time 

15 download. 

16 

17 Args: 

18 db (Database): 

19 group_id (int): 

20 recipes (list[str]): Recipe Slugs 

21 """ 

22 super().__init__(db, group_id) 

23 self.recipes = recipes 

24 

25 @property 1a

26 def destination_dir(self) -> str: 1a

27 return "recipes" 

28 

29 def items(self) -> Iterator[ExportedItem]: 1a

30 for slug in self.recipes: 

31 yield ExportedItem( 

32 name=slug, 

33 model=self.db.recipes.multi_query({"slug": slug, "group_id": self.group_id}, limit=1)[0], 

34 ) 

35 

36 def _post_export_hook(self, item: Recipe) -> None: 1a

37 """Copy recipe directory contents into the zip folder""" 

38 recipe_dir = item.directory 

39 

40 if recipe_dir.exists() and self.write_dir_to_zip: 

41 self.write_dir_to_zip(recipe_dir, f"{self.destination_dir}/{item.slug}", {".json"})