diff --git a/recipes.py b/recipes.py index 0e1224f..6b07350 100644 --- a/recipes.py +++ b/recipes.py @@ -137,12 +137,16 @@ class PriceDB: class IngredientInstance: - def __init__(self, ctx: Context) -> None: + def __init__( + self, ctx: Context, defaultamount: float = 1, defaultunit: Optional[Unit] = None + ) -> None: self.ctx = ctx + self.defaultamount = float(defaultamount) + self.defaultunit: Optional[Unit] = defaultunit def load(self, dct: Dict[str, Any]) -> List[str]: issues = [] - issues += assert_dict(dct, ["name"], ["amount", "unit", "note"]) + issues += assert_dict(dct, ["name"], ["amount", "unit", "note", "or"]) assert_type(dct, "name", str) self.name = dct["name"] @@ -152,7 +156,7 @@ class IngredientInstance: except RuntimeError as e: issues.append(str(e)) - self.amount = 1.0 + self.amount = self.defaultamount if "amount" in dct: if isinstance(dct["amount"], float): self.amount = dct["amount"] @@ -161,19 +165,28 @@ class IngredientInstance: else: raise RuntimeError(f"{dct['amount']} has to be int or float") - self.unit = None + self.unit = self.defaultunit if "unit" in dct: assert_type(dct, "unit", str) try: self.unit = self.ctx.units.get(dct["unit"]) except RuntimeError as e: issues.append(str(e)) - self.note = "" if "note" in dct: assert_type(dct, "note", str) self.note = dct["note"] + self.alternatives = [] + if "or" in dct: + assert_list(dct["or"]) + for ingdct in dct["or"]: + ingredient = IngredientInstance( + self.ctx, defaultamount=self.amount, defaultunit=self.unit + ) + ingredient.load(ingdct) + self.alternatives.append(ingredient) + return issues diff --git a/templates/recipe.html b/templates/recipe.html index bebd15c..7cbc227 100644 --- a/templates/recipe.html +++ b/templates/recipe.html @@ -1,9 +1,20 @@ +{% macro ingredientpart(ing) -%} + {{ing.amount}} {{ing.unit.name}} {{ ing.name }} +{%- endmacro %} +{% macro ingredient(ing) -%} + {{ingredientpart(ing)}} + {% if ing.alternatives|length != 0 %} + {% for alting in ing.alternatives %} + or {{ingredientpart(alting)}} + {% endfor %} + {% endif %} +{%- endmacro %}