diff --git a/docassemble/ALDashboard/data/questions/validate_attachment.yml b/docassemble/ALDashboard/data/questions/validate_attachment.yml index f4046f5..23067ab 100644 --- a/docassemble/ALDashboard/data/questions/validate_attachment.yml +++ b/docassemble/ALDashboard/data/questions/validate_attachment.yml @@ -2,6 +2,9 @@ include: - nav.yml --- +modules: + - .validate_attachment +--- question: | Paste the contents of a long attachment block (beginning with the line `fields:`) @@ -14,30 +17,7 @@ fields: rows: 10 --- code: | - import ruamel.yaml as yaml - parsed_blocks = yaml.load(fields_statement) ---- -#mandatory: True -question: | - ${ parsed_blocks['fields'] } ---- -code: | - import mako.template - import mako.runtime - import re - - mako.runtime.UNDEFINED = DAEmpty() - from mako import exceptions - errors = [] - empty_rows = [] - for index, row in enumerate(parsed_blocks['fields']): - try: - mytemplate = mako.template.Template(next(iter(row.values()))) - content = mytemplate.render() - except: - # Row in XLSX file is 1 indexed, and it has a header row - errors.append((f"Error on row {index}, id: {row}",exceptions.text_error_template().render())) - del mytemplate + errors = validate_attachment_block(fields_statement) load_all_errors = True --- need: @@ -58,11 +38,4 @@ subquestion: | ``` % endfor - % endif - - % if len(empty_rows): - #### ${ len(empty_rows) } untranslated strings found - - Rows ${ comma_and_list(empty_rows) } have untranslated text. - % endif - \ No newline at end of file + % endif \ No newline at end of file diff --git a/docassemble/ALDashboard/validate_attachment.py b/docassemble/ALDashboard/validate_attachment.py new file mode 100644 index 0000000..96d0bb9 --- /dev/null +++ b/docassemble/ALDashboard/validate_attachment.py @@ -0,0 +1,22 @@ +from docassemble.base.util import DAEmpty +import ruamel.yaml +import mako.template +import mako.runtime +mako.runtime.UNDEFINED = DAEmpty() +from mako import exceptions +from typing import List, Tuple + +__all__ = ['validate_attachment_block'] + +def validate_attachment_block(fields_statement: str) -> List[Tuple[str,str]]: + yaml = ruamel.yaml.YAML(typ='rt') + parsed_blocks = yaml.load(fields_statement) + + errors = [] + for index, row in enumerate(parsed_blocks['fields']): + try: + mytemplate = mako.template.Template(next(iter(row.values()))) + content = mytemplate.render() + except: + errors.append((f"Error on row {index}, id: {row}",exceptions.text_error_template().render())) + return errors \ No newline at end of file