You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 从app导入db和login_manager对象
from app import db, login_manager
# 从flask_login导入UserMixin类提供用户认证功能
from flask_login import UserMixin
# 从werkzeug.security导入密码哈希相关函数
from werkzeug.security import generate_password_hash, check_password_hash
# 从datetime导入datetime类
from datetime import datetime
# 定义User类继承UserMixin和db.Model
class User(UserMixin, db.Model):
# 指定数据库表名
__tablename__ = 'users'
# 定义用户ID字段整数类型主键
id = db.Column(db.Integer, primary_key=True)
# 定义用户名字段,字符串类型,唯一且不能为空
username = db.Column(db.String(64), unique=True, nullable=False)
# 定义邮箱字段,字符串类型,唯一且不能为空
email = db.Column(db.String(120), unique=True, nullable=False)
# 定义密码哈希字段,字符串类型
password_hash = db.Column(db.String(255))
# 定义管理员权限字段布尔类型默认为False
is_admin = db.Column(db.Boolean, default=False)
# 定义真实姓名字段,字符串类型
real_name = db.Column(db.String(64))
# 定义电话字段,字符串类型
phone = db.Column(db.String(20))
# 定义用户状态字段,字符串类型,默认为'active'
status = db.Column(db.String(20), default='active') # active/disabled
# 定义最后登录时间字段,日期时间类型
last_login = db.Column(db.DateTime)
# 定义创建时间字段日期时间类型默认为当前UTC时间
created_at = db.Column(db.DateTime, default=datetime.utcnow)
# 定义设置密码方法,将明文密码转换为哈希值存储
def set_password(self, password):
self.password_hash = generate_password_hash(password)
# 定义检查密码方法,验证输入密码是否正确
def check_password(self, password):
return check_password_hash(self.password_hash, password)