From eefc6dd444b2ac7bd6532f1f5af310f2f5e2eb84 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Fri, 16 Oct 2020 19:37:12 +0200
Subject: [PATCH] snapshots: detail status action comments (#47367)

---
 tests/test_snapshots.py | 43 +++++++++++++++++++++++++++++++++++++++++
 wcs/admin/workflows.py  | 18 ++++++++++++++---
 2 files changed, 58 insertions(+), 3 deletions(-)

diff --git a/tests/test_snapshots.py b/tests/test_snapshots.py
index ba62401d..30481e15 100644
--- a/tests/test_snapshots.py
+++ b/tests/test_snapshots.py
@@ -385,8 +385,51 @@ def test_form_snapshot_save(pub, formdef_with_history):
     for i in range(10, 15):
         formdef.description = 'this is a description (%s)' % i
         formdef.store()
 
     resp = app.get('/backoffice/forms/%s/history/' % formdef_with_history.id)
     assert [x.attrib['class'] for x in resp.pyquery.find('ul.snapshots-list li')] == [
             'new-day', 'collapsed', 'collapsed', 'collapsed', 'collapsed', 'has-label',
             'collapsed', 'collapsed', 'collapsed', 'collapsed', 'collapsed', 'collapsed']
+
+
+def test_snaphost_workflow_status_item_comments(pub):
+    create_superuser(pub)
+    create_role()
+
+    Workflow.wipe()
+    workflow = Workflow(name='test')
+    workflow.add_status(name='baz')
+    workflow.store()
+
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/workflows/1/')
+    resp = resp.click('baz')
+
+    resp.forms[0]['action-interaction'] = 'Webservice'
+    resp = resp.forms[0].submit()
+    resp = resp.follow()
+
+    resp = resp.click('Edit', href='items/1/')
+    resp.form['url'] = 'http://example.org'
+    resp = resp.forms[0].submit('submit')
+    resp = resp.follow()
+    resp = resp.follow()
+
+    resp = resp.click('Edit', href='items/1/')
+    resp.form['label'] = 'foo'
+    resp = resp.forms[0].submit('submit')
+    resp = resp.follow()
+    resp = resp.follow()
+
+    resp = resp.click(href='items/1/delete')
+    resp = resp.form.submit('submit')
+
+    resp = app.get('/backoffice/workflows/%s/history/' % workflow.id)
+    comments = [x.text[18:x.text.find('\n')]
+                for x in resp.html.find('ul', {'class': 'snapshots-list'}).find_all('li')]
+    assert comments == [
+        'Deletion of action "Webservice (foo)" in status "baz"',
+        'Change in action "Webservice (foo)" in status "baz"',
+        'Change in action "Webservice" in status "baz"',
+        'New action "Webservice" in status "baz"',
+        '']
diff --git a/wcs/admin/workflows.py b/wcs/admin/workflows.py
index 65fc3241..ef88d801 100644
--- a/wcs/admin/workflows.py
+++ b/wcs/admin/workflows.py
@@ -290,17 +290,21 @@ class WorkflowItemPage(Directory):
             r = TemplateIO(html=True)
             r += htmltext('<h2>%s</h2>') % _(self.item.description)
             r += form.render()
             if self.item.support_substitution_variables:
                 r += get_publisher().substitutions.get_substitution_html_table()
             return r.getvalue()
         else:
             self.item.submit_admin_form(form)
-            self.workflow.store(comment=_('Change in action "%s"') % _(self.item.description))
+            self.workflow.store(
+                comment=_('Change in action "%(description)s" in status "%(status)s"') % {
+                    'description': self.item.render_as_line(),
+                    'status': self.parent.name,
+                })
             return redirect('..')
 
     def delete(self):
         form = Form(enctype='multipart/form-data')
         form.widgets.append(HtmlWidget('<p>%s</p>' % _('You are about to remove an item.')))
         form.add_submit('delete', _('Delete'))
         form.add_submit('cancel', _('Cancel'))
         if form.get_widget('cancel').parse():
@@ -309,17 +313,21 @@ class WorkflowItemPage(Directory):
             get_response().breadcrumb.append(('delete', _('Delete')))
             self.html_top(title = _('Delete Item'))
             r = TemplateIO(html=True)
             r += htmltext('<h2>%s</h2>') % _('Deleting Item')
             r += form.render()
             return r.getvalue()
         else:
             del self.parent.items[self.parent.items.index(self.item)]
-            self.workflow.store(comment=_('Deletion of action "%s"') % _(self.item.description))
+            self.workflow.store(
+                comment=_('Deletion of action "%(description)s" in status "%(status)s"') % {
+                    'description': self.item.render_as_line(),
+                    'status': self.parent.name,
+                })
             return redirect('../../')
 
     def _q_lookup(self, component):
         t = self.item.q_admin_lookup(self.workflow, self.parent, component,
                 self.html_top)
         if t:
             return t
         return Directory._q_lookup(self, component)
@@ -566,17 +574,21 @@ class WorkflowStatusPage(Directory):
         if not form.is_submitted() or form.has_errors():
             get_session().message = ('error', _('Submitted form was not filled properly.'))
             return redirect('.')
 
         for category in ('status-change', 'interaction', 'formdata-action', 'user-action'):
             action_type = form.get_widget('action-%s' % category).parse()
             if action_type:
                 self.status.append_item(action_type)
-                self.workflow.store(comment=_('New action'))
+                self.workflow.store(
+                    comment=_('New action "%(description)s" in status "%(status)s"') % {
+                        'description': self.status.items[-1].description,
+                        'status': self.status.name,
+                    })
                 return redirect('.')
 
         get_session().message = ('error', _('Submitted form was not filled properly.'))
         return redirect('.')
 
 
     def delete(self):
         form = Form(enctype="multipart/form-data")
-- 
2.28.0

