From 99d5ca90a24716267ab2e48d81559e2ed4cf536c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 2 Apr 2020 15:17:32 +0200
Subject: [PATCH] wcs: don't display empty sites in BackofficeSubmissionCell
 (#41090)

---
 combo/apps/wcs/models.py                              | 10 ++++++++--
 .../templates/combo/wcs/backoffice_submission.html    |  8 ++++++--
 combo/public/static/js/combo.public.js                | 11 +++++++++++
 tests/test_wcs.py                                     |  9 ++++++++-
 4 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/combo/apps/wcs/models.py b/combo/apps/wcs/models.py
index b441aef..b9358e7 100644
--- a/combo/apps/wcs/models.py
+++ b/combo/apps/wcs/models.py
@@ -754,9 +754,15 @@ class BackofficeSubmissionCell(WcsDataBaseCell):
 
     def get_cell_extra_context(self, context):
         context = super(BackofficeSubmissionCell, self).get_cell_extra_context(context)
+        all_formdefs = context.pop('all_formdefs')
+        formdefs = {}
         # add a fake category where it's missing
-        for site_formdefs in context['all_formdefs'].values():
+        for key, site_formdefs in all_formdefs.items():
+            if not site_formdefs['data']:
+                continue
+            formdefs[key] = site_formdefs
             for formdef in site_formdefs['data']:
-                if not 'category' in formdef:
+                if 'category' not in formdef:
                     formdef['category'] = _('Misc')
+        context['all_formdefs'] = formdefs
         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 136a7d9..f126db4 100644
--- a/combo/apps/wcs/templates/combo/wcs/backoffice_submission.html
+++ b/combo/apps/wcs/templates/combo/wcs/backoffice_submission.html
@@ -1,8 +1,9 @@
 {% load i18n %}
 {% block cell-content %}
+{% if all_formdefs %}
 <h2>{% trans "New Form" %}</h2>
 {% for site_formdefs in all_formdefs.values %}
-  <div class="links-list">
+<div class="links-list">
   {% if all_formdefs.keys|length > 1 %}<h3>{{ site_formdefs.title }}</h3>{% endif %}
   <ul>
   {% regroup site_formdefs.data|dictsort:"category" by category as categories_formdefs %}
@@ -12,7 +13,10 @@
       <li><a href="{{formdef.backoffice_submission_url}}?NameID={{name_id}}&ReturnURL={{ absolute_uri|iriencode }}">{{formdef.title}}</a></li>
     {% endfor %}
   {% endfor %}
-</ul>
+  </ul>
 </div>
 {% endfor %}
+{% elif not combo_hide_empty_message %}
+<div class="empty-message"><p>{% trans "There are no possible backoffice submissions." %}</p></div>
+{% endif %}
 {% endblock %}
diff --git a/combo/public/static/js/combo.public.js b/combo/public/static/js/combo.public.js
index 46cbe72..6350596 100644
--- a/combo/public/static/js/combo.public.js
+++ b/combo/public/static/js/combo.public.js
@@ -25,12 +25,18 @@ function combo_load_cell(elem) {
               } else {
                   $elem.find('> div').html(data);
               }
+              if (! $elem.find('> div').children().length) {
+                $elem.addClass('empty-cell');
+              } else {
+                $elem.removeClass('empty-cell')
+              }
               $(document).trigger('combo:cell-loaded', $elem);
           },
           error: function(error) {
               var msg = $(elem).data('ajax-cell-error-message');
               if (!msg) msg = 'Unknown error (code: ' + error.status + ')';
               $elem
+                  .removeClass('empty-cell')
                   .find('.loading')
                   .addClass('error-loading')
                   .html('<span class="loading-message">' + msg + '</span>');
@@ -81,6 +87,11 @@ $(function() {
   $('[data-ajax-cell-must-load]').each(function(idx, elem) {
     combo_load_cell($(elem).parents('div.cell')[0]);
   });
+  $('div.cell > div').each(function(idx, elem) {
+    if (! $('[data-ajax-cell-must-load]', $(elem)).length && ! $(elem).children().length) {
+      $(elem).parent().addClass('empty-cell');
+    }
+  });
 
   /* utility functions and events, for themes */
   $('.togglable').on('click', function() {
diff --git a/tests/test_wcs.py b/tests/test_wcs.py
index 4e13c1d..4c055f5 100644
--- a/tests/test_wcs.py
+++ b/tests/test_wcs.py
@@ -1172,6 +1172,7 @@ def test_wcs_search_engines(app):
     assert 'tracking-code' not in search_engines.keys()
     assert len([x for x in search_engines.keys() if x.startswith('formdata:')]) == 0
 
+
 @wcs_present
 def test_backoffice_submission_cell_render(context):
     page = Page(title='xxx', slug='test_backoffice_submission_cell_render', template_name='standard')
@@ -1180,15 +1181,19 @@ def test_backoffice_submission_cell_render(context):
     cell.wcs_site = 'default'
     cell.save()
 
-    context['synchronous'] = True # to get fresh content
+    context['synchronous'] = True  # to get fresh content
 
     result = cell.render(context)
     assert '/backoffice/submission/a-private-form/' not in result
+    assert context['all_formdefs'] == {}
+    assert 'h2' not in result
 
     context['request'].user = MockUser()
 
     result = cell.render(context)
     assert '/backoffice/submission/a-private-form/' not in result
+    assert context['all_formdefs'] == {}
+    assert 'h2' not in result
 
     class MockUser2(MockUser):
         email = 'foo2@example.net'
@@ -1196,6 +1201,8 @@ def test_backoffice_submission_cell_render(context):
 
     result = cell.render(context)
     assert '/backoffice/submission/a-private-form/' in result
+    assert list(context['all_formdefs'].keys()) == ['default']
+    assert 'h2' in result
 
 
 @wcs_present
-- 
2.20.1

