This commit is contained in:
vabene1111
2021-01-13 01:08:51 +01:00
parent 983d40f2c1
commit 0eebd438ca
8 changed files with 120 additions and 21 deletions

View File

@@ -16,7 +16,7 @@ class IngredientObject(object):
if ingredient.no_amount:
self.amount = ""
else:
self.amount = f'[[calculateAmount({ingredient.amount})]]'
self.amount = f"<scalable-number v-bind:number='{ingredient.amount}' v-bind:factor='servings'></scalable-number>"
if ingredient.unit:
self.unit = ingredient.unit
else:
@@ -32,17 +32,7 @@ class IngredientObject(object):
def render_instructions(step): # TODO deduplicate markdown cleanup code
ingredients = []
for i in step.ingredients.all():
ingredients.append(IngredientObject(i))
try:
template = Template(step.instruction)
instructions = template.render(ingredients=ingredients)
except TemplateSyntaxError:
instructions = step.instruction
instructions = step.instruction
tags = markdown_tags + [
'pre', 'table', 'td', 'tr', 'th', 'tbody', 'style', 'thead'
@@ -56,4 +46,17 @@ def render_instructions(step): # TODO deduplicate markdown cleanup code
)
markdown_attrs['*'] = markdown_attrs['*'] + ['class']
return bleach.clean(parsed_md, tags, markdown_attrs)
instructions = bleach.clean(parsed_md, tags, markdown_attrs)
ingredients = []
for i in step.ingredients.all():
ingredients.append(IngredientObject(i))
try:
template = Template(instructions)
instructions = template.render(ingredients=ingredients)
except TemplateSyntaxError:
pass
return instructions

View File

@@ -158,11 +158,15 @@ class IngredientSerializer(WritableNestedModelSerializer):
class StepSerializer(WritableNestedModelSerializer):
ingredients = IngredientSerializer(many=True)
ingredients_markdown = serializers.SerializerMethodField('get_ingredients_markdown')
def get_ingredients_markdown(self, obj):
return obj.get_instruction_render()
class Meta:
model = Step
fields = (
'id', 'name', 'type', 'instruction', 'ingredients',
'id', 'name', 'type', 'instruction', 'ingredients', 'ingredients_markdown',
'time', 'order', 'show_as_header'
)