Projet

Général

Profil

0001-fields-warning-if-prefill-with-email-on-non-Email-fi.patch

Lauréline Guérin, 03 septembre 2020 15:44

Télécharger (5,24 ko)

Voir les différences:

Subject: [PATCH] fields: warning if prefill with email on non Email field
 (#30742)

 tests/test_forms_admin_pages.py | 26 ++++++++++++++++++++++----
 wcs/admin/fields.py             | 27 ++++++++++++++++++++-------
 2 files changed, 42 insertions(+), 11 deletions(-)
tests/test_forms_admin_pages.py
1263 1263
    assert '<legend>Additional parameters</legend>' in resp.text
1264 1264
    assert '>Prefill</label>' in resp.text
1265 1265
    # check the "prefill" field is under additional parameters
1266
    assert resp.text.index('<legend>Additional parameters</legend>') < \
1267
            resp.text.index('>Prefill</label>')
1266
    assert resp.text.index('<legend>Additional parameters</legend>') < resp.text.index('>Prefill</label>')
1268 1267

  
1269 1268
    # complete the "prefill" field
1270 1269
    resp.forms[0]['prefill$type'] = 'String / Template'
......
1281 1280
    assert '<legend>Additional parameters</legend>' in resp.text
1282 1281
    assert '>Data Source</label>' in resp.text
1283 1282
    # check the "data source" field is under additional parameters
1284
    assert resp.text.index('<legend>Additional parameters</legend>') < \
1285
            resp.text.index('>Data Source</label>')
1283
    assert resp.text.index('<legend>Additional parameters</legend>') < resp.text.index('>Data Source</label>')
1286 1284

  
1287 1285
    # start filling the "data source" field
1288 1286
    resp.forms[0]['data_source$type'] = 'json'
......
1294 1292
    resp = resp.click('Edit', href='1/')
1295 1293
    assert resp.text.index('<legend>Additional parameters</legend>') > resp.text.index('>Data Source</label>')
1296 1294

  
1295
    resp = app.get('/backoffice/forms/1/fields/1/')
1296
    assert resp.forms[0]['label'].value == '1st field'
1297
    resp.forms[0]['prefill$type'] = 'User Field'
1298
    resp.forms[0]['prefill$value_user'] = 'Email (builtin)'
1299
    resp = resp.forms[0].submit('submit')
1300
    assert resp.location == 'http://example.net/backoffice/forms/1/fields/#itemId_1'
1301
    resp = resp.follow()
1302
    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
1303

  
1304
    formdef.fields += [fields.EmailField(id='2', label='2nd field')]
1305
    formdef.store()
1306
    resp = app.get('/backoffice/forms/1/fields/2/')
1307
    assert resp.forms[0]['label'].value == '2nd field'
1308
    resp.forms[0]['prefill$type'] = 'User Field'
1309
    resp.forms[0]['prefill$value_string'] = 'email'
1310
    resp = resp.forms[0].submit('submit')
1311
    assert resp.location == 'http://example.net/backoffice/forms/1/fields/#itemId_2'
1312
    resp = resp.follow()
1313
    assert "Are you sure you want to prefill" not in resp.text
1314

  
1297 1315

  
1298 1316
def test_form_prefill_field(pub):
1299 1317
    create_superuser(pub)
wcs/admin/fields.py
16 16
# You should have received a copy of the GNU General Public License
17 17
# along with this program; if not, see <http://www.gnu.org/licenses/>.
18 18

  
19
import copy
20

  
19 21
from quixote import redirect
20 22
from quixote.directory import Directory
21 23
from quixote.html import TemplateIO, htmltext, htmlescape
......
23 25
from wcs.qommon import _
24 26
from wcs.qommon.form import *
25 27
from wcs.qommon import errors, misc
28
from wcs.qommon import get_cfg
26 29
from wcs.qommon.backoffice.menu import html_top
27 30
from wcs.qommon.admin.menu import command_icon
28 31

  
29 32
from wcs import fields
30 33
from wcs.formdef import FormDef
31
from wcs.fields import get_field_types, get_field_options
32
import copy
34
from wcs.fields import get_field_options
33 35

  
34 36

  
35 37
class FieldDefPage(Directory):
......
85 87
            r += htmltext('<h2>%s</h2>') % misc.ellipsize(self.field.unhtmled_label, 80)
86 88
            r += form.render()
87 89
            return r.getvalue()
88
        else:
89
            self.submit(form)
90
            if form.get_widget('items') is None and self.field.type == 'item':
91
                return redirect('.')
92
            return redirect('../#itemId_%s' % self.field.id)
90

  
91
        self.submit(form)
92

  
93
        prefill_type = self.field.prefill.get('type') if self.field.prefill else None
94
        prefill_value = self.field.prefill.get('value') if self.field.prefill else None
95
        users_cfg = get_cfg('users', {})
96
        field_email = users_cfg.get('field_email') or 'email'
97
        if self.field.key != 'email' and prefill_type == 'user' and prefill_value == field_email:
98
            get_session().message = (
99
                'warning',
100
                _("\"%s\" is not an email field. Are you sure you want to prefill it with user's email ?")
101
                % self.field.label)
102

  
103
        if form.get_widget('items') is None and self.field.type == 'item':
104
            return redirect('.')
105
        return redirect('../#itemId_%s' % self.field.id)
93 106

  
94 107
    def submit(self, form):
95 108
        for f in self.field.get_admin_attributes():
96
-