Example #1
0
class UserCreateForm(BaseUserAdminForm):

    password = StringField(
        _l("Password"),
        description=_l("If empty a random password will be generated."),
        widget=widgets.PasswordInput(autocomplete="off"),
    )
Example #2
0
class BaseUserAdminForm(Form):

    email = StringField(
        _l("Email"),
        description=_l("Users log in with their email address."),
        view_widget=widgets.EmailWidget(),
        filters=(strip,),
        validators=[required()],
    )
    first_name = StringField(
        _l("First Name"),
        description=_l("ex: John"),
        filters=(strip,),
        validators=[required()],
    )
    last_name = StringField(
        _l("Last Name"),
        description=_l("ex: Smith"),
        filters=(strip,),
        validators=[required()],
    )

    can_login = BooleanField(
        _l("Login enabled"),
        description=_l("If unchecked, user will not be able to connect."),
        widget=widgets.BooleanWidget(),
    )

    groups = QuerySelect2Field(
        _l("Groups"),
        validators=(optional(),),
        multiple=True,
        collection_class=set,
        query_factory=lambda: Group.query.order_by(sa.sql.func.lower(Group.name).asc()),
        get_label="name",
    )

    roles = Select2MultipleField(
        _l("Roles"),
        description=_l(
            "Prefer groups to manage access rights. Directly assigning roles "
            "to users is possible but discouraged."
        ),
        choices=lambda: [(r.name, r.label) for r in Role.assignable_roles()],
    )

    password = StringField(
        _l("New Password"),
        description=_l("If empty the current password will not be changed."),
        widget=widgets.PasswordInput(autocomplete="off"),
    )
Example #3
0
class UserAdminForm(BaseUserAdminForm):

    confirm_password = StringField(
        _l(u'Confirm new password'),
        widget=widgets.PasswordInput(autocomplete='off'))

    def validate_password(self, field):
        pwd = field.data
        confirmed = self['confirm_password'].data

        if pwd != confirmed:
            raise ValidationError(
                _(u'Passwords differ. Ensure you have typed same password in both'
                  u' "password" field and "confirm password" field.'))
Example #4
0
class UserPreferencesForm(Form):

    password = StringField(_l(u'New Password'),
                           widget=widgets.PasswordInput(autocomplete='off'))
    confirm_password = StringField(
        _l(u'Confirm new password'),
        widget=widgets.PasswordInput(autocomplete='off'))

    photo = fields.FileField(label=_l('Photo'),
                             widget=widgets.ImageInput(width=55, height=55))

    locale = fields.LocaleSelectField(
        label=_l(u'Preferred Language'),
        validators=(validators.required(), ),
        default=lambda: get_default_locale(),
    )

    timezone = fields.TimezoneField(
        label=_l(u'Time zone'),
        validators=(validators.required(), ),
        default=babel.dates.LOCALTZ,
    )

    def validate_password(self, field):
        pwd = field.data
        confirmed = self['confirm_password'].data

        if pwd != confirmed:
            raise ValidationError(
                _(u'Passwords differ. Ensure you have typed same password in both'
                  u' "password" field and "confirm password" field.'))

    def validate_photo(self, field):
        data = request.form.get(field.name)
        if not data:
            return

        data = field.data
        filename = data.filename
        valid = any(filename.lower().endswith(ext)
                    for ext in ('.png', '.jpg', '.jpeg'))

        if not valid:
            raise ValidationError(
                _(u'Only PNG or JPG image files are accepted'))

        img_type = imghdr.what('ignored', data.read())

        if img_type not in ('png', 'jpeg'):
            raise ValidationError(
                _(u'Only PNG or JPG image files are accepted'))

        data.seek(0)
        try:
            # check this is actually an image file
            im = PIL.Image.open(data)
            im.load()
        except:
            raise ValidationError(_(u'Could not decode image file'))

        # convert to jpeg
        # FIXME: better do this at model level?
        jpeg = BytesIO()
        im.convert('RGBA').save(jpeg, 'JPEG')
        field.data = jpeg.getvalue()
Example #5
0
class UserPreferencesForm(Form):

    password = StringField(_l("New Password"),
                           widget=widgets.PasswordInput(autocomplete="off"))
    confirm_password = StringField(
        _l("Confirm new password"),
        widget=widgets.PasswordInput(autocomplete="off"))

    photo = fields.FileField(label=_l("Photo"),
                             widget=widgets.ImageInput(width=55, height=55))

    locale = fields.LocaleSelectField(
        label=_l("Preferred Language"),
        validators=[required()],
        default=lambda: get_default_locale(),
    )

    timezone = fields.TimezoneField(label=_l("Time zone"),
                                    validators=(required(), ),
                                    default=babel.dates.LOCALTZ)

    def validate_password(self, field: StringField) -> None:
        pwd = field.data
        confirmed = self["confirm_password"].data

        if pwd != confirmed:
            raise ValidationError(
                _("Passwords differ. Ensure you have typed same password "
                  'in both "password" field and "confirm password" field.'))

    def validate_photo(self, field: FileField) -> None:
        data = request.form.get(field.name)
        if not data:
            return

        data = field.data
        filename = data.filename
        valid = any(filename.lower().endswith(ext)
                    for ext in (".png", ".jpg", ".jpeg"))

        if not valid:
            raise ValidationError(
                _("Only PNG or JPG image files are accepted"))

        img_type = imghdr.what("ignored", data.read())

        if img_type not in ("png", "jpeg"):
            raise ValidationError(
                _("Only PNG or JPG image files are accepted"))

        data.seek(0)
        try:
            # check this is actually an image file
            im = PIL.Image.open(data)
            im.load()
        except Exception:
            raise ValidationError(_("Could not decode image file"))

        # convert to jpeg
        # FIXME: better do this at model level?
        jpeg = BytesIO()
        im.convert("RGB").save(jpeg, "JPEG")
        field.data = jpeg.getvalue()