Makes the TUI validate username with the endpoint.

This commit is contained in:
Carlos Nihelton 2022-05-09 15:40:38 -03:00
parent 46347d1bcc
commit 9cb27da947
No known key found for this signature in database
GPG Key ID: 6FE346D245197E9A
2 changed files with 22 additions and 6 deletions

View File

@ -16,7 +16,7 @@
import logging
from subiquity.client.controller import SubiquityTuiController
from subiquity.common.types import IdentityData
from subiquity.common.types import IdentityData, UsernameValidation
from subiquity.ui.views import IdentityView
log = logging.getLogger('subiquity.client.controllers.identity')
@ -25,6 +25,7 @@ log = logging.getLogger('subiquity.client.controllers.identity')
class IdentityController(SubiquityTuiController):
endpoint_name = 'identity'
username_validation = UsernameValidation.OK
async def make_ui(self):
data = await self.endpoint.GET()
@ -48,3 +49,10 @@ class IdentityController(SubiquityTuiController):
"IdentityController.done next_screen user_spec=%s",
identity_data)
self.app.next_screen(self.endpoint.POST(identity_data))
async def _validate_username(self, username):
self.username_validation = \
await self.endpoint.validate_username.GET(username)
def validate_username(self, _, value):
self.app.aio_loop.create_task(self._validate_username(value))

View File

@ -35,7 +35,7 @@ from subiquitycore.utils import crypt_password
from subiquitycore.view import BaseView
from subiquity.common.resources import resource_path
from subiquity.common.types import IdentityData
from subiquity.common.types import IdentityData, UsernameValidation
log = logging.getLogger("subiquity.ui.views.identity")
@ -83,9 +83,11 @@ PasswordField = simple_field(PasswordEditor)
class IdentityForm(Form):
def __init__(self, reserved_usernames, initial):
self.reserved_usernames = reserved_usernames
def __init__(self, controller, initial):
self.controller = controller
super().__init__(initial=initial)
connect_signal(self.username.widget, 'change',
controller.validate_username)
realname = RealnameField(_("Your name:"))
hostname = UsernameField(
@ -116,6 +118,7 @@ class IdentityForm(Form):
def validate_username(self):
username = self.username.value
state = self.controller.username_validation
if len(username) < 1:
return _("Username missing")
@ -128,11 +131,16 @@ class IdentityForm(Form):
return _(
"Username must match USERNAME_REGEX: " + USERNAME_REGEX)
if username in self.reserved_usernames:
if state == UsernameValidation.SYSTEM_RESERVED:
return _(
'The username "{username}" is reserved for use by the system.'
).format(username=username)
if state == UsernameValidation.ALREADY_IN_USE:
return _(
'The username "{username}" is already in use.'
).format(username=username)
def validate_password(self):
if len(self.password.value) < 1:
return _("Password must be set")
@ -182,7 +190,7 @@ class IdentityView(BaseView):
'hostname': identity_data.hostname,
}
self.form = IdentityForm(reserved_usernames, initial)
self.form = IdentityForm(controller, initial)
connect_signal(self.form, 'submit', self.done)
setup_password_validation(self.form, _("passwords"))