workflows: store attachments even if not displayed in history (#32983) #1246

Merged
fpeters merged 1 commits from wip/32983-attachment-store-but-do-not-display into main 2024-03-15 07:19:54 +01:00
3 changed files with 62 additions and 7 deletions

View File

@ -21,7 +21,12 @@ from wcs.qommon.misc import ConnectionError
from wcs.wf.create_formdata import Mapping
from wcs.wf.export_to_model import transform_to_pdf
from wcs.wf.form import WorkflowFormFieldsFormDef
from wcs.workflows import ContentSnapshotPart, Workflow, WorkflowBackofficeFieldsFormDef
from wcs.workflows import (
AttachmentEvolutionPart,
ContentSnapshotPart,
Workflow,
WorkflowBackofficeFieldsFormDef,
)
from wcs.wscalls import NamedWsCall
from ..utilities import clean_temporary_pub, create_temporary_pub, get_app, login
@ -258,7 +263,7 @@ def test_formdata_attachment_download_to_backoffice_file_field_only(pub):
attach = st1.add_action('addattachment', id='_attach')
attach.by = ['_submitter']
attach.backoffice_filefield_id = 'bo1'
attach.attach_to_history = False # store only in backoffice field
attach.attach_to_history = False # do not display in history
wf.store()
assert attach.get_backoffice_filefield_options() == [('bo1', 'bo field 1', 'bo1')]
@ -291,11 +296,56 @@ def test_formdata_attachment_download_to_backoffice_file_field_only(pub):
assert bo1.content_type == 'text/plain'
assert bo1.get_content() == b'foobar'
# but nothing in history
# nothing displayed in history
resp = resp.follow()
assert 'resp.text' not in resp.text
assert len(formdata.evolution) == 2
assert len(formdata.evolution[0].parts) == 1
assert isinstance(formdata.evolution[0].parts[0], ContentSnapshotPart)
assert formdata.evolution[1].parts is None
# but attachment stored
assert isinstance(formdata.evolution[1].parts[0], AttachmentEvolutionPart)
def test_formdata_attachment_stored(pub):
create_user(pub)
wf = Workflow(name='status')
st1 = wf.add_status('Status1', 'st1')
attach = st1.add_action('addattachment', id='_attach')
attach.by = ['_submitter']
attach.backoffice_filefield_id = None # do not store as backoffice field
attach.attach_to_history = False # do not display in history
wf.store()
FormDef.wipe()
formdef = FormDef()
formdef.name = 'test'
formdef.workflow_id = wf.id
formdef.fields = []
formdef.store()
formdef.data_class().wipe()
resp = login(get_app(pub), username='foo', password='foo').get('/test/')
resp = resp.forms[0].submit('submit')
assert 'Check values then click submit.' in resp.text
resp = resp.forms[0].submit('submit')
assert resp.status_int == 302
resp = resp.follow()
assert 'The form has been recorded' in resp.text
resp.forms[0]['attachment_attach$file'] = Upload('test.txt', b'foobar', 'text/plain')
resp = resp.forms[0].submit('button_attach')
# nothing displayed in history
resp = resp.follow()
assert 'resp.text' not in resp.text
formdata = formdef.data_class().select()[0]
assert len(formdata.evolution) == 2
assert len(formdata.evolution[0].parts) == 1
assert isinstance(formdata.evolution[0].parts[0], ContentSnapshotPart)
# but attachment stored
assert isinstance(formdata.evolution[1].parts[0], AttachmentEvolutionPart)
def test_formdata_attachment_file_options(pub):

View File

@ -167,9 +167,10 @@ class AddAttachmentWorkflowStatusItem(WorkflowStatusItem):
self.store_in_backoffice_filefield(
formdata, self.backoffice_filefield_id, filename, content_type, outstream.read()
)
if self.attach_to_history:
f.fp.seek(0)
evo.add_part(AttachmentEvolutionPart.from_upload(f, varname=self.varname))
f.fp.seek(0)
evo_part = AttachmentEvolutionPart.from_upload(f, varname=self.varname)
evo_part.display_in_history = self.attach_to_history
evo.add_part(evo_part)
def get_parameters(self):
parameters = (

View File

@ -372,6 +372,7 @@ class AttachmentEvolutionPart(EvolutionPart):
render_for_fts = None
storage = None
storage_attrs = None
display_in_history = True
def __init__(
self,
@ -452,6 +453,9 @@ class AttachmentEvolutionPart(EvolutionPart):
return odict
def is_hidden(self):
return bool(not self.display_in_history)
def view(self):
show_link = True
if self.has_redirect_url():