# Path: app/models.py
# class User(UserMixin, db.Model):
#     __tablename__ = 'users'
#     id = db.Column(db.Integer, primary_key=True)
#     username = db.Column(db.String(64), unique=True, index=True)
#     email = db.Column(db.String(64), unique=True, index=True)
#     password_hash = db.Column(db.String(128))
#     member_since = db.Column(db.DateTime(), default=datetime.utcnow)
#     last_seen = db.Column(db.DateTime(), default=datetime.utcnow)
#     status = db.Column(db.Boolean, default=False)
#     role = db.Column(db.Boolean, default=False)
#     articles = db.relationship('Article', backref='author', lazy='dynamic')
# 
#     @property
#     def password(self):
#         raise ArithmeticError('非明文密码，不可读。')
# 
#     @password.setter
#     def password(self, password):
#         self.password_hash = generate_password_hash(password=password)
# 
#     def verify_password(self, password):
#         return check_password_hash(self.password_hash, password=password)
# 
#     def is_admin(self):
#         return self.role
# 
#     def ping(self):
#         self.last_seen = datetime.utcnow()
#         db.session.add(self)
# 
#     def is_author(self):
#         return Article.query.filter_by(author_id=self.id).first()
# 
#     def __repr__(self):
#         return '<User %r>' % self.username


from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField, SelectField, TextAreaField
from wtforms.validators import DataRequired, Length, Email, Regexp, EqualTo
from wtforms import ValidationError
from ..models import User
    password = PasswordField('密码', validators=[DataRequired(), EqualTo('password2', message='密码必须一致！')])
    password2 = PasswordField('重输密码', validators=[DataRequired()])
    submit = SubmitField('注 册')

    def validate_username(self, field):
        if User.query.filter_by(username=field.data).first():
            raise ValidationError('用户名已被注册！')

    def validate_email(self, field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError('邮箱已被注册！')


class ChangePasswordForm(FlaskForm):
    old_password = PasswordField('旧密码', validators=[DataRequired()])
    password = PasswordField('密码', validators=[DataRequired(), EqualTo('password2', message='密码必须一致！')])
    password2 = PasswordField('重输密码', validators=[DataRequired()])
    submit = SubmitField('更新密码')


class AddUserForm(FlaskForm):
    username = StringField('用户名', validators=[DataRequired(), Length(1, 64, message='姓名长度要在1和64之间'),
                       Regexp('^[\u4E00-\u9FFF]+$', flags=0, message='用户名必须为中文')])
    email = StringField('邮箱', validators=[DataRequired(), Length(6, 64, message='邮件长度要在6和64之间'),
                        Email(message='邮件格式不正确！')])
    role = SelectField('权限', choices=[('True', '管理员'), ('False', '一般用户') ])
    status = SelectField('状态', choices=[('True', '正常'), ('False', '注销') ])
    submit = SubmitField('添加用户')

    def validate_username(self, field):

# Path: app/models.py
# class User(UserMixin, db.Model):
#     __tablename__ = 'users'
#     id = db.Column(db.Integer, primary_key=True)
#     username = db.Column(db.String(64), unique=True, index=True)
#     email = db.Column(db.String(64), unique=True, index=True)
#     password_hash = db.Column(db.String(128))
#     member_since = db.Column(db.DateTime(), default=datetime.utcnow)
#     last_seen = db.Column(db.DateTime(), default=datetime.utcnow)
#     status = db.Column(db.Boolean, default=False)
#     role = db.Column(db.Boolean, default=False)
#     articles = db.relationship('Article', backref='author', lazy='dynamic')
# 
#     @property
#     def password(self):
#         raise ArithmeticError('非明文密码，不可读。')
# 
#     @password.setter
#     def password(self, password):
#         self.password_hash = generate_password_hash(password=password)
# 
#     def verify_password(self, password):
#         return check_password_hash(self.password_hash, password=password)
# 
#     def is_admin(self):
#         return self.role
# 
#     def ping(self):
#         self.last_seen = datetime.utcnow()
#         db.session.add(self)
# 
#     def is_author(self):
#         return Article.query.filter_by(author_id=self.id).first()
# 
#     def __repr__(self):
#         return '<User %r>' % self.username


# Path: app/admin/forms.py
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField, SelectField, TextAreaField
from wtforms.validators import DataRequired, Length, Email, Regexp, EqualTo
from wtforms import ValidationError
from ..models import User

    password = PasswordField('密码', validators=[DataRequired(), EqualTo('password2', message='密码必须一致！')])
    password2 = PasswordField('重输密码', validators=[DataRequired()])
    submit = SubmitField('注 册')

    def validate_username(self, field):
        if User.query.filter_by(username=field.data).first():
            raise ValidationError('用户名已被注册！')

    def validate_email(self, field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError('邮箱已被注册！')


class ChangePasswordForm(FlaskForm):
    old_password = PasswordField('旧密码', validators=[DataRequired()])
    password = PasswordField('密码', validators=[DataRequired(), EqualTo('password2', message='密码必须一致！')])
    password2 = PasswordField('重输密码', validators=[DataRequired()])
    submit = SubmitField('更新密码')


class AddUserForm(FlaskForm):
    username = StringField('用户名', validators=[DataRequired(), Length(1, 64, message='姓名长度要在1和64之间'),
                       Regexp('^[\u4E00-\u9FFF]+$', flags=0, message='用户名必须为中文')])
    email = StringField('邮箱', validators=[DataRequired(), Length(6, 64, message='邮件长度要在6和64之间'),
                        Email(message='邮件格式不正确！')])
    role = SelectField('权限', choices=[('True', '管理员'), ('False', '一般用户') ])
    status = SelectField('状态', choices=[('True', '正常'), ('False', '注销') ])
    submit = SubmitField('添加用户')

    def validate_username(self, field):

        if User.query.filter_by(username=field.data).first():