From 6012337e1100ba968023195c4216496a7a030127 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Thu, 7 Nov 2019 09:17:28 +0100
Subject: [PATCH 2/3] backoffice: unify workflows list (#37510)

---
 tests/test_admin_pages.py |  1 -
 wcs/admin/workflows.py    | 44 +++++++++++++++++++++++++--------------
 2 files changed, 28 insertions(+), 17 deletions(-)

diff --git a/tests/test_admin_pages.py b/tests/test_admin_pages.py
index e90bff1d8..e459badb4 100644
--- a/tests/test_admin_pages.py
+++ b/tests/test_admin_pages.py
@@ -5361,7 +5361,6 @@ def test_studio_workflows(pub, studio):
     create_superuser(pub)
     app = login(get_app(pub))
     resp = app.get('/backoffice/workflows/')
-    assert '<h2>Workflows for cards</h2>' in resp.body
     resp = resp.click(r'Default \(cards\)')
     assert 'status/recorded/' in resp.body
     assert 'status/deleted/' in resp.body
diff --git a/wcs/admin/workflows.py b/wcs/admin/workflows.py
index 97514daa7..eec1470b5 100644
--- a/wcs/admin/workflows.py
+++ b/wcs/admin/workflows.py
@@ -1832,14 +1832,13 @@ class WorkflowsDirectory(Directory):
             workflows_in_carddef_use = set(carddef_workflows[0].id)
             for carddef in CardDef.select(lightweight=True):
                 workflows_in_carddef_use.add(str(carddef.workflow_id))
-            form_workflow_title = _('Workflows for forms')
         else:
             carddef_workflows = []
             workflows_in_carddef_use = set()
-            form_workflow_title = None
 
         shared_workflows = []
         unused_workflows = []
+        workflows = formdef_workflows + carddef_workflows
 
         for workflow in Workflow.select(order_by='name'):
             if (str(workflow.id) in workflows_in_formdef_use and str(workflow.id) in workflows_in_carddef_use):
@@ -1848,24 +1847,37 @@ class WorkflowsDirectory(Directory):
                 formdef_workflows.append(workflow)
             elif str(workflow.id) in workflows_in_carddef_use:
                 carddef_workflows.append(workflow)
+            if (str(workflow.id) in workflows_in_formdef_use or str(workflow.id) in workflows_in_carddef_use):
+                workflows.append(workflow)
             else:
                 unused_workflows.append(workflow)
 
-        def workflow_section(r, title, workflows):
-            if workflows:
-                if title:
-                    r += htmltext('<h2>%s</h2>') % title
-                r += htmltext('<ul class="biglist">')
-                for workflow in workflows:
-                    r += htmltext('<li>')
-                    r += htmltext('<strong class="label"><a href="%s/">%s</a></strong>') % (workflow.id, workflow.name)
-                    r += htmltext('</li>')
-                r += htmltext('</ul>')
+        def workflow_section(r, workflows):
+            r += htmltext('<div class="bo-block"><ul class="biglist">')
+            for workflow in workflows:
+                if workflow in shared_workflows:
+                    css_class = 'shared-workflow'
+                    usage_label = _('Forms and card models')
+                elif workflow in formdef_workflows:
+                    css_class = 'formdef-workflow'
+                    usage_label = _('Forms')
+                elif workflow in carddef_workflows:
+                    css_class = 'carddef-workflow'
+                    usage_label = _('Card models')
+                else:
+                    css_class = 'unused-workflow'
+                    usage_label = None
+                r += htmltext('<li class="%s">' % css_class)
+                r += htmltext('<strong class="label"><a href="%s/">%s</a></strong>') % (workflow.id, workflow.name)
+                if usage_label and carddef_workflows:
+                    r += htmltext('<p class="details badge">%s</p>') % usage_label
+                r += htmltext('</li>')
+            r += htmltext('</ul></div>')
 
-        workflow_section(r, _('Workflows for both forms and cards'), shared_workflows)
-        workflow_section(r, form_workflow_title, formdef_workflows)
-        workflow_section(r, _('Workflows for cards'), carddef_workflows)
-        workflow_section(r, _('Unused workflows'), unused_workflows)
+        workflow_section(r, workflows)
+        if unused_workflows:
+            r += htmltext('<h2>%s</h2>') % _('Unused workflows')
+            workflow_section(r, unused_workflows)
 
         return r.getvalue()
 
-- 
2.24.0

