step sections

This commit is contained in:
Emi Vasilek 2023-11-21 01:43:22 +01:00
parent 98c77cb102
commit e4890b6fd8
3 changed files with 48 additions and 11 deletions

View file

@ -205,6 +205,14 @@ class IngredientInstance(Element):
price=price, price=price,
) )
class StepSection(Element):
def __init__(self, ctx: Context, title: str, steps: List[str]) -> None:
self.title = title
self.steps = steps
@classmethod
def from_dict(cls, ctx: Context, dct: Dict[str, Any]) -> Self:
return cls(ctx, dct["section"], dct["steps"])
class Recipe(Element): class Recipe(Element):
def __init__( def __init__(
@ -214,7 +222,7 @@ class Recipe(Element):
ingredients: List[IngredientInstance], ingredients: List[IngredientInstance],
subrecipes: List["Recipe"], subrecipes: List["Recipe"],
price: Optional["PriceDB"], price: Optional["PriceDB"],
steps: List[str], stepsections: List[StepSection],
) -> None: ) -> None:
super().__init__(ctx) super().__init__(ctx)
self.srcpath = "" self.srcpath = ""
@ -223,7 +231,7 @@ class Recipe(Element):
self.ingredients = ingredients self.ingredients = ingredients
self.subrecipes = subrecipes self.subrecipes = subrecipes
self.price = price self.price = price
self.steps = steps self.stepsections = stepsections
@classmethod @classmethod
def from_dict(cls, ctx: Context, dct: Dict[str, Any]) -> Self: def from_dict(cls, ctx: Context, dct: Dict[str, Any]) -> Self:
@ -268,16 +276,24 @@ class Recipe(Element):
currency=currency, currency=currency,
) )
steps = [] stepsections: List[StepSection] = []
if "steps" in dct: if "steps" in dct:
steps = dct["steps"] defaultstepsection = StepSection(ctx, "default", [])
stepsections = [defaultstepsection]
for step in dct["steps"]:
if isinstance(step, str):
defaultstepsection.steps.append(step)
if isinstance(step, dict):
section = StepSection.from_dict(ctx, step)
stepsections.append(section)
return cls( return cls(
ctx=ctx, ctx=ctx,
title=dct["title"], title=dct["title"],
ingredients=ingredients, ingredients=ingredients,
subrecipes=subrecipes, subrecipes=subrecipes,
price=price, price=price,
steps=steps, stepsections=stepsections,
) )
class Conversion(Element): class Conversion(Element):

View file

@ -24,9 +24,25 @@
} }
} }
}, },
"steps": {
"type": "array",
"items": {
"oneOf": [
{ "type": "string" },
{
"type": "object",
"additionalProperties": false,
"required": [ "section" ],
"properties": {
"section": { "type": "string" },
"steps": { "steps": {
"type": "array", "type": "array",
"items": {"type": "string"} "items": {"type": "string"}
}
}
}
]
}
}, },
"subrecipes": { "subrecipes": {
"type": "array", "type": "array",

View file

@ -24,11 +24,16 @@
{% endfor %} {% endfor %}
{% if rec.price != None %}price: {{price(rec.price)}}{%endif%} {% if rec.price != None %}price: {{price(rec.price)}}{%endif%}
{% endif %} {% endif %}
{% if rec.steps|length != 0 %} {% if rec.stepsections|length != 0 %}
<h3>Steps</h3> <h3>Steps</h3>
{% for step in rec.steps %} {% for stepsection in rec.stepsections %}
{% if stepsection.title != "default" %}
<h4>{{stepsection.title}}</h4>
{% endif %}
{% for step in stepsection.steps %}
<li>{{ step }}</li> <li>{{ step }}</li>
{% endfor %} {% endfor %}
{% endfor %}
{% endif %} {% endif %}
<hr> <hr>
{%- endmacro %} {%- endmacro %}