class Profile(BaseModel, db.Model): """个人资料""" __tablename__ = "user_profile" id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False) mobile = db.Column(db.String(11), unique=True, nullable=False) # 手机号 nickname = db.Column(db.String(32)) # 昵称 avatar = db.Column(db.String(128)) # 用户头像路径 info = db.Column(db.Text()) # 个人简介 addresses = db.relationship("Address", backref="user") products = db.relationship("Product", backref="seller") orders = db.relationship("Order", backref="buyer") cart = db.relationship("Cart", backref="user")
class ProductMedia(BaseModel, db.Model): """商品照片""" __tablename__ = "product_medias" id = db.Column(db.Integer, primary_key=True) product_id = db.Column(db.Integer, db.ForeignKey("products.id"), nullable=False) filename = db.Column(db.String(64), nullable=False) # 文件名
class AuthenticateDocuments(BaseModel, db.Model): """认证使用的文件""" __tablename__ = "user_authenticate_documents" id = db.Column(db.Integer, primary_key=True) apply_id = db.Column(db.Integer, db.ForeignKey("user_authenticates.id"), nullable=False) filename = db.Column(db.String(64), nullable=False) # 文件名
class User(BaseModel, db.Model, UserMixin): """用户""" __tablename__ = "user" id = db.Column(db.Integer, primary_key=True) # 用户编号 type = db.Column( # 账户类型 db.Enum( # 枚举 "PERSONAL", # 个人, "BUSINESS", # 商家 "ADVERTISEMENT", # 广告商 "ADMINISTRATOR", # 管理员 ), nullable=False, index=True) username = db.Column(db.String(32), unique=True, nullable=False) # 用户暱称 password_hash = db.Column(db.String(128), nullable=False) # 密码的散列值 profile = db.relationship("Profile", backref="user") # 加上property装饰器后,会把函数变为属性,属性名即为函数名 @property def password(self): """读取属性的函数行为""" raise AttributeError("这个属性只能设置,不能读取") # 使用这个装饰器, 对应设置属性操作 @password.setter def password(self, value): """ 设置属性 user.password = "" :param value: 设置属性时的数据 value就是 "", 原始的明文密码 :return: """ self.password_hash = werkzeug.security.generate_password_hash(value) def check_password(self, passwd): """ 检验密码的正确性 :param passwd: 用户登录时填写的原始密码 :return: 如果正确,返回True, 否则返回False """ return werkzeug.security.check_password_hash(self.password_hash, passwd)
class Product(BaseModel, db.Model): """商品""" __tablename__ = "products" id = db.Column(db.Integer, primary_key=True) seller_id = db.Column(db.Integer, db.ForeignKey("user_profile.user_id"), nullable=False) name = db.Column(db.String(32), nullable=False) description = db.Column(db.Text()) price = db.Column(db.Integer, nullable=False) status = db.Column( db.Enum( "SELLING", # 出售中 "SELLED" # 已出售 ), default="SELLING") attachments = db.relationship("ProductMedia", backref="product")
class Address(BaseModel, db.Model): """地址""" __tablename__ = "user_addresses" id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey("user_profile.user_id"), nullable=False) country = db.Column(db.String(32)) # 国家 province = db.Column(db.String(32)) # 省/直辖市 city = db.Column(db.String(32)) # 城市 street = db.Column(db.String(32)) # 街道 name = db.Column(db.String(32)) # 联系人姓名 phone = db.Column(db.String(32)) # 联系人电话