From 3be678955e4657b221bc750ffd728b5c2e7014c7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Sun, 19 Jun 2022 17:54:49 +0200
Subject: [PATCH] workflows: store attachment references as templates (#66398)

---
 tests/admin_pages/test_workflow.py | 38 ++++++++++++++++++++++--------
 wcs/workflows.py                   | 19 ++++++++++++---
 2 files changed, 44 insertions(+), 13 deletions(-)

diff --git a/tests/admin_pages/test_workflow.py b/tests/admin_pages/test_workflow.py
index e916ba84a..70755f859 100644
--- a/tests/admin_pages/test_workflow.py
+++ b/tests/admin_pages/test_workflow.py
@@ -1077,22 +1077,22 @@ def test_workflows_edit_email_action(pub):
     resp = app.get(item_url)
     assert "Attachments" in resp.text
     assert "Attachments (templates or Python expressions)" not in resp.text
-    assert resp.form['attachments$element0$choice'].value == 'form_var_upload_raw'
+    assert resp.form['attachments$element0$choice'].value == '{{form_var_upload_raw}}'
     assert len(resp.form['attachments$element0$choice'].options) == 5
     resp = resp.form.submit('attachments$add_element')  # add one
-    resp.form['attachments$element1$choice'] = 'form_var_upload2_raw'
+    resp.form['attachments$element1$choice'] = '{{form_var_upload2_raw}}'
     resp = resp.form.submit('submit')
     assert resp.location
     sendmail = Workflow.get(workflow.id).get_status(st1.id).items[0]
-    assert sendmail.attachments == ['form_var_upload_raw', 'form_var_upload2_raw']
+    assert sendmail.attachments == ['{{form_var_upload_raw}}', '{{form_var_upload2_raw}}']
 
     resp = app.get(item_url)
     resp = resp.form.submit('attachments$add_element')  # add one
-    resp.form['attachments$element2$choice'] = 'form_fbo3_3x'
+    resp.form['attachments$element2$choice'] = '{{form_fbo3_3x}}'
     resp = resp.form.submit('submit')
     assert resp.location
     sendmail = Workflow.get(workflow.id).get_status(st1.id).items[0]
-    assert sendmail.attachments == ['form_var_upload_raw', 'form_var_upload2_raw', 'form_fbo3_3x']
+    assert sendmail.attachments == ['{{form_var_upload_raw}}', '{{form_var_upload2_raw}}', '{{form_fbo3_3x}}']
 
     resp = app.get(item_url)
     resp = resp.form.submit('attachments$add_element')  # add one
@@ -1102,9 +1102,26 @@ def test_workflows_edit_email_action(pub):
     assert resp.location
     sendmail = Workflow.get(workflow.id).get_status(st1.id).items[0]
     assert sendmail.attachments == [
+        '{{form_var_upload_raw}}',
+        '{{form_var_upload2_raw}}',
+        '{{form_fbo3_3x}}',
+        '{"content":"foo", "filename":"bar.txt"}',
+    ]
+
+    # check old attachments python expressions are converted to templates
+    workflow.refresh_from_storage()
+    workflow.get_status(st1.id).items[0].attachments = [
         'form_var_upload_raw',
         'form_var_upload2_raw',
-        'form_fbo3_3x',
+        '{"content":"foo", "filename":"bar.txt"}',
+    ]
+    workflow.store()
+    resp = app.get(item_url)
+    resp = resp.form.submit('submit')
+    sendmail = Workflow.get(workflow.id).get_status(st1.id).items[0]
+    assert sendmail.attachments == [
+        '{{form_var_upload_raw}}',
+        '{{form_var_upload2_raw}}',
         '{"content":"foo", "filename":"bar.txt"}',
     ]
 
@@ -1119,10 +1136,11 @@ def test_workflows_edit_email_action(pub):
     assert resp.location
     sendmail = Workflow.get(workflow.id).get_status(st1.id).items[0]
     assert sendmail.attachments == [
-        'form_var_upload_raw',
-        'form_var_upload2_raw',
+        '{{form_var_upload_raw}}',
+        '{{form_var_upload2_raw}}',
         '{"content":"foo", "filename":"bar.txt"}',
     ]
+
     # remove all backoffice fields
     workflow = Workflow.get(workflow.id)
     workflow.backoffice_fields_formdef.fields = []
@@ -1133,8 +1151,8 @@ def test_workflows_edit_email_action(pub):
     assert resp.location
     sendmail = Workflow.get(workflow.id).get_status(st1.id).items[0]
     assert sendmail.attachments == [
-        'form_var_upload_raw',
-        'form_var_upload2_raw',
+        '{{form_var_upload_raw}}',
+        '{{form_var_upload2_raw}}',
         '{"content":"foo", "filename":"bar.txt"}',
     ]
 
diff --git a/wcs/workflows.py b/wcs/workflows.py
index 049d46649..d0cb3cf82 100644
--- a/wcs/workflows.py
+++ b/wcs/workflows.py
@@ -2154,6 +2154,19 @@ class WorkflowStatusItem(XmlSerialisable):
             if any(x for x in attribute_value if isinstance(x, int)):
                 setattr(self, roles_attribute, [str(x) for x in attribute_value])
                 changed = True
+        if getattr(self, 'attachments', None):
+            if any(x for x in self.attachments if not x.startswith('{{')):
+                # convert old attachment python expression to templates
+                for field in self.parent.parent.get_backoffice_fields():
+                    if field.key != 'file':
+                        continue
+                    if field.varname:
+                        codename = 'form_var_%s_raw' % field.varname
+                    else:
+                        codename = 'form_f%s' % field.id.replace('-', '_')  # = form_fbo<...>
+                    if codename in self.attachments:
+                        changed = True
+                        self.attachments = ['{{%s}}' % x if x == codename else x for x in self.attachments]
         return changed
 
     def render_as_line(self):
@@ -2568,16 +2581,16 @@ class WorkflowStatusItem(XmlSerialisable):
             if field.key != 'file':
                 continue
             if field.varname:
-                codename = 'form_var_%s_raw' % field.varname
+                codename = '{{form_var_%s_raw}}' % field.varname
             else:
-                codename = 'form_f%s' % field.id.replace('-', '_')  # = form_fbo<...>
+                codename = '{{form_f%s}}' % field.id.replace('-', '_')  # = form_fbo<...>
                 varnameless.append(codename)
             attachments_options.append((codename, field.label, codename))
         # filter: do not consider removed fields without varname
         attachments = [
             attachment
             for attachment in self.attachments or []
-            if ((not attachment.startswith('form_fbo')) or (attachment in varnameless))
+            if ((not attachment.startswith('{{form_fbo')) or (attachment in varnameless))
         ]
         return attachments_options, attachments
 
-- 
2.36.1

