From 1525ceafd5b581dd594a316b381c98a60d6c682f Mon Sep 17 00:00:00 2001 From: Francesco Siddi Date: Wed, 27 Mar 2019 12:11:57 +0100 Subject: [PATCH] Fix for find_markdown_fields project hook Original commit 3b59d3ee9aacae517b06bf25346efa3f2dae0fe7 Breaking commit 32e25ce129612010a4c14dfee0d21d1a93666108 The breaking commit was actually meant to remove the need for this hook logic entirely, by relying on a custom validator instead. This works for nodes, but it currently does not work for projects. The issue needs to be further investigated via T63006. --- pillar/api/projects/hooks.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/pillar/api/projects/hooks.py b/pillar/api/projects/hooks.py index 4f9eb9d7..d27359ba 100644 --- a/pillar/api/projects/hooks.py +++ b/pillar/api/projects/hooks.py @@ -253,26 +253,30 @@ def parse_markdown(project, original=None): schema = current_app.config['DOMAIN']['projects']['schema'] def find_markdown_fields(schema, project): - """Find and process all makrdown validated fields.""" - for k, v in schema.items(): - if not isinstance(v, dict): + """Find and process all Markdown coerced fields. + + - look for fields with a 'coerce': 'markdown' property + - parse the name of the field and generate the sibling field name (__html -> ) + - parse the content of the field as markdown and save it in __html + """ + for field_name, field_value in schema.items(): + if not isinstance(field_value, dict): + continue + if field_value.get('coerce') != 'markdown': + continue + if field_name not in project: continue - if v.get('validator') == 'markdown': - # If there is a match with the validator: markdown pair, assign the sibling - # property (following the naming convention __html) - # the processed value. - if k in project: - html = pillar.markdown.markdown(project[k]) - field_name = pillar.markdown.cache_field_name(k) - project[field_name] = html - if isinstance(project, dict) and k in project: - find_markdown_fields(v, project[k]) + # Construct markdown source field name (strip the leading '_' and the trailing '_html') + source_field_name = field_name[1:-5] + html = pillar.markdown.markdown(project[source_field_name]) + project[field_name] = html + + if isinstance(project, dict) and field_name in project: + find_markdown_fields(field_value, project[field_name]) find_markdown_fields(schema, project) - return 'ok' - def parse_markdowns(items): for item in items: