From 8ed3667f11553cfbf5c65bdd47e29a8e52bb3879 Mon Sep 17 00:00:00 2001 From: Thomas NOEL Date: Wed, 26 Aug 2015 16:31:54 +0200 Subject: [PATCH] password field can disable confirmation input (#8111) --- tests/test_widgets.py | 5 +++++ wcs/fields.py | 6 +++++- wcs/qommon/form.py | 19 ++++++++++++------- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/tests/test_widgets.py b/tests/test_widgets.py index 599bce7..b6f33c9 100644 --- a/tests/test_widgets.py +++ b/tests/test_widgets.py @@ -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 diff --git a/wcs/fields.py b/wcs/fields.py index 8faf0b7..9225cc3 100644 --- a/wcs/fields.py +++ b/wcs/fields.py @@ -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) diff --git a/wcs/qommon/form.py b/wcs/qommon/form.py index 754b044..7897b5c 100644 --- a/wcs/qommon/form.py +++ b/wcs/qommon/form.py @@ -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 -- 2.5.0