From 9c2ddf1cf446d425264825bc0967b6b2023b6a2d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 9 Dec 2021 12:04:42 +0100
Subject: [PATCH] backoffice: show some fields in additional options if empty
 only (#58752)

---
 tests/admin_pages/test_datasource.py | 24 +++++++++++++++++++++++-
 wcs/admin/data_sources.py            |  6 +++---
 2 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/tests/admin_pages/test_datasource.py b/tests/admin_pages/test_datasource.py
index 6afdf466..c247793d 100644
--- a/tests/admin_pages/test_datasource.py
+++ b/tests/admin_pages/test_datasource.py
@@ -447,7 +447,7 @@ def test_data_sources_edit(pub):
     create_superuser(pub)
     NamedDataSource.wipe()
     data_source = NamedDataSource(name='foobar')
-    data_source.data_source = {'type': 'formula', 'value': '[]'}
+    data_source.data_source = {'type': 'json', 'value': 'http://some.url'}
     data_source.store()
 
     FormDef.wipe()
@@ -463,6 +463,28 @@ def test_data_sources_edit(pub):
 
     assert NamedDataSource.get(1).description == 'data source description'
 
+    resp = app.get('/backoffice/settings/data-sources/1/edit')
+    assert '<legend>Additional options</legend>' in resp.text
+    assert '>Data Attribute</label>' in resp.text
+    assert '>Id Attribute</label>' in resp.text
+    assert '>Text Attribute</label>' in resp.text
+    assert resp.text.index('<legend>Additional options</legend>') < resp.text.index('>Data Attribute</label>')
+    assert resp.text.index('>Data Attribute</label>') < resp.text.index('>Id Attribute</label>')
+    assert resp.text.index('>Id Attribute</label>') < resp.text.index('>Text Attribute</label>')
+
+    data_source.data_attribute = 'data'
+    data_source.id_attribute = 'id'
+    data_source.text_attribute = 'text'
+    data_source.store()
+    resp = app.get('/backoffice/settings/data-sources/1/edit')
+    assert '<legend>Additional options</legend>' in resp.text
+    assert '>Data Attribute</label>' in resp.text
+    assert '>Id Attribute</label>' in resp.text
+    assert '>Text Attribute</label>' in resp.text
+    assert resp.text.index('>Data Attribute</label>') < resp.text.index('>Id Attribute</label>')
+    assert resp.text.index('>Id Attribute</label>') < resp.text.index('>Text Attribute</label>')
+    assert resp.text.index('>Text Attribute</label>') < resp.text.index('<legend>Additional options</legend>')
+
 
 def test_data_sources_edit_duplicate_name(pub):
     create_superuser(pub)
diff --git a/wcs/admin/data_sources.py b/wcs/admin/data_sources.py
index edbd6a31..e22b7a15 100644
--- a/wcs/admin/data_sources.py
+++ b/wcs/admin/data_sources.py
@@ -186,7 +186,7 @@ class NamedDataSourceUI:
                     'Possibility to chain attributes with a dot separator (example: data.results)'
                 ),
                 required=False,
-                advanced=True,
+                advanced=not (self.datasource.data_attribute),
                 attrs={
                     'data-dynamic-display-child-of': 'data_source$type',
                     'data-dynamic-display-value': 'json',
@@ -199,7 +199,7 @@ class NamedDataSourceUI:
                 title=_('Id Attribute'),
                 hint=_('Name of the attribute containing the identifier of an entry (default: id)'),
                 required=False,
-                advanced=True,
+                advanced=not (self.datasource.id_attribute),
                 attrs={
                     'data-dynamic-display-child-of': 'data_source$type',
                     'data-dynamic-display-value': 'json',
@@ -212,7 +212,7 @@ class NamedDataSourceUI:
                 title=_('Text Attribute'),
                 hint=_('Name of the attribute containing the label of an entry (default: text)'),
                 required=False,
-                advanced=True,
+                advanced=not (self.datasource.text_attribute),
                 attrs={
                     'data-dynamic-display-child-of': 'data_source$type',
                     'data-dynamic-display-value': 'json',
-- 
2.33.0

