From f0db601e8a7be4c9e219e1740f8fcb2aa05d3ebf Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Sun, 31 Oct 2021 14:22:14 +0100
Subject: [PATCH] backoffice: add direct links from preview to page fields
 (#8882)

---
 tests/admin_pages/test_form.py       | 33 +++++++++++++++++++++++++---
 wcs/admin/forms.py                   | 16 ++++++++++----
 wcs/qommon/static/css/dc2/admin.scss |  7 ++++++
 3 files changed, 49 insertions(+), 7 deletions(-)

diff --git a/tests/admin_pages/test_form.py b/tests/admin_pages/test_form.py
index 81437b5a7..08fc20599 100644
--- a/tests/admin_pages/test_form.py
+++ b/tests/admin_pages/test_form.py
@@ -1317,7 +1317,7 @@ def test_form_new_field(pub):
 
     # check it's in the preview
     resp = app.get('/backoffice/forms/1/')
-    assert '<h3 data-field-id="1">baz</h3>' in resp.text
+    assert resp.pyquery('.form-preview h3').text() == 'baz'
 
 
 def test_form_field_without_label(pub):
@@ -2366,7 +2366,7 @@ def test_form_edit_field_warnings(pub):
     formdef.fields.append(fields.PageField(id='1000', label='page', type='page'))
     formdef.store()
     resp = app.get('/backoffice/forms/1/')
-    resp = resp.click(href='fields/')
+    resp = resp.click(href='fields/', index=0)
     assert 'more than 500 fields' not in resp.text
     assert 'first field should be of type "page"' in resp.text
 
@@ -2375,7 +2375,7 @@ def test_form_edit_field_warnings(pub):
     )
     formdef.store()
     resp = app.get('/backoffice/forms/1/')
-    resp = resp.click(href='fields/')
+    resp = resp.click(href='fields/', index=0)
     assert 'more than 500 fields' in resp.text
     assert 'first field should be of type "page"' in resp.text
 
@@ -3049,3 +3049,30 @@ def test_form_category_management_roles(pub, backoffice_user, backoffice_role):
     resp = app.get(formdata.get_backoffice_url())
     assert 'inspect' in resp.text
     resp = app.get(formdata.get_backoffice_url() + 'inspect')
+
+
+def test_form_preview_edit_page_fields(pub):
+    create_superuser(pub)
+    create_role(pub)
+
+    FormDef.wipe()
+    formdef = FormDef()
+    formdef.name = 'form title'
+    formdef.fields = [
+        fields.PageField(id='1', label='first page', type='page'),
+        fields.StringField(id='2', label='a field', type='string'),
+        fields.StringField(id='3', label='another field', type='string'),
+        fields.PageField(id='4', label='second page', type='page'),
+    ]
+    formdef.store()
+    formdef.data_class().wipe()
+
+    app = login(get_app(pub))
+    resp = app.get('/backoffice/forms/%s/' % formdef.id)
+    assert resp.pyquery('fieldset.formpage a')
+    assert [x.attrib['href'] for x in resp.pyquery('fieldset.formpage a')] == [
+        'fields/pages/1/',
+        'fields/pages/4/',
+    ]
+    resp = resp.click('edit page fields', index=0)
+    assert '<h2>form title - page 1 - first page</h2>' in resp.text
diff --git a/wcs/admin/forms.py b/wcs/admin/forms.py
index 0087d4eb3..7cc294d78 100644
--- a/wcs/admin/forms.py
+++ b/wcs/admin/forms.py
@@ -848,7 +848,9 @@ class FormDefPage(Directory):
         r += htmltext('</div>')
 
         r += htmltext('<div class="bo-block clear">')
-        r += htmltext('<h3 class="clear">%s <span class="change">(<a href="fields/">%s</a>)</span></h3>') % (
+        r += htmltext(
+            '<h3 class="clear">%s <span class="change"><a class="pk-button" href="fields/">%s</a></span></h3>'
+        ) % (
             _('Fields'),
             _('edit'),
         )
@@ -1162,8 +1164,7 @@ class FormDefPage(Directory):
         form = Form(action='#', use_tokens=False)
         form.attrs['data-backoffice-preview'] = 'on'
         on_page = 0
-        for i, field in enumerate(self.formdef.fields):
-            field.id = i
+        for field in self.formdef.fields:
             if getattr(field, 'add_to_form', None):
                 try:
                     get_request().disable_error_notifications = True
@@ -1184,8 +1185,15 @@ class FormDefPage(Directory):
                     form.widgets.append(HtmlWidget('<fieldset class="formpage">'))
                     on_page += 1
                     form.widgets.append(
-                        HtmlWidget('<legend>%s %s</legend>' % (_('Page #%s:') % on_page, field.label))
+                        HtmlWidget('<legend><span>%s — %s</span> ' % (_('Page #%s:') % on_page, field.label))
+                    )
+                    form.widgets.append(
+                        HtmlWidget(
+                            '<a class="pk-button" href="%s">%s</a>'
+                            % ('fields/pages/%s/' % field.id, _('edit page fields'))
+                        )
                     )
+                    form.widgets.append(HtmlWidget('</legend>'))
                 elif field.key == 'title':
                     form.widgets.append(HtmlWidget('<h3>%s</h3>' % field.label))
                 elif field.key == 'subtitle':
diff --git a/wcs/qommon/static/css/dc2/admin.scss b/wcs/qommon/static/css/dc2/admin.scss
index 96b097eee..f50d0aec8 100644
--- a/wcs/qommon/static/css/dc2/admin.scss
+++ b/wcs/qommon/static/css/dc2/admin.scss
@@ -852,6 +852,13 @@ div.splitcontent-right {
 fieldset.formpage {
 	border: 1px solid #bcbcbc;
 	margin: 1em 0;
+	span {
+		font-weight: bold;
+	}
+	a.pk-button {
+		position: absolute;
+		right: 1em;
+	}
 }
 
 div.dataview::after {
-- 
2.33.1

