From 394f90006a2624ea4774bed9cbd4b087bd01eade Mon Sep 17 00:00:00 2001
From: Thomas NOEL <tnoel@entrouvert.com>
Date: Fri, 10 May 2019 14:31:06 +0200
Subject: [PATCH] workflows: allow add attachment with only a varname (#32983)

---
 tests/test_form_pages.py | 33 +++++++++++++++++++++++++++++++++
 wcs/wf/attachment.py     |  5 +++--
 wcs/workflows.py         |  5 +++--
 3 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/tests/test_form_pages.py b/tests/test_form_pages.py
index 37e8cbaf..1025c4a5 100644
--- a/tests/test_form_pages.py
+++ b/tests/test_form_pages.py
@@ -2772,6 +2772,7 @@ def test_formdata_attachment_download(pub):
     attach = AddAttachmentWorkflowStatusItem()
     attach.id = '_attach'
     attach.by = ['_submitter']
+    assert attach.attach_to_history == True
     st1.items.append(attach)
     attach.parent = st1
     wf.store()
@@ -2813,6 +2814,7 @@ def test_formdata_attachment_download_with_substitution_variable(pub):
     st1 = wf.add_status('Status1', 'st1')
     attach = AddAttachmentWorkflowStatusItem()
     attach.varname = 'attached_doc'
+    assert attach.attach_to_history == True
     attach.id = '_attach'
     attach.by = ['_submitter']
     st1.items.append(attach)
@@ -2866,6 +2868,37 @@ def test_formdata_attachment_download_with_substitution_variable(pub):
     assert content_disposition.split(';')[0] == 'attachment'
     assert resp.request.environ['PATH_INFO'].endswith(attachment_variable.filename)
 
+    # do not show in history, only in substitution variables
+    attach.attach_to_history = False
+    wf.store()
+    formdef.data_class().wipe()
+
+    resp = login(get_app(pub), username='foo', password='foo').get('/test/')
+    resp = resp.forms[0].submit('submit')
+    resp = resp.forms[0].submit('submit')
+    resp = resp.follow()
+    assert 'The form has been recorded' in resp.body
+    resp.forms[0]['attachment_attach'] = Upload('test.txt', 'foobar', 'text/plain')
+    resp = resp.forms[0].submit('button_attach')
+    assert formdef.data_class().count() == 1
+    formdata = formdef.data_class().select()[0]
+    assert formdata.evolution[-1].parts[0].__class__.__name__ == 'AttachmentEvolutionPart'
+    attachment = formdata.evolution[-1].parts[0]
+    assert attachment.content_type == 'text/plain'
+    assert attachment.orig_filename == 'test.txt'
+
+    resp = resp.follow()  # back to form page
+    # no link to the document
+    assert 'test.txt' not in resp.body
+    # substitution variable is ok
+    variables = formdef.data_class().select()[0].get_substitution_variables()
+    assert 'attachments' in variables
+    attachments = variables['attachments']
+    assert attachments is not None
+    doc = attachments.attached_doc
+    # cannot download the document
+    resp = login(get_app(pub), username='foo', password='foo').get(doc.url, status=404)
+
 def test_formdata_attachment_download_to_backoffice_file_field(pub):
     create_user(pub)
     wf = Workflow(name='status')
diff --git a/wcs/wf/attachment.py b/wcs/wf/attachment.py
index dc7024bc..2dbcf658 100644
--- a/wcs/wf/attachment.py
+++ b/wcs/wf/attachment.py
@@ -135,9 +135,10 @@ class AddAttachmentWorkflowStatusItem(WorkflowStatusItem):
                     filename,
                     content_type,
                     outstream.read())
-            if self.attach_to_history:
+            if self.attach_to_history or self.varname:
                 f.fp.seek(0)
-                evo.add_part(AttachmentEvolutionPart.from_upload(f, varname=self.varname))
+                evo.add_part(AttachmentEvolutionPart.from_upload(f, varname=self.varname,
+                                                                 is_visible=bool(self.attach_to_history)))
 
     def get_parameters(self):
         parameters = ('by', 'required', 'title', 'display_title', 'button_label',
diff --git a/wcs/workflows.py b/wcs/workflows.py
index 3cb97370..a4eb8ec8 100644
--- a/wcs/workflows.py
+++ b/wcs/workflows.py
@@ -179,14 +179,15 @@ class AttachmentEvolutionPart: #pylint: disable=C1001
         self.is_visible = is_visible
 
     @classmethod
-    def from_upload(cls, upload, varname=None):
+    def from_upload(cls, upload, varname=None, is_visible=True):
         return AttachmentEvolutionPart(
                 upload.base_filename,
                 upload.fp,
                 upload.orig_filename,
                 upload.content_type,
                 upload.charset,
-                varname=varname)
+                varname=varname,
+                is_visible=is_visible)
 
     def get_file_pointer(self):
         return open(self.filename)
-- 
2.20.1

