Skip to content

Commit

Permalink
Update attachment validation to latest ruamel.yaml version
Browse files Browse the repository at this point in the history
  • Loading branch information
nonprofittechy committed Feb 12, 2024
1 parent 3b5e143 commit 8abbf0e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
37 changes: 5 additions & 32 deletions docassemble/ALDashboard/data/questions/validate_attachment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
include:
- nav.yml
---
modules:
- .validate_attachment
---
question: |
Paste the contents of a long attachment block
(beginning with the line `fields:`)
Expand All @@ -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:
Expand All @@ -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
% endif
22 changes: 22 additions & 0 deletions docassemble/ALDashboard/validate_attachment.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8abbf0e

Please sign in to comment.