class RegionForm(Form): username = UsernameField(_("Pick a username for the admin account:"), help=_("Enter the administrative username.")) password = PasswordField( _("Choose a password:"******"Please enter the password for this account.")) def validate_username(self): if len(self.username.value) < 1: return _("Username missing") if len(self.username.value) > USERNAME_MAXLEN: return _("Username too long, must be < ") + str(USERNAME_MAXLEN) if not re.match(r'[a-z_][a-z0-9_-]*', self.username.value): return _("Username must match NAME_REGEX, i.e. [a-z_][a-z0-9_-]*") def validate_password(self): if len(self.password.value) < 1: return _("Password must be set")
class SSHForm(Form): install_server = BooleanField(_("Install OpenSSH server")) ssh_import_id = ChoiceField( _("Import SSH identity:"), choices=[ (_("No"), True, None), (_("from GitHub"), True, "gh"), (_("from Launchpad"), True, "lp"), ], help=_("You can import your SSH keys from GitHub or Launchpad.")) import_username = UsernameField(_ssh_import_data[None]['caption']) pwauth = BooleanField(_("Allow password authentication over SSH")) cancel_label = _("Back") def __init__(self, initial): super().__init__(initial=initial) connect_signal( self.install_server.widget, 'change', self._toggle_server) self._toggle_server(None, self.install_server.value) def _toggle_server(self, sender, new_value): if new_value: self.ssh_import_id.enabled = True self.import_username.enabled = self.ssh_import_id.value is not None self.pwauth.enabled = self.ssh_import_id.value is not None else: self.ssh_import_id.enabled = False self.import_username.enabled = False self.pwauth.enabled = False # validation of the import username does not read from # ssh_import_id.value because it is sometimes done from the # 'select' signal of the import id selector, which is called # before the import id selector's value has actually changed. so # the signal handler stuffs the value here before doing # validation (yes, this is a hack). ssh_import_id_value = None def validate_import_username(self): if self.ssh_import_id_value is None: return username = self.import_username.value if len(username) == 0: return _("This field must not be blank.") if len(username) > SSH_IMPORT_MAXLEN: return _("SSH id too long, must be < ") + str(SSH_IMPORT_MAXLEN) if self.ssh_import_id_value == 'lp': lp_regex = r"^[a-z0-9][a-z0-9\+\.\-]*$" if not re.match(lp_regex, self.import_username.value): return _("A Launchpad username must start with a letter or " "number. All letters must be lower-case. The " "characters +, - and . are also allowed after " "the first character.""") elif self.ssh_import_id_value == 'gh': if not re.match(r'^[a-zA-Z0-9\-]+$', username): return _("A GitHub username may only contain alphanumeric " "characters or single hyphens, and cannot begin or " "end with a hyphen.")