From 3b2a565c52ba5ea9416480058e6dcafe8a265982 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Thu, 26 Sep 2019 15:34:57 +0200
Subject: [PATCH] wcs: use wcs category order in backoffice submission cell
 (#29187)

---
 combo/apps/wcs/models.py                      | 23 ++++++++
 .../combo/wcs/backoffice_submission.html      | 11 ++--
 tests/test_wcs.py                             | 53 ++++++++++++++-----
 3 files changed, 68 insertions(+), 19 deletions(-)

diff --git a/combo/apps/wcs/models.py b/combo/apps/wcs/models.py
index c305336..c7b758a 100644
--- a/combo/apps/wcs/models.py
+++ b/combo/apps/wcs/models.py
@@ -557,4 +557,27 @@ class BackofficeSubmissionCell(WcsDataBaseCell):
             for formdef in site_formdefs['data']:
                 if not 'category' in formdef:
                     formdef['category'] = _('Misc')
+
+        # group formdef by categories, keeping the w.c.s. categories order
+        sites = []
+        for site in context['all_formdefs'].values():
+            category_names = []
+            tmp_categories = {}
+            for formdef in site['data']:
+                category_name = formdef['category']
+
+                # remind first seen category order
+                if category_name not in category_names:
+                    category_names.append(category_name)
+
+                    tmp_categories[category_name] = []
+                tmp_categories[category_name].append(formdef)
+            categories = []
+            for category_name in category_names:
+                categories.append({
+                    'name': category_name,
+                    'formdefs': tmp_categories[category_name]
+                })
+            sites.append({'title': site['title'], 'categories': categories})
+        context['reordered_all_formdefs'] = sites
         return context
diff --git a/combo/apps/wcs/templates/combo/wcs/backoffice_submission.html b/combo/apps/wcs/templates/combo/wcs/backoffice_submission.html
index 83e431b..a7cbd02 100644
--- a/combo/apps/wcs/templates/combo/wcs/backoffice_submission.html
+++ b/combo/apps/wcs/templates/combo/wcs/backoffice_submission.html
@@ -1,14 +1,13 @@
 {% load combo i18n %}
 {% block cell-content %}
 <h2>{% trans "New Form" %}</h2>
-{% for site_formdefs in all_formdefs.values %}
+{% for site in reordered_all_formdefs %}
   <div class="links-list">
-  {% if all_formdefs.keys|length > 1 %}<h3>{{ site_formdefs.title }}</h3>{% endif %}
+  {% if reordered_all_formdefs|length > 1 %}<h3>{{ site.title }}</h3>{% endif %}
   <ul>
-  {% regroup site_formdefs.data|dictsort:"category" by category as categories_formdefs %}
-  {% for category_formdefs in categories_formdefs %}
-    <li><h4>{{ category_formdefs.grouper }}</h4></li>
-    {% for formdef in category_formdefs.list|dictsort:"title" %}
+  {% for category in site.categories %}
+    <li><h4>{{ category.name }}</h4></li>
+    {% for formdef in category.formdefs %}
       <li><a href="{{formdef.backoffice_submission_url}}?NameID={{name_id}}&ReturnURL={{ absolute_uri|iriencode }}">{{formdef.title}}</a></li>
     {% endfor %}
   {% endfor %}
diff --git a/tests/test_wcs.py b/tests/test_wcs.py
index 4f03bf9..5c64160 100644
--- a/tests/test_wcs.py
+++ b/tests/test_wcs.py
@@ -139,17 +139,19 @@ if '127.0.0.2' in get_publisher().get_frontoffice_url():
     code.id = 'CNPHNTFB'
     code.formdata = formdata
 
-# a private formdef
+# private formdefs
 role = Role(name='Blah')
 role.store()
 
-formdef = FormDef()
-formdef.name = 'a private form'
-formdef.category_id = cats[2].id
-formdef.roles = [role.id]
-formdef.backoffice_submission_roles = [role.id]
-formdef.fields = []
-formdef.store()
+for i in range(1,5):
+    category_id = 2+i%2  # not ordered by categories
+    formdef = FormDef()
+    formdef.category_id = cats[category_id].id
+    formdef.name = 'private form %d (category %s)' % (i, formdef.category_id)
+    formdef.roles = [role.id]
+    formdef.backoffice_submission_roles = [role.id]
+    formdef.fields = []
+    formdef.store()
 
 user2 = get_publisher().user_class()  # agent
 user2.name = 'foo2 bar2'
@@ -259,13 +261,19 @@ def test_form_cell_setup():
     form_class = cell.get_default_form_class()
     form = form_class()
     assert form.fields['formdef_reference'].widget.choices == [
-            (u'default:a-private-form', u'test : a private form'),
             (u'default:a-second-form-title', u'test : a second form title'),
             (u'default:form-title', u'test : form title'),
+            (u'default:private-form-1-category-4', u'test : private form 1 (category 4)'),
+            (u'default:private-form-2-category-3', u'test : private form 2 (category 3)'),
+            (u'default:private-form-3-category-4', u'test : private form 3 (category 4)'),
+            (u'default:private-form-4-category-3', u'test : private form 4 (category 3)'),
             (u'default:third-form-title', u'test : third form title'),
-            (u'other:a-private-form', u'test2 : a private form'),
             (u'other:a-second-form-title', u'test2 : a second form title'),
             (u'other:form-title', u'test2 : form title'),
+            (u'other:private-form-1-category-4', u'test2 : private form 1 (category 4)'),
+            (u'other:private-form-2-category-3', u'test2 : private form 2 (category 3)'),
+            (u'other:private-form-3-category-4', u'test2 : private form 3 (category 4)'),
+            (u'other:private-form-4-category-3', u'test2 : private form 4 (category 3)'),
             (u'other:third-form-title', u'test2 : third form title')]
 
 
@@ -471,8 +479,10 @@ def test_forms_of_category_cell_setup():
     form = form_class()
     assert form.fields['category_reference'].widget.choices == [
             (u'default:test-3', u'test : Test 3'),
+            (u'default:test-4', u'test : Test 4'),
             (u'default:test-9', u'test : Test 9'),
             (u'other:test-3', u'test2 : Test 3'),
+            (u'other:test-4', u'test2 : Test 4'),
             (u'other:test-9', u'test2 : Test 9')]
 
 @wcs_present
@@ -589,8 +599,9 @@ def test_manager_current_forms(app, admin_user):
 
     resp = app.get('/manage/pages/%s/' % page.id)
     assert ('data-cell-reference="%s"' % cells[0].get_reference()) in resp.text
-    assert len(resp.form['c%s-categories' % cells[0].get_reference()].options) == 4
-    resp.form['c%s-categories' % cells[0].get_reference()].value = ['default:test-3', 'default:test-9']
+    assert len(resp.form['c%s-categories' % cells[0].get_reference()].options) == 6
+    resp.form['c%s-categories' % cells[0].get_reference()].value = [
+        'default:test-3', 'default:test-9']
     resp = resp.form.submit().follow()
     assert resp.form['c%s-categories' % cells[0].get_reference()].value == ['default:test-3', 'default:test-9']
 
@@ -781,4 +792,20 @@ def test_backoffice_submission_cell_render(context):
     context['request'].user = MockUser2()
 
     result = cell.render(context)
-    assert '/backoffice/submission/a-private-form/' in result
+    assert [x['title'] for x in context['all_formdefs'].values()[0]['data']] == [
+        'private form 1 (category 4)',
+        'private form 2 (category 3)',
+        'private form 3 (category 4)',
+        'private form 4 (category 3)']
+    assert [c['formdefs'][i]['title']
+            for c in context['reordered_all_formdefs'][0]['categories']
+                for i in range(0, 2)] == [
+         'private form 1 (category 4)',
+         'private form 3 (category 4)',
+         'private form 2 (category 3)',
+         'private form 4 (category 3)']
+    assert result.count('/backoffice/submission/private-form-') == 4
+    index = {}
+    for i in range(1, 5):
+        index['form%d' % i] = result.find('/private-form-%d' % i)
+    assert index['form1'] < index['form3'] < index['form2'] < index['form4']
-- 
2.23.0

