Coverage for opt/mealie/lib/python3.12/site-packages/mealie/db/models/recipe/api_extras.py: 94%

31 statements  

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

1import sqlalchemy as sa 1a

2from sqlalchemy.orm import Mapped, mapped_column 1a

3 

4from mealie.db.models._model_base import SqlAlchemyBase 1a

5from mealie.db.models._model_utils.guid import GUID 1a

6 

7 

8def api_extras(func): 1a

9 """Decorator function to unpack the extras into a dict; requires an "extras" column""" 

10 

11 def wrapper(*args, **kwargs): 1a

12 extras = kwargs.pop("extras") 1klmnbcdoefghpiqrj

13 

14 if extras is None: 14 ↛ 15line 14 didn't jump to line 15 because the condition on line 14 was never true1klmnbcdoefghpiqrj

15 extras = [] 

16 else: 

17 extras = [{"key": key, "value": value} for key, value in extras.items()] 1klmnbcdoefghpiqrj

18 

19 return func(*args, extras=extras, **kwargs) 1klmnbcdoefghpiqrj

20 

21 return wrapper 1a

22 

23 

24class ExtrasGeneric: 1a

25 """ 

26 Template for API extensions 

27 

28 This class is not an actual table, so it does not inherit from SqlAlchemyBase 

29 """ 

30 

31 id: Mapped[int] = mapped_column(sa.Integer, primary_key=True) 1a

32 key_name: Mapped[str | None] = mapped_column(sa.String) 1a

33 value: Mapped[str | None] = mapped_column(sa.String) 1a

34 

35 def __init__(self, key, value) -> None: 1a

36 self.key_name = key 1bcdefghij

37 self.value = value 1bcdefghij

38 

39 

40# used specifically for recipe extras 

41class ApiExtras(ExtrasGeneric, SqlAlchemyBase): 1a

42 __tablename__ = "api_extras" 1a

43 recipee_id: Mapped[GUID | None] = mapped_column(GUID, sa.ForeignKey("recipes.id"), index=True) 1a

44 

45 

46class IngredientFoodExtras(ExtrasGeneric, SqlAlchemyBase): 1a

47 __tablename__ = "ingredient_food_extras" 1a

48 ingredient_food_id: Mapped[GUID | None] = mapped_column(GUID, sa.ForeignKey("ingredient_foods.id"), index=True) 1a

49 

50 

51class ShoppingListExtras(ExtrasGeneric, SqlAlchemyBase): 1a

52 __tablename__ = "shopping_list_extras" 1a

53 shopping_list_id: Mapped[GUID | None] = mapped_column(GUID, sa.ForeignKey("shopping_lists.id"), index=True) 1a

54 

55 

56class ShoppingListItemExtras(ExtrasGeneric, SqlAlchemyBase): 1a

57 __tablename__ = "shopping_list_item_extras" 1a

58 shopping_list_item_id: Mapped[GUID | None] = mapped_column( 1a

59 GUID, sa.ForeignKey("shopping_list_items.id"), index=True 

60 )