class Role(db.Model): # 第五课内容 __tablename__ = 'roles' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) index = db.Column(db.String(64)) default = db.Column(db.Boolean, default=False, index=True) permissions = db.Column(db.Integer) users = db.relationship('User', backref='role', lazy='dynamic') @staticmethod def insert_roles(): roles = { 'User': (Permission.GENERAL, 'main', True), 'Administrator': ( Permission.ADMINISTER, 'admin', False # grants all permissions ) } for r in ["User", "Administrator"]: role = Role.query.filter_by(name=r).first() if role is None: role = Role(name=r) role.permissions = roles[r][0] role.index = roles[r][1] role.default = roles[r][2] db.session.add(role) db.session.commit() def __repr__(self): return '<Role \'%s\'>' % self.name
class Cookie(UserMixin, db.Model): # 第二课增加内容 __tablename__ = 'cookies' # 这是我们将来建出来的表的表名,在这里定义,下面的都是字段名和字段类型长度这些 cookie_id = db.Column(db.BigInteger, primary_key=True, autoincrement=True) accnt_name = db.Column(db.String(128), index=True) # 用户名 ck_content = db.Column(db.String(5000)) # cookie内容 def __init__(self, **kwargs): # 第五课内容 super(Cookie, self).__init__(**kwargs) print(self)
class RepositoryModel(db.Model): """ Classe que representa a tabela de repositórios do Github Como não há uma especificação dos atributos necessários, Salvei apenas as informações essenciais para um repositório. """ __tablename__ = "repositories" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False, unique=True) url = db.Column(db.String(100), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey("users.id"), nullable=False) @classmethod def find_by_id(cls, id): return cls.query.find_by_id(id=id).first() @classmethod def find_by_name(cls, name): return cls.query.filter_by(name=name).first() @classmethod def get_all_repositories(cls): return cls.query.all() def save(self): db.session.add(self) db.session.commit() def delete(self): db.session.remove(self) db.session.commit()
class UserModel(db.Model): """ Mapeamento da tabela de usuários do banco de dados. """ __tablename__ = "users" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(40), nullable=False, unique=True) # Cria o relacionamento com a tabela de Repositórios e efetua o join de forma # dinâmica quando acessamos este atributo repositories = db.relationship('RepositoryModel', lazy="dynamic") @classmethod def find_by_id(cls, id): return cls.query.filter_by(id=id).first() @classmethod def find_by_name(cls, name): return cls.query.filter_by(name=name).first() @classmethod def get_all_users(cls): # Aqui é retirado o atributo repositories, pois não queremos listar todos os repositórios de todos # usuários return cls.query.with_entities(UserModel.id, UserModel.name).all() def save(self): db.session.add(self) db.session.commit() def delete(self): db.session.remove(self) db.session.commit()
class User(UserMixin, db.Model): # 第二课增加内容 __tablename__ = 'users' # 这是我们将来建出来的表的表名,在这里定义,下面的都是字段名和字段类型长度这些 id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True) email = db.Column(db.String(64), unique=True, index=True) password_hash = db.Column(db.String(128)) role_id = db.Column(db.Integer, db.ForeignKey('roles.id'), default=1) # 第五课内容 def __init__(self, **kwargs): # 第五课内容 super(User, self).__init__(**kwargs) print(self) if self.role is None: if self.username == current_app.config[ 'ADMIN_USER'] or self.email == current_app.config[ 'ADMIN_EMAIL']: self.role = Role.query.filter_by( permissions=Permission.ADMINISTER).first() if self.role is None: self.role = Role.query.filter_by(default=True).first() def can(self, permissions): # 第五课内容 return self.role is not None and \ (self.role.permissions & permissions) == permissions def is_admin(self): # 第五课内容 return self.can(Permission.ADMINISTER) # 脱敏 @property def password(self): # 第二课增加内容 raise AttributeError('`password` is not a readable attribute') # 加密 @password.setter def password(self, password): # 第二课增加内容 self.password_hash = generate_password_hash(password) # 校验密码 def verify_password(self, password): # 第二课增加内容 return check_password_hash(self.password_hash, password) # 查询返回的格式 def __repr__(self): # 第二课增加内容 return '<User \'%s\'>' % self.username
class User(UserMixin, db.Model): #第二课增加内容 __tablename__ = 'users' #这是我们将来建出来的表的表名,在这里定义,下面的都是字段名和字段类型长度这些 id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True) email = db.Column(db.String(64), unique=True, index=True) password_hash = db.Column(db.String(128)) #脱敏 @property def password(self): #第二课增加内容 raise AttributeError('`password` is not a readable attribute') #加密 @password.setter def password(self, password): #第二课增加内容 self.password_hash = generate_password_hash(password) #校验密码 def verify_password(self, password): #第二课增加内容 return check_password_hash(self.password_hash, password) def __repr__(self): #第二课增加内容 return '<User \'%s\'>' % self.username
class Room(UserMixin, db.Model): # 第二课增加内容 __tablename__ = 'rooms' # 这是我们将来建出来的表的表名,在这里定义,下面的都是字段名和字段类型长度这些 room_id = db.Column(db.BigInteger, primary_key=True, autoincrement=False) accnt_name = db.Column(db.String(128), db.ForeignKey("cookies.accnt_name")) # 用户名 start_time = db.Column(db.DateTime) # 直播开始时间 finsh_time = db.Column(db.DateTime) # 直播结束时间 ttl_per_nm = db.Column(db.Integer) # 进入直播间总人数 max_oln_nm = db.Column(db.Integer) # 最大在线人数 avg_oln_nm = db.Column(db.Float) # 平均在线人数 prd_cmt_nm = db.Column(db.Integer) # 评论数 prd_lik_nm = db.Column(db.Integer) # 点赞数 inc_fan_nm = db.Column(db.Integer) # 新增粉丝数 fan_per_rt = db.Column(db.Float) # 进粉率=新增粉丝数/进入直播间总人数 avg_wth_tm = db.Column(db.Time) # 人均观看时长 prd_shw_nm = db.Column(db.Integer) # 商品曝光人数 prd_clk_nm = db.Column(db.Integer) # 商品点击人数 pay_per_nm = db.Column(db.Integer) # 成交人数 prd_oln_nm = db.Column(db.Integer) # 上架商品数 pay_ord_nm = db.Column(db.Integer) # 成交订单量 prd_shw_rt = db.Column(db.Float) # 商品点击率 = 商品点击人数/商品曝光人数 pay_per_rt = db.Column(db.Float) # 曝光转化率 = 成交人数/进入直播间总人数 pay_prd_nm = db.Column(db.Integer) # 成交商品数:需要统计有成交量的商品数 pay_moneys = db.Column(db.Float) # 成交金额 person_val = db.Column(db.Float) # 人均价值 = 成交金额/进入直播间总人数 ord_per_rt = db.Column(db.Float) # 订单创建率 = 创建订单数/进入直播间总人数 ord_pay_rt = db.Column(db.Float) # 创建成交率 = 成交人数/创建订单数 nat_cmd_fd = db.Column(db.Float) # 自然推荐-推荐feed nat_fcs_tb = db.Column(db.Float) # 关注tab nat_search = db.Column(db.Float) # 搜索 nat_living = db.Column(db.Float) # 自然推荐-直播广场 nat_sam_ct = db.Column(db.Float) # 自然推荐-同城feed nat_others = db.Column(db.Float) # 其他 nat_per_hm = db.Column(db.Float) # 个人主页 nat_sht_fl = db.Column(db.Float) # 短视频引流 nat_cmd_ot = db.Column(db.Float) # 自然推荐-其他 nat_dy_mal = db.Column(db.Float) # 抖音商城 pay_sm_cmd = db.Column(db.Float) # 小店随心推 pay_ths_pc = db.Column(db.Float) # 千川PC版 pay_dou_ad = db.Column(db.Float) # DOU+广告 pay_oth_ad = db.Column(db.Float) # 其他竞价广告 pay_top_lv = db.Column(db.Float) # 品牌广告-toplive pay_top_ot = db.Column(db.Float) # 品牌广告-其他 def __init__(self, **kwargs): # 第五课内容 super(Room, self).__init__(**kwargs) print(self)