Example #1
0
class RegisterForm(ConfirmRegisterForm, NextFormMixin):
    password_confirm = PasswordField(
        get_form_field_label("retype_password"),
        validators=[
            EqualTo("password", message="RETYPE_PASSWORD_MISMATCH"),
            validators.Optional(),
        ],
    )

    csrf_token = uuid.uuid4()

    def validate(self):
        if not super(RegisterForm, self).validate():
            return False

        if not config_value("UNIFIED_SIGNIN"):
            # password_confirm required
            if not self.password_confirm.data or not self.password_confirm.data.strip(
            ):
                self.password_confirm.errors.append(
                    get_message("PASSWORD_NOT_PROVIDED")[0])
                return False

        return True

    def __init__(self, *args, **kwargs):
        super(RegisterForm, self).__init__(*args, **kwargs)
        if not self.next.data:
            self.next.data = request.args.get("next", "")
Example #2
0
class ExtendedRegisterForm(RegisterForm):
    first_name = StringField('Nombre', [Required()])
    last_name = StringField('Apellidos', [Required()])
    telegram = StringField('Telegram', [
        validators.Optional(),
        validators.Regexp(
            '[a-zA-Z0-9_-]{5,}',
            message=u'Introduzca un usuario válido de Telegram sin @')
    ])
    year = SelectField('Curso', [Required()],
                       choices=[('1', 'Primero'), ('2', 'Segundo'),
                                ('3', 'Tercero'), ('4', 'Cuarto')])
    school = SelectField(
        'Escuela',
        choices=json_choices_centros('./static/json/centros.json'),
        id='select_school',
        default=59)
    degree = SelectField(
        'Plan de Estudios',
        choices=json_choices_planes('./static/json/planes.json'),
        id='select_degree',
        default='59EC')
    dni = StringField(
        'DNI o NIE',
        validators=[
            unique_user_dni,
            validators.Regexp(
                '[0-9A-Z][0-9]{7}[A-Z]',
                message=u'Introduzca un DNI o NIE válido en mayúsculas.'),
            Required()
        ])
    self_edit = HiddenField()
class ConfirmRegisterForm(Form, RegisterFormMixin):
    user_id = StringField(
        get_form_field_label("username"), validators=[],
    )

    username = StringField(
        get_form_field_label("username"), validators=[],
    )

    password = PasswordField(
        get_form_field_label("password"), validators=[validators.Optional()]
    )

    email = StringField(
        get_form_field_label("email"), validators=[validators.Optional()]
    )

    nickname = StringField(
        get_form_field_label("nickname"), validators=[validators.Optional()]
    )

    realname = StringField(
        get_form_field_label("realname"), validators=[validators.Optional()]
    )

    career = StringField(
        get_form_field_label("career"), validators=[validators.Optional()]
    )

    address = StringField(
        get_form_field_label("address"), validators=[validators.Optional()]
    )

    over18 = BooleanField(
        get_form_field_label("over18"), validators=[validators.Optional()]
    )

    jwt = StringField(
        get_form_field_label("jwt"), validators=[],
    )

    def validate(self):
        if not super(ConfirmRegisterForm, self).validate():
            return False

        # XXX hack with user_id data
        if not self.user_id.data and self.username.data:
            self.user_id.data = self.username.data

        # To support unified sign in - we permit registering with no password.
        if not config_value("UNIFIED_SIGNIN"):
            # password required
            if not self.password.data or not self.password.data.strip():
                self.password.errors.append(get_message("PASSWORD_NOT_PROVIDED")[0])
                return False

        if not self.password.data:
            return False

        if self.password.data:
            # We do explicit validation here for passwords
            # (rather than write a validator class) for 2 reasons:
            # 1) We want to control which fields are passed -
            #    sometimes that's current_user
            #    other times it's the registration fields.
            # 2) We want to be able to return multiple error messages.

            rfields = {}
            for k, v in self.data.items():
                if hasattr(_datastore.user_model, k):
                    rfields[k] = v
            if 'password' in rfields:
                del rfields["password"]

            pbad = _security._password_validator(self.password.data, True, **rfields)

            # validate with ptt-server

            user_id = self.user_id.data
            password = self.password.data
            ip = get_ip()

            email = self.email.data
            nickname = self.nickname.data
            realname = self.realname.data
            career = self.career.data
            address = self.address.data
            over18 = self.over18.data

            err, result = register_user(user_id, password, ip, email, nickname, realname, career, address, over18)
            if err is not None:
                self.user_id.errors = result['err']
                return False

            self.jwt.data = result

        return True