From 66307a08fae5114086e2c5b5d23ca316da87407e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Tue, 4 Jan 2022 15:49:19 +0100
Subject: [PATCH] backoffice: do not propose block subfields for columns
 (#60265)

---
 tests/backoffice_pages/test_all.py     | 43 ++++++++++++++++++++++++++
 tests/backoffice_pages/test_filters.py | 11 ++++---
 wcs/backoffice/management.py           |  3 ++
 3 files changed, 52 insertions(+), 5 deletions(-)

diff --git a/tests/backoffice_pages/test_all.py b/tests/backoffice_pages/test_all.py
index 7f7f68d5f..d618e1ded 100644
--- a/tests/backoffice_pages/test_all.py
+++ b/tests/backoffice_pages/test_all.py
@@ -824,6 +824,49 @@ def test_backoffice_card_field_columns(pub):
     } in resp_geojson.json['features'][0]['properties']['display_fields']
 
 
+def test_backoffice_block_columns(pub):
+    create_environment(pub)
+    create_superuser(pub)
+
+    BlockDef.wipe()
+    block = BlockDef()
+    block.name = 'foobar'
+    block.fields = [
+        fields.StringField(id='123', required=True, label='Test', type='string'),
+    ]
+    block.store()
+
+    formdef = FormDef.get_by_urlname('form-title')
+    formdef.fields.append(
+        fields.BlockField(id='8', label='Block', type='block:foobar', varname='data', max_items=3),
+    )
+    formdef.store()
+
+    for formdata in formdef.data_class().select(lambda x: x.status == 'wf-new'):
+        formdata.data['8'] = {
+            'data': [{'123': 'blah'}],
+            'schema': {'123': 'string'},
+        }
+        formdata.data['8_display'] = 'blah'
+        formdata.store()
+
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/management/form-title/')
+    # check Block / Test is not part of the possible columns
+    assert [x.text_content() for x in resp.pyquery('#columns-filter label')] == [
+        'Number',
+        'Created',
+        'Last Modified',
+        'User Label',
+        '1st field',
+        '2nd field',
+        'Status',
+        '3rd field',
+        'Block',
+        'Anonymised',
+    ]
+
+
 def test_backoffice_form_category_permissions(pub):
     user = create_user(pub)
     create_environment(pub)
diff --git a/tests/backoffice_pages/test_filters.py b/tests/backoffice_pages/test_filters.py
index 78a68cd0a..78b960637 100644
--- a/tests/backoffice_pages/test_filters.py
+++ b/tests/backoffice_pages/test_filters.py
@@ -794,11 +794,12 @@ def test_backoffice_block_field_filter(pub, local_user):
 
     app = login(get_app(pub))
     resp = app.get('/backoffice/management/form-title/')
-    assert '<label><input type="checkbox" name="1"/>Block Data / String</label>' in resp
-    assert '<label><input type="checkbox" name="2"/>Block Data / Item</label>' in resp
-    assert '<label><input type="checkbox" name="3"/>Block Data / Bool</label>' in resp
-    assert '<label><input type="checkbox" name="4"/>Block Data / Date</label>' in resp
-    assert '<label><input type="checkbox" name="5"/>Block Data / Email</label>' in resp
+    # block subfields should not be proposed as columns
+    assert '<label><input type="checkbox" name="1"/>Block Data / String</label>' not in resp
+    assert '<label><input type="checkbox" name="2"/>Block Data / Item</label>' not in resp
+    assert '<label><input type="checkbox" name="3"/>Block Data / Bool</label>' not in resp
+    assert '<label><input type="checkbox" name="4"/>Block Data / Date</label>' not in resp
+    assert '<label><input type="checkbox" name="5"/>Block Data / Email</label>' not in resp
 
     # string
     resp = app.get('/backoffice/management/form-title/')
diff --git a/wcs/backoffice/management.py b/wcs/backoffice/management.py
index 0fd003f6c..4ae856ad7 100644
--- a/wcs/backoffice/management.py
+++ b/wcs/backoffice/management.py
@@ -1335,6 +1335,9 @@ class FormPage(Directory):
             for field in sorted(self.get_formdef_fields(), key=get_column_position):
                 if not hasattr(field, 'get_view_value'):
                     continue
+                if getattr(field, 'block_field', None):
+                    # fields from blocks cannot yet be added as column
+                    continue
                 classnames = ''
                 attrs = ''
                 if isinstance(field, RelatedField):
-- 
2.34.1

