Beispiel #1
0
class UserForm(Base):
    password = StringField(validators=[
        DataRequired('密码不能为空'),
        Length(min=6, max=20, message='密码长度需要在[6,20]之间')
    ])
    name = StringField(validators=[
        DataRequired('用户名不能为空'),
        Length(min=3, max=11, message='用户名长度需要在[3,11]之间')
    ])
Beispiel #2
0
class RegisterForm(UserForm):
    class_name = StringField(validators=[DataRequired('班级不能为空')])
    email = StringField(validators=[Email('邮箱格式不正确')])
    re_password = StringField(validators=[
        DataRequired('请重复密码'),
        Length(min=6, max=20, message='新密码长度需要在[6,20]之间')
    ])

    def validate_name(self, value):
        user = User.query.filter_by(False, name=value.data).first()
        if user:
            raise ValidationError(message='用户名已被注册')

    def validate_re_password(self, value):
        if 'password' not in self.errors.keys():
            if self.password.data != value.data:
                raise ValidationError(message='与密码不一致')

    def register(self):
        with db.auto_commit():
            user = User(name=self.name.data,
                        password=self.password.data,
                        class_name=self.class_name.data,
                        email=self.email.data)
            db.session.add(user)
Beispiel #3
0
class NameForm(Base):
    name = StringField(validators=[
        DataRequired('用户名不能为空'),
        Length(min=3, max=11, message='用户名长度需要在[3,11]之间')
    ])

    def validate_name(self, value):
        if User.check_new_name(value.data):
            raise ValidationError(message='用户名已存在')
Beispiel #4
0
class SearchBookForm(Base):
    keyword = StringField(validators=[
        DataRequired('关键字不能为空'),
        Length(max=20, message='关键字长度不能超过20')
    ])

    def search(self):
        books = Books.query.filter(
            or_(Books.title.ilike("%" + self.keyword.data + "%"), \
                Books.author.ilike("%" + self.keyword.data + "%"), )
        ).order_by(Books.id.asc()).all()
        data = jsonify(books).json
        return data
Beispiel #5
0
class UpdateTitleForm(FileIDForm):
    title = StringField(validators=[
        DataRequired('文件名不能为空'),
        Length(max=100, message='文件名长度不能超过20')
    ])

    def validate_title(self, value):
        file = File.query.filter_by(o=False, title=value.data).first()
        if file:
            raise ValidationError(message='filename is exist')

    def update_title(self):
        file = File.query.filter_by(id=self.file_id.data).first_or_404()
        with db.auto_commit():
            file.title = self.title.data
Beispiel #6
0
class UpdatePasswordForm(Base):
    """
    旧 新 重复新
    """
    new_password = StringField(validators=[
        DataRequired('新密码不能为空'),
        Length(min=6, max=20, message='新密码长度需要在[6,20]之间')
    ])
    re_password = StringField(validators=[
        DataRequired('请重复密码'),
        Length(min=6, max=20, message='新密码长度需要在[6,20]之间')
    ])
    old_password = StringField(validators=[
        DataRequired('原密码不能为空'),
        Length(min=6, max=20, message='原密码长度需要在[6,20]之间')
    ])

    def validate_old_password(self, value):
        user = User.query.filter_by(id=g.user.id).first_or_404()
        if user.check_pwd(value.data):
            with db.auto_commit():
                user.password = self.new_password.data
        else:
            if 'new_password' not in self.errors.keys(
            ) and 're_password' not in self.errors.keys():
                raise ValidationError(message='原密码错误')

    def validate_re_password(self, value):
        if 'new_password' not in self.errors.keys():
            if self.new_password.data != value.data:
                raise ValidationError(message='与新密码不一致')

    def update_password(self):
        user = User.query.get(g.user.id)
        with db.auto_commit():
            user.password = self.new_password.data
Beispiel #7
0
class UploadFileForm(PublicForm):
    filename = StringField(validators=[
        DataRequired('文件名不能为空'),
        Length(max=100, message='文件名长度不能超过20')
    ])

    def validate_filename(self, value):
        title, tail = spilt_point(value.data)
        file = File.query.filter_by(o=False, title=title,
                                    owner_id=g.user.id).first()
        if file:
            raise ValidationError(message='filename is exist')

    def save_file(self, file):
        user = User.query.filter_by(id=g.user.id).first_or_404()
        public = True if self.public.data == FilePublicAuthEnum.PUBLIC.value else False
        user.save_file(self.filename.data,
                       file,
                       type=SaveFileEnum.FILE.value,
                       public=public)