From b4e9824753049d7246cec0fcce4f98b08dae464b Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Thu, 4 Mar 2021 12:40:15 +0100
Subject: [PATCH 2/2] fields: display ItemsField using <ul> on summary page if
 possible (#44123)

---
 tests/test_fields.py              | 39 +++++++++++++++++++++++++++++++
 wcs/fields.py                     | 23 ++++++++++++++++++
 wcs/qommon/static/css/qommon.scss |  5 ++++
 3 files changed, 67 insertions(+)

diff --git a/tests/test_fields.py b/tests/test_fields.py
index ab9081c3..659f65a5 100644
--- a/tests/test_fields.py
+++ b/tests/test_fields.py
@@ -155,6 +155,45 @@ def test_items():
     assert field.get_options() == [('1', 'foo', '1'), ('2', 'bar', '2')]
     assert field.get_options() == [('1', 'foo', '1'), ('2', 'bar', '2')]  # twice for cached behaviour
 
+    # if labels are available, display using <ul>
+    field = fields.ItemsField()
+    view_value = field.get_view_value('a, b', value_id=['1', '2'], labels=['a', 'b'])
+    elems = BeautifulSoup(str(view_value)).find('ul').find_all('li')
+    assert len(elems) == 2
+    assert elems[0].text == 'a'
+    assert elems[1].text == 'b'
+
+
+def test_items_get_value_info():
+    # no data source : labels are available
+    field = fields.ItemsField(id='1', items=['a', 'b', 'c'])
+    assert field.get_value_info({'1': ['un', 'deux'], '1_display': 'un, deux'}) == (
+        'un, deux',
+        {'value_id': ['un', 'deux'], 'labels': ['un', 'deux']},
+    )
+
+    # data source with structured : labels are available
+    field = fields.ItemsField(id='1')
+    field.data_source = {'type': 'formula', 'value': '''[('1', 'un'), ('2', 'deux')]'''}
+    assert field.get_value_info(
+        {
+            '1': ['un', 'deux'],
+            '1_display': 'un, deux',
+            '1_structured': [{'id': '1', 'text': 'un'}, {'id': '2', 'text': 'deux'}],
+        }
+    ) == (
+        'un, deux',
+        {'value_id': ['un', 'deux'], 'labels': ['un', 'deux']},
+    )
+
+    # data source with no strucured : no labels
+    field = fields.ItemsField(id='1')
+    field.data_source = {'type': 'formula', 'value': '''[('1', 'un'), ('2', 'deux')]'''}
+    assert field.get_value_info({'1': ['un', 'deux'], '1_display': 'un, deux'}) == (
+        'un, deux',
+        {'value_id': ['un', 'deux'], 'labels': []},
+    )
+
 
 def test_password():
     assert fields.PasswordField().get_view_value('xxx') == '●' * 8
diff --git a/wcs/fields.py b/wcs/fields.py
index 0ce72fa1..c7f91bfd 100644
--- a/wcs/fields.py
+++ b/wcs/fields.py
@@ -2224,7 +2224,30 @@ class ItemsField(WidgetField, ItemFieldMixin):
         except TypeError:
             raise ValueError('invalid data for items type (%r)' % value)
 
+    def get_value_info(self, data):
+        value, value_details = super().get_value_info(data)
+        labels = []
+        if not self.data_source:
+            value_id = value_details.get('value_id')
+            if value_id:
+                labels = value_id.copy()
+        else:
+            structured_values = self.get_structured_value(data)
+            if structured_values:
+                labels = [x['text'] for x in structured_values]
+        value_details['labels'] = labels
+        return (value, value_details)
+
     def get_view_value(self, value, **kwargs):
+        if kwargs.get('labels'):
+            # summary page and labels are available
+            r = TemplateIO(html=True)
+            r += htmltext('<ul>')
+            for x in kwargs['labels']:
+                r += htmltext('<li>%s</li>' % x)
+            r += htmltext('</ul>')
+            return r.getvalue()
+
         if type(value) is str:  # == display_value
             return value
         if value:
diff --git a/wcs/qommon/static/css/qommon.scss b/wcs/qommon/static/css/qommon.scss
index caf29759..68dbb6e0 100644
--- a/wcs/qommon/static/css/qommon.scss
+++ b/wcs/qommon/static/css/qommon.scss
@@ -301,6 +301,11 @@ div.dataview .value {
 	margin-left: 1em;
 }
 
+div.dataview div.field-type-items .value ul {
+	margin-top: 0;
+	margin-bottom : 0;
+}
+
 div.dataview .field-type-block > .value {
 	margin-left: 0;
 }
-- 
2.20.1

