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='用户名已存在')
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