support or in ingredients - ingredient alternatives

This commit is contained in:
Emi Vasilek 2023-11-05 11:47:25 +01:00
parent 87884d9ab3
commit 3d3f0b0012
2 changed files with 31 additions and 7 deletions

View file

@ -137,12 +137,16 @@ class PriceDB:
class IngredientInstance: 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.ctx = ctx
self.defaultamount = float(defaultamount)
self.defaultunit: Optional[Unit] = defaultunit
def load(self, dct: Dict[str, Any]) -> List[str]: def load(self, dct: Dict[str, Any]) -> List[str]:
issues = [] issues = []
issues += assert_dict(dct, ["name"], ["amount", "unit", "note"]) issues += assert_dict(dct, ["name"], ["amount", "unit", "note", "or"])
assert_type(dct, "name", str) assert_type(dct, "name", str)
self.name = dct["name"] self.name = dct["name"]
@ -152,7 +156,7 @@ class IngredientInstance:
except RuntimeError as e: except RuntimeError as e:
issues.append(str(e)) issues.append(str(e))
self.amount = 1.0 self.amount = self.defaultamount
if "amount" in dct: if "amount" in dct:
if isinstance(dct["amount"], float): if isinstance(dct["amount"], float):
self.amount = dct["amount"] self.amount = dct["amount"]
@ -161,19 +165,28 @@ class IngredientInstance:
else: else:
raise RuntimeError(f"{dct['amount']} has to be int or float") raise RuntimeError(f"{dct['amount']} has to be int or float")
self.unit = None self.unit = self.defaultunit
if "unit" in dct: if "unit" in dct:
assert_type(dct, "unit", str) assert_type(dct, "unit", str)
try: try:
self.unit = self.ctx.units.get(dct["unit"]) self.unit = self.ctx.units.get(dct["unit"])
except RuntimeError as e: except RuntimeError as e:
issues.append(str(e)) issues.append(str(e))
self.note = "" self.note = ""
if "note" in dct: if "note" in dct:
assert_type(dct, "note", str) assert_type(dct, "note", str)
self.note = dct["note"] 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 return issues

View file

@ -1,3 +1,14 @@
{% 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 %}
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
@ -12,7 +23,7 @@
<h2>{{part.title}}</h2> <h2>{{part.title}}</h2>
<h3>Ingredients</h3> <h3>Ingredients</h3>
{% for ing in part.ingredients %} {% for ing in part.ingredients %}
<li>{{ing.amount}} {{ing.unit.name}} {{ ing.name }}</li> <li>{{ingredient(ing)}}</li>
{% endfor %} {% endfor %}
<h3>Steps</h3> <h3>Steps</h3>
{% for step in part.steps %} {% for step in part.steps %}