From 8dc4a3e38ca2095000e557d8d41d2bc46e84fcec Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Thu, 3 Sep 2020 15:44:03 +0200
Subject: [PATCH] fields: warning if prefill with email on non Email field
 (#30742)

---
 tests/test_forms_admin_pages.py | 26 ++++++++++++++++++++++----
 wcs/admin/fields.py             | 28 +++++++++++++++++++++-------
 2 files changed, 43 insertions(+), 11 deletions(-)

diff --git a/tests/test_forms_admin_pages.py b/tests/test_forms_admin_pages.py
index 33f71249..abe38d5f 100644
--- a/tests/test_forms_admin_pages.py
+++ b/tests/test_forms_admin_pages.py
@@ -1263,8 +1263,7 @@ def test_form_edit_field_advanced(pub):
     assert '<legend>Additional parameters</legend>' in resp.text
     assert '>Prefill</label>' in resp.text
     # check the "prefill" field is under additional parameters
-    assert resp.text.index('<legend>Additional parameters</legend>') < \
-            resp.text.index('>Prefill</label>')
+    assert resp.text.index('<legend>Additional parameters</legend>') < resp.text.index('>Prefill</label>')
 
     # complete the "prefill" field
     resp.forms[0]['prefill$type'] = 'String / Template'
@@ -1281,8 +1280,7 @@ def test_form_edit_field_advanced(pub):
     assert '<legend>Additional parameters</legend>' in resp.text
     assert '>Data Source</label>' in resp.text
     # check the "data source" field is under additional parameters
-    assert resp.text.index('<legend>Additional parameters</legend>') < \
-            resp.text.index('>Data Source</label>')
+    assert resp.text.index('<legend>Additional parameters</legend>') < resp.text.index('>Data Source</label>')
 
     # start filling the "data source" field
     resp.forms[0]['data_source$type'] = 'json'
@@ -1294,6 +1292,26 @@ def test_form_edit_field_advanced(pub):
     resp = resp.click('Edit', href='1/')
     assert resp.text.index('<legend>Additional parameters</legend>') > resp.text.index('>Data Source</label>')
 
+    resp = app.get('/backoffice/forms/1/fields/1/')
+    assert resp.forms[0]['label'].value == '1st field'
+    resp.forms[0]['prefill$type'] = 'User Field'
+    resp.forms[0]['prefill$value_user'] = 'Email (builtin)'
+    resp = resp.forms[0].submit('submit')
+    assert resp.location == 'http://example.net/backoffice/forms/1/fields/'
+    resp = resp.follow()
+    assert "&quot;1st field&quot; is not an email field. Are you sure you want to prefill it with user's email?" in resp.text
+
+    formdef.fields += [fields.EmailField(id='2', label='2nd field')]
+    formdef.store()
+    resp = app.get('/backoffice/forms/1/fields/2/')
+    assert resp.forms[0]['label'].value == '2nd field'
+    resp.forms[0]['prefill$type'] = 'User Field'
+    resp.forms[0]['prefill$value_string'] = 'email'
+    resp = resp.forms[0].submit('submit')
+    assert resp.location == 'http://example.net/backoffice/forms/1/fields/#itemId_2'
+    resp = resp.follow()
+    assert "Are you sure you want to prefill" not in resp.text
+
 
 def test_form_prefill_field(pub):
     create_superuser(pub)
diff --git a/wcs/admin/fields.py b/wcs/admin/fields.py
index 583cc485..9fb90077 100644
--- a/wcs/admin/fields.py
+++ b/wcs/admin/fields.py
@@ -16,6 +16,8 @@
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, see <http://www.gnu.org/licenses/>.
 
+import copy
+
 from quixote import redirect
 from quixote.directory import Directory
 from quixote.html import TemplateIO, htmltext, htmlescape
@@ -23,13 +25,13 @@ from quixote.html import TemplateIO, htmltext, htmlescape
 from wcs.qommon import _
 from wcs.qommon.form import *
 from wcs.qommon import errors, misc
+from wcs.qommon import get_cfg
 from wcs.qommon.backoffice.menu import html_top
 from wcs.qommon.admin.menu import command_icon
 
 from wcs import fields
 from wcs.formdef import FormDef
-from wcs.fields import get_field_types, get_field_options
-import copy
+from wcs.fields import get_field_options
 
 
 class FieldDefPage(Directory):
@@ -86,11 +88,23 @@ class FieldDefPage(Directory):
             r += htmltext('<h2>%s</h2>') % misc.ellipsize(self.field.unhtmled_label, 80)
             r += form.render()
             return r.getvalue()
-        else:
-            self.submit(form)
-            if form.get_widget('items') is None and self.field.type == 'item':
-                return redirect('.')
-            return redirect('../#itemId_%s' % self.field.id)
+
+        self.submit(form)
+        if form.get_widget('items') is None and self.field.type == 'item':
+            return redirect('.')
+
+        prefill_type = self.field.prefill.get('type') if self.field.prefill else None
+        prefill_value = self.field.prefill.get('value') if self.field.prefill else None
+        users_cfg = get_cfg('users', {})
+        field_email = users_cfg.get('field_email') or 'email'
+        if self.field.key != 'email' and prefill_type == 'user' and prefill_value == field_email:
+            get_session().message = (
+                'warning',
+                _("\"%s\" is not an email field. Are you sure you want to prefill it with user's email?")
+                % self.field.label)
+            return redirect('..')
+
+        return redirect('../#itemId_%s' % self.field.id)
 
     def submit(self, form):
         for f in self.field.get_admin_attributes():
-- 
2.28.0

