workflows: record an error when using a RTF model with disabled support (#88124) #1272

Merged
fpeters merged 1 commits from wip/88124-export-to-model-error-on-rtf into main 2024-03-15 14:26:29 +01:00
2 changed files with 51 additions and 14 deletions

View File

@ -36,7 +36,7 @@ from wcs.qommon import force_str
from wcs.qommon.form import UploadedFile
from wcs.qommon.http_request import HTTPRequest
from wcs.qommon.upload_storage import PicklableUpload
from wcs.wf.export_to_model import ExportToModel, UploadValidationError, transform_to_pdf
from wcs.wf.export_to_model import ExportToModel, transform_to_pdf
from wcs.workflows import Workflow, WorkflowBackofficeFieldsFormDef
from ..admin_pages.test_all import create_superuser
@ -320,6 +320,8 @@ def test_export_to_model_xml(pub):
item.attach_to_history = True
def run(template, filename='/foo/template.xml', content_type='application/xml'):
formdata.evolution[-1].parts = None
formdata.store()
pub.loggederror_class.wipe()
upload = QuixoteUpload(filename, content_type=content_type)
upload.fp = io.BytesIO()
@ -330,8 +332,9 @@ def test_export_to_model_xml(pub):
pub.substitutions.reset()
pub.substitutions.feed(formdata)
item.perform(formdata)
with open(formdata.evolution[0].parts[-1].get_file_path()) as fd:
return fd.read()
if formdata.evolution[0].parts:
with open(formdata.evolution[0].parts[-1].get_file_path()) as fd:
return fd.read()
# good XML
assert run(template='<a>{{ form_var_string }}</a>') == '<a>écho</a>'
@ -341,23 +344,53 @@ def test_export_to_model_xml(pub):
assert run(template='<a>{{ form_var_string }}</a>', filename='/foo/template.svg') == '<a>écho</a>'
# unknown file format
with pytest.raises(UploadValidationError) as e:
run(
template='<a>{{ form_var_string }}</a>',
filename='/foo/template.txt',
content_type='application/octet-stream',
)
assert str(e.value) == 'Only OpenDocument and XML files can be used.'
assert not run(
template='<a>{{ form_var_string }}</a>',
filename='/foo/template.txt',
content_type='application/octet-stream',
)
assert pub.loggederror_class.count() == 1
assert pub.loggederror_class.select()[0].summary == 'Only OpenDocument and XML files can be used.'
# invalid UTF-8
with pytest.raises(UploadValidationError) as e:
assert run(template=b'<name>test \xE0 {{form_var_string}}</name>') == ''
assert str(e.value) == 'XML model files must be UTF-8.'
assert not run(template=b'<name>test \xE0 {{form_var_string}}</name>')
assert pub.loggederror_class.count() == 1
assert pub.loggederror_class.select()[0].summary == 'XML model files must be UTF-8.'
# malformed XML
assert run(template='<a>{{ form_var_string }}<a>') == '<a>écho<a>'
# on error in the XML correctness no exception is raised but an error is logged
assert pub.loggederror_class.count() == 1
assert pub.loggederror_class.select()[0].summary == 'The rendered template is not a valid XML document.'
def test_export_to_model_disabled_rtf(pub):
formdef = FormDef()
formdef.name = 'foo-export-to-template-with-django'
formdef.fields = [
StringField(id='1', label='String', varname='string'),
]
formdef.store()
formdata = formdef.data_class()()
formdata.just_created()
formdata.store()
item = ExportToModel()
item.method = 'non-interactive'
item.attach_to_history = True
upload = QuixoteUpload('test.rtf', content_type='application/rtf')
upload.fp = io.BytesIO()
upload.fp.write(b'{\\rtf...')
upload.fp.seek(0)
item.model_file = UploadedFile(pub.app_dir, None, upload)
item.convert_to_pdf = False
pub.substitutions.reset()
pub.substitutions.feed(formdata)
pub.loggederror_class.wipe()
item.perform(formdata)
assert pub.loggederror_class.count() == 1
assert pub.loggederror_class.select()[0].summary == 'Only OpenDocument and XML files can be used.'
@pytest.mark.parametrize('filename', ['template-form-details.odt', 'template-form-details-no-styles.odt'])

View File

@ -938,7 +938,11 @@ class ExportToModel(WorkflowStatusItem):
model_file = self.get_model_file()
if not model_file:
return
outstream = self.apply_template_to_formdata(formdata, model_file)
try:
outstream = self.apply_template_to_formdata(formdata, model_file)
except UploadValidationError as e:
get_publisher().record_error(str(e), formdata=formdata, exception=e)
return
filename = self.get_filename(model_file)
content_type = model_file.content_type
if self.convert_to_pdf: