From ab7fa5bcabbf5d281fcac174661d731e1e5982fe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Wed, 31 Jul 2019 12:46:26 +0200
Subject: [PATCH 2/2] backoffice: display formdefs using a data source (#33693)

---
 tests/test_admin_pages.py                      | 13 +++++++++++++
 wcs/admin/data_sources.py                      | 16 ++++++++++++++++
 wcs/formdef.py                                 |  4 ++++
 wcs/templates/wcs/backoffice/data-sources.html | 12 ++++++++++++
 4 files changed, 45 insertions(+)

diff --git a/tests/test_admin_pages.py b/tests/test_admin_pages.py
index 4cc27ac7f..63a243acd 100644
--- a/tests/test_admin_pages.py
+++ b/tests/test_admin_pages.py
@@ -4621,6 +4621,19 @@ def test_data_sources_view(pub):
     assert 'Preview' in resp.body
     assert 'Additional keys are available: foo' in resp.body
 
+    # check formdef listing
+    FormDef.wipe()
+    formdef = FormDef()
+    formdef.name = 'test data source'
+    formdef.fields = [
+        fields.ItemField(id='1', label='item', data_source={'type': data_source.slug})
+    ]
+    formdef.store()
+
+    resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id)
+    assert 'Usage in forms' in resp.body
+    assert '/backoffice/forms/%s/' % formdef.id in resp.body
+
 def test_data_sources_edit(pub):
     create_superuser(pub)
     NamedDataSource.wipe()
diff --git a/wcs/admin/data_sources.py b/wcs/admin/data_sources.py
index 71f93fc00..134a9d749 100644
--- a/wcs/admin/data_sources.py
+++ b/wcs/admin/data_sources.py
@@ -21,9 +21,11 @@ from quixote.html import TemplateIO, htmltext
 from qommon import _
 from qommon import template
 from qommon.form import *
+from qommon.misc import json_response
 from qommon.backoffice.menu import html_top
 from wcs.data_sources import (NamedDataSource, DataSourceSelectionWidget,
         get_structured_items)
+from wcs.formdef import FormDef
 
 class NamedDataSourceUI(object):
     def __init__(self, datasource):
@@ -130,6 +132,20 @@ class NamedDataSourcePage(Directory):
                 templates=['wcs/backoffice/data-sources.html'],
                 context={'view': self, 'datasource': self.datasource})
 
+    def usage_in_formdefs(self):
+        formdefs = []
+        for formdef in FormDef.select(ignore_errors=True, ignore_migration=True, order_by='name'):
+            for field in (formdef.fields or []):
+                data_source = getattr(field, 'data_source', None)
+                if not data_source:
+                    continue
+                if data_source.get('type') == self.datasource.slug:
+                    formdefs.append(formdef)
+                    break
+            else:
+                continue
+        return formdefs
+
     def preview_block(self):
         if self.datasource.data_source.get('type') not in ('json', 'formula'):
             return ''
diff --git a/wcs/formdef.py b/wcs/formdef.py
index 26562830a..bb153e90b 100644
--- a/wcs/formdef.py
+++ b/wcs/formdef.py
@@ -518,6 +518,10 @@ class FormDef(StorableObject):
         base_url = get_publisher().get_frontoffice_url()
         return '%s/api/forms/%s/' % (base_url, self.url_name)
 
+    def get_admin_url(self):
+        base_url = get_publisher().get_backoffice_url()
+        return '%s/forms/%s/' % (base_url, self.id)
+
     def get_backoffice_submission_url(self):
         base_url = get_publisher().get_backoffice_url() + '/submission'
         return '%s/%s/' % (base_url, self.url_name)
diff --git a/wcs/templates/wcs/backoffice/data-sources.html b/wcs/templates/wcs/backoffice/data-sources.html
index d6376a54d..0ce631c6f 100644
--- a/wcs/templates/wcs/backoffice/data-sources.html
+++ b/wcs/templates/wcs/backoffice/data-sources.html
@@ -27,6 +27,18 @@
   {% endif %}
 </ul>
 {{ view.preview_block|safe }}
+
+{% with view.usage_in_formdefs as formdefs %}
+  {% if formdefs %}
+  <h3>{% trans "Usage in forms" %}</h3>
+  <ul class="biglist">
+  {% for formdef in formdefs %}
+    <li><a href="{{ formdef.get_admin_url }}">{{ formdef.name }}</a></li>
+  {% endfor %}
+  </ul>
+  {% endif %}
+{% endwith %}
+
 {% else %}
 <p class="infonotice">{% trans "Not configured" %}</p>
 {% endif %}
-- 
2.23.0.rc0

