ui: move confirmation mechanism in subiquitycore.ui

Signed-off-by: Olivier Gayot <olivier.gayot@canonical.com>
This commit is contained in:
Olivier Gayot 2022-06-24 11:18:47 +02:00
parent d63b44c014
commit 384e4b62ac
4 changed files with 22 additions and 29 deletions

View File

@ -48,9 +48,6 @@ from subiquity.ui.views.filesystem.compound import (
get_possible_components, get_possible_components,
MultiDeviceField, MultiDeviceField,
) )
from subiquity.ui.views.identity import (
setup_password_validation,
)
log = logging.getLogger('subiquity.ui.views.filesystem.lvm') log = logging.getLogger('subiquity.ui.views.filesystem.lvm')
@ -83,9 +80,9 @@ class VolGroupForm(CompoundDiskForm):
self.deleted_vg_names = deleted_vg_names self.deleted_vg_names = deleted_vg_names
super().__init__(model, possible_components, initial) super().__init__(model, possible_components, initial)
connect_signal(self.encrypt.widget, 'change', self._change_encrypt) connect_signal(self.encrypt.widget, 'change', self._change_encrypt)
setup_password_validation(field=self.passphrase, self.confirm_passphrase.use_as_confirmation(
field_confirm=self.confirm_passphrase, for_field=self.passphrase,
desc=_("Passphrases")) desc=_("Passphrases"))
self._change_encrypt(None, self.encrypt.value) self._change_encrypt(None, self.encrypt.value)
name = VGNameField(_("Name:")) name = VGNameField(_("Name:"))

View File

@ -27,7 +27,6 @@ from subiquitycore.ui.interactive import (
StringEditor, StringEditor,
) )
from subiquitycore.ui.form import ( from subiquitycore.ui.form import (
BoundFormField,
Form, Form,
simple_field, simple_field,
WantsToKnowFormField, WantsToKnowFormField,
@ -187,21 +186,6 @@ class IdentityForm(Form):
return _("Passwords do not match") return _("Passwords do not match")
def setup_password_validation(field: BoundFormField,
field_confirm: BoundFormField,
desc: str) -> None:
""" Set up an automatic check for password vs password confirmation. """
def _check_password(sender, new_text):
password = field.value
if not password.startswith(new_text):
field_confirm.show_extra(
# desc is "Passwords" or "Passphrases"
("info_error", _("{desc} do not match").format(desc=desc)))
else:
field_confirm.show_extra('')
connect_signal(field_confirm.widget, 'change', _check_password)
class IdentityView(BaseView): class IdentityView(BaseView):
title = _("Profile setup") title = _("Profile setup")
excerpt = _("Enter the username and password you will use to log in to " excerpt = _("Enter the username and password you will use to log in to "
@ -231,10 +215,11 @@ class IdentityView(BaseView):
self.form = IdentityForm(controller, initial) self.form = IdentityForm(controller, initial)
self.form.confirm_password.use_as_confirmation(
for_field=self.form.password,
desc=_("Passwords"))
connect_signal(self.form, 'submit', self.done) connect_signal(self.form, 'submit', self.done)
setup_password_validation(field=self.form.password,
field_confirm=self.form.confirm_password,
desc=_("Passwords"))
super().__init__( super().__init__(
screen( screen(

View File

@ -280,6 +280,18 @@ class BoundFormField(object):
for row in self._rows: for row in self._rows:
row.enabled = val row.enabled = val
def use_as_confirmation(self, for_field: "BoundFormField", desc: str) \
-> None:
""" Mark this field as a confirmation field for another field.
This will automatically compare the value of both fields when this
field (a.k.a., the confirmation field) is changed. """
def _check_confirmation(sender, new_text):
if not for_field.value.startswith(new_text):
self.show_extra(("info_error", _(f"{desc} do not match")))
else:
self.show_extra("")
connect_signal(self.widget, "change", _check_confirmation)
class BoundSubFormField(BoundFormField): class BoundSubFormField(BoundFormField):
def is_in_error(self): def is_in_error(self):

View File

@ -23,7 +23,6 @@ from subiquity.common.types import IdentityData
from subiquity.ui.views.identity import ( from subiquity.ui.views.identity import (
IdentityForm, IdentityForm,
IdentityView, IdentityView,
setup_password_validation,
) )
from subiquitycore.ui.utils import screen from subiquitycore.ui.utils import screen
from subiquitycore.utils import crypt_password from subiquitycore.utils import crypt_password
@ -72,9 +71,9 @@ class WSLIdentityView(BaseView):
self.form = WSLIdentityForm(controller, initial) self.form = WSLIdentityForm(controller, initial)
connect_signal(self.form, 'submit', self.done) connect_signal(self.form, 'submit', self.done)
setup_password_validation(field=self.form.password, self.form.confirm_password.use_as_confirmation(
field_confirm=self.form.confirm_password, for_field=self.form.password,
desc=_("passwords")) desc=_("Passwords"))
super().__init__( super().__init__(
screen( screen(