password field: add a flag to disable confirmation input (#8111)

This commit is contained in:
Thomas NOËL 2015-08-26 16:31:54 +02:00
parent 0b295c86ce
commit 2b4ff0ced6
3 changed files with 22 additions and 8 deletions

View File

@ -146,6 +146,11 @@ def test_passwordentry_widget_success():
mock_form_submission(req, widget, {'test$pwd1': 'foo', 'test$pwd2': 'foo'})
assert widget.parse() == {'cleartext': 'foo'}
widget = PasswordEntryWidget('test', formats=['cleartext'], confirmation=False)
req.form = {}
mock_form_submission(req, widget, {'test$pwd1': 'foo'})
assert widget.parse() == {'cleartext': 'foo'}
def test_passwordentry_widget_errors():
# mismatch

View File

@ -1655,11 +1655,12 @@ class PasswordField(WidgetField):
count_lowercase = 0
count_digit = 0
count_special = 0
confirmation = True
confirmation_title = None
formats = ['sha1']
extra_attributes = ['formats', 'min_length', 'max_length',
'count_uppercase', 'count_lowercase', 'count_digit',
'count_special', 'confirmation_title']
'count_special', 'confirmation', 'confirmation_title']
widget_class = PasswordEntryWidget
@ -1691,6 +1692,9 @@ class PasswordField(WidgetField):
form.add(IntWidget, 'count_special',
title=_('Minimum number of special characters'),
value=self.count_special)
form.add(CheckboxWidget, 'confirmation',
title=_('Add a confirmation input'),
value=self.confirmation)
form.add(StringWidget, 'confirmation_title', size=50,
title=_('Label for confirmation input'),
value=self.confirmation_title)

View File

@ -1842,6 +1842,7 @@ class PasswordEntryWidget(CompositeWidget):
count_lowercase = 0
count_digit = 0
count_special = 0
confirmation = True
def __init__(self, name, value=None, **kwargs):
# hint will be displayed with pwd1 widget
@ -1853,6 +1854,7 @@ class PasswordEntryWidget(CompositeWidget):
self.count_lowercase = kwargs.get('count_lowercase', 0)
self.count_digit = kwargs.get('count_digit', 0)
self.count_special = kwargs.get('count_special', 0)
self.confirmation = kwargs.get('confirmation', True)
confirmation_title = kwargs.get('confirmation_title') or _('Confirmation')
self.formats = kwargs.get('formats', ['sha1'])
@ -1862,9 +1864,10 @@ class PasswordEntryWidget(CompositeWidget):
required=kwargs.get('required', False),
autocomplete='off',
hint=hint)
self.add(PasswordWidget, name='pwd2', title=confirmation_title,
required=kwargs.get('required', False),
autocomplete='off')
if self.confirmation:
self.add(PasswordWidget, name='pwd2', title=confirmation_title,
required=kwargs.get('required', False),
autocomplete='off')
else:
encoded_value = base64.encodestring(json.dumps(value))
if value:
@ -1912,7 +1915,6 @@ $(function() {
request.form.get('%s$encoded' % self.name)))
return
pwd1 = self.get('pwd1') or ''
pwd2 = self.get('pwd2') or ''
if not self.get_widget('pwd1'):
# we are in read-only mode, stop here.
@ -1955,9 +1957,12 @@ $(function() {
'Password must contain at least %(count)d special characters.',
count) % {'count': count})
if pwd1 != pwd2:
self.get_widget('pwd2').set_error(_('Passwords do not match'))
pwd1 = None
if self.confirmation:
pwd2 = self.get('pwd2') or ''
if pwd1 != pwd2:
self.get_widget('pwd2').set_error(_('Passwords do not match'))
pwd1 = None
if set_errors:
self.get_widget('pwd1').set_error(' '.join(set_errors))
pwd1 = None