class Commission(db.Model, BaseModelMixin): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) user = db.relationship('User', backref=db.backref('commission_user', lazy='dynamic'), foreign_keys=[user_id]) year = db.Column(db.Integer) rate = db.Column(db.Float) creator_id = db.Column(db.Integer, db.ForeignKey('user.id')) creator = db.relationship('User', backref=db.backref('commission_creator', lazy='dynamic'), foreign_keys=[creator_id]) create_time = db.Column(db.DateTime) __table_args__ = (db.UniqueConstraint('user_id', 'year', name='_commission_user_year'), ) __mapper_args__ = {'order_by': year.desc()} def __init__(self, user, year=None, rate=None, creator=None, create_time=None): self.user = user self.creator = creator self.year = year or datetime.datetime.now().year self.rate = rate or 0.0 self.create_time = datetime.date.today()
class Performance(db.Model, BaseModelMixin): id = db.Column(db.Integer, primary_key=True) creator_id = db.Column(db.Integer, db.ForeignKey('user.id')) creator = db.relationship('User', backref=db.backref('performance_creator', lazy='dynamic')) status = db.Column(db.Integer) year = db.Column(db.Integer) q_month = db.Column(db.String(10)) t_money = db.Column(db.Float) # 区域销售目标总计 location = db.Column(db.Integer) create_time = db.Column(db.DateTime) __mapper_args__ = {'order_by': create_time.desc()} __table_args__ = (db.UniqueConstraint( 'location', 'year', 'q_month', name='_performance_location_year_q'), ) def __init__(self, creator, year, q_month, t_money, location, create_time=None, status=None): self.creator = creator self.year = year self.q_month = q_month self.t_money = t_money self.location = location self.create_time = create_time or datetime.date.today() self.status = status or PER_STATUS_NEW @property def location_cn(self): return TEAM_LOCATION_CN[self.location]
class VenueGenre(db.Model): __tablename__ = 'venue_genre' genre_id = db.Column(db.Integer, db.ForeignKey('genres.id'), primary_key=True) venue_id = db.Column(db.Integer, db.ForeignKey('venues.id'), primary_key=True) genre = db.relationship('Genre', backref=db.backref('venue_genre', cascade='all, delete-orphan')) venue = db.relationship('Venue', backref=db.backref('venue_genre', cascade='all, delete-orphan')) __table_args__ = (db.UniqueConstraint(genre_id, venue_id), ) @classmethod def delete_old(cls, venue_id, genres): venues_to_delete = db.session.query(cls).filter_by( venue_id=venue_id).filter(cls.genre_id.notin_(genres)) venues_to_delete.delete(synchronize_session=False) @classmethod def get_genres_ids(cls, venue_id): results = cls.query.filter_by(venue_id=venue_id).all() return [str(result.genre_id) for result in results] def __repr__(self): return f'<VenreGenre venue {self.venue_id} genre {self.genre_id}>'
class LikeItem(db.Model, BaseLikeObject): __tablename__ = 'like' __table_args__ = (db.UniqueConstraint('user', 'target', 'kind', name='uk_user_target_kind'), ) def __init__(self, user, target, action, time, kind): self.user = user self.target = target self.action = action self.kind = kind @classmethod def add_like(cls, user_id, target, kind): try: like = cls(user_id, target, LIKE, kind) db.session.add(like) db.session.commit() _flush_like_num(target, kind) except IntegrityError: db.session.rollback() db.session.query(cls).filter_by(user=user_id, target=target, kind=kind).\ update({action: LIKE}) db.session.commit() return like @classmethod def add_dislike(cls, user, target, kind): try: like = cls(user, target, DISLIKE, kind) db.session.add(like) db.session.commit() _flush_dislike_num(target, kind) except IntegrityError: db.session.query(cls).filter_by(user=user_id, target=target, kind=kind).\ update({action: DISLIKE}) db.session.commit() return like @classmethod @cache(_LIKE_NUM_TARGET % ('{target}', '{kind}')) def get_like_num_by_target(cls, target, kind): db.session.query(cls.id).filter_by(kind=kind, target=target, action=LIKE).count() @classmethod @cache(_DISLIKE_NUM_TARGET % ('{target}', '{kind}')) def get_dislike_num_by_target(cls, target, kind): db.session.query(cls.id).filter_by(kind=kind, target=target, action=DISLIKE).count()
class GistWatchers(db.Model): __tablename__ = 'gist_watchers' __table_args__ = (db.UniqueConstraint('uid', 'gid', name='uix_uid_gid'), ) id = db.Column('id', db.Integer, primary_key=True, autoincrement=True) uid = db.Column(db.Integer, nullable=False) gid = db.Column(db.Integer, nullable=False, index=True) oid = db.Column(db.Integer, nullable=False) def __init__(self, uid, gid, oid): self.uid = uid self.gid = gid self.oid = oid
class UserGists(db.Model): __tablename__ = 'user_gists' __table_args__ = (db.UniqueConstraint('uid', 'oid', name='uix_uid_oid'), ) id = db.Column('id', db.Integer, primary_key=True, autoincrement=True) uid = db.Column(db.Integer, nullable=False) oid = db.Column(db.Integer, nullable=False, index=True) count = db.Column(db.Integer, nullable=False, default=0) def __init__(self, oid, uid, count=0): self.oid = oid self.uid = uid self.count = count
class searchAdClientOrderExecutiveReport(db.Model, BaseModelMixin): __tablename__ = 'searchAd_bra_client_order_executive_report' id = db.Column(db.Integer, primary_key=True) client_order_id = db.Column( db.Integer, db.ForeignKey('searchAd_bra_client_order.id')) # 客户合同 client_order = db.relationship( 'searchAdClientOrder', backref=db.backref('executive_reports', lazy='dynamic')) money = db.Column(db.Float()) month_day = db.Column(db.DateTime) days = db.Column(db.Integer) create_time = db.Column(db.DateTime) # 合同文件打包 order_json = db.Column(db.Text(), default=json.dumps({})) status = db.Column(db.Integer, index=True) contract_status = db.Column(db.Integer, index=True) __table_args__ = (db.UniqueConstraint( 'client_order_id', 'month_day', name='_searchAd_client_order_month_day'),) __mapper_args__ = {'order_by': create_time.desc()} def __init__(self, client_order, money=0, month_day=None, days=0, create_time=None): self.client_order = client_order self.money = money self.month_day = month_day or datetime.date.today() self.days = days self.create_time = create_time or datetime.date.today() # 合同文件打包 self.status = client_order.status self.contract_status = client_order.contract_status # 获取相应合同字段 dict_order = {} dict_order['client_name'] = client_order.client.name dict_order['agent_name'] = client_order.agent.name dict_order['contract'] = client_order.contract dict_order['campaign'] = client_order.campaign dict_order['industry_cn'] = client_order.client.industry_cn dict_order['locations'] = client_order.locations dict_order['sales'] = [ {'id': k.id, 'name': k.name, 'location': k.team.location}for k in client_order.direct_sales] dict_order['salers_ids'] = [k['id'] for k in dict_order['sales']] dict_order['get_saler_leaders'] = [ k.id for k in client_order.get_saler_leaders()] dict_order['resource_type_cn'] = client_order.resource_type_cn dict_order['operater_users'] = [ {'id': k.id, 'name': k.name}for k in client_order.operater_users] dict_order['client_start'] = client_order.client_start.strftime( '%Y-%m-%d') dict_order['client_end'] = client_order.client_end.strftime('%Y-%m-%d') self.order_json = json.dumps(dict_order) @property def month_cn(self): return self.month_day.strftime('%Y-%m') + u'月'
class searchAdAgentRebate(db.Model, BaseModelMixin): __tablename__ = 'searchAd_bra_agent_rebate' id = db.Column(db.Integer, primary_key=True) agent_id = db.Column(db.Integer, db.ForeignKey('searchAd_agent.id')) # 代理公司id agent = db.relationship('searchAdAgent', backref=db.backref('agentrebate', lazy='dynamic')) inad_rebate = db.Column(db.Float) douban_rebate = db.Column(db.Float) year = db.Column(db.Date) creator_id = db.Column(db.Integer, db.ForeignKey('user.id')) creator = db.relationship('User', backref=db.backref( 'searchAd_created_agent_rebate', lazy='dynamic')) create_time = db.Column(db.DateTime) # 添加时间 __table_args__ = (db.UniqueConstraint( 'agent_id', 'year', name='_searchAd_agent_rebate_year'), ) __mapper_args__ = {'order_by': create_time.desc()} def __init__(self, agent, inad_rebate=0.0, douban_rebate=0.0, year=None, creator=None, create_time=None): self.agent = agent self.inad_rebate = inad_rebate self.douban_rebate = douban_rebate self.year = year or datetime.date.tody() self.creator = creator self.create_time = create_time or datetime.datetime.now() def __repr__(self): return '<searchAdAgentRebate %s>' % (self.id) @property def create_time_cn(self): return self.create_time.strftime("%Y-%m-%d")
class searchAdClientOrderReject(db.Model, BaseModelMixin): __tablename__ = 'searchAd_bra_client_order_reject' id = db.Column(db.Integer, primary_key=True) client_order_id = db.Column( db.Integer, db.ForeignKey('searchAd_bra_client_order.id')) # 客户合同 client_order = db.relationship( 'searchAdClientOrder', backref=db.backref('client_order_reject_id', lazy='dynamic')) reject_time = db.Column(db.DateTime) __table_args__ = (db.UniqueConstraint( 'client_order_id', 'reject_time', name='_searchAd_client_order_reject_time'),) __mapper_args__ = {'order_by': reject_time.desc()} def __init__(self, client_order, reject_time=None): self.client_order = client_order self.reject_time = reject_time or datetime.date.today() @property def reject_time_cn(self): return self.reject_time.strftime('%Y-%m') + u'月'
class TeamMembers(db.Model): __tablename__ = 'team_members' __table_args__ = (db.UniqueConstraint('tid', 'uid', name='uix_tid_uid'), ) id = db.Column('id', db.Integer, primary_key=True, autoincrement=True) tid = db.Column(db.Integer, nullable=False) uid = db.Column(db.Integer, nullable=False) admin = db.Column(BIT(1), nullable=False, default=0) join = db.Column(db.DateTime, default=datetime.now) def __init__(self, tid, uid, admin=0): self.tid = tid self.uid = uid self.admin = admin def delete(self): db.session.delete(self) db.session.commit() def set_as_admin(self): self.admin = 1
class Team(db.Model): __tablename__ = 'team' __table_args__ = (db.UniqueConstraint('oid', 'name', name='uix_oid_name'), ) id = db.Column('id', db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.CHAR(30), nullable=False) display = db.Column(db.CHAR(30), nullable=False) oid = db.Column(db.Integer, nullable=False) members = db.Column(db.Integer, nullable=False, default=0) pic = db.Column(db.String(255), default='default.png') repos = db.Column(db.Integer, default=0) create = db.Column(db.DateTime, default=datetime.now) private = db.Column(BIT(1), nullable=False, default=0) def __init__(self, oid, name, display, private=0, members=0): self.oid = oid self.name = name self.private = private self.display = display self.members = members
class searchAdMediumRebate(db.Model, BaseModelMixin): __tablename__ = 'searchAd_bra_medium_rebate' id = db.Column(db.Integer, primary_key=True) medium_id = db.Column(db.Integer, db.ForeignKey('searchAd_medium.id')) # 媒体id medium = db.relationship('searchAdMedium', backref=db.backref('mediumrebate', lazy='dynamic')) rebate = db.Column(db.Float) year = db.Column(db.Date) creator_id = db.Column(db.Integer, db.ForeignKey('user.id')) creator = db.relationship('User', backref=db.backref( 'searchAd_created_medium_rebate', lazy='dynamic')) create_time = db.Column(db.DateTime) # 添加时间 __table_args__ = (db.UniqueConstraint( 'medium_id', 'year', name='_searchAd_medium_rebate_year'), ) __mapper_args__ = {'order_by': create_time.desc()} def __init__(self, medium, rebate=0.0, year=None, creator=None, create_time=None): self.medium = medium self.rebate = rebate self.year = year or datetime.date.today() self.creator = creator self.create_time = create_time or datetime.datetime.now() def __repr__(self): return '<searchAdMediumRebate %s>' % (self.id) @property def create_time_cn(self): return self.create_time.strftime("%Y-%m-%d")
class DepositTranscation(BaseModel): __tablename__ = 'pool_deposit_transaction' id = db.Column(db.Integer, primary_key=True) # 用户id account_key = db.Column(db.String(64), index=True) # 交易所在区块高度 block_number = db.Column(db.Integer) # 确认数 confirmed = db.Column(db.SmallInteger) # 需要确认数 need_confirm_num = db.Column(db.SmallInteger) # 推送过的确认数 pushed_confirm = db.Column(db.SmallInteger, server_default='0') # 币种名 coin_name = db.Column(db.String(32)) # 交易id tx_id = db.Column(db.String(255), index=True) # 充值数量 amount = db.Column(db.DECIMAL(32, 16)) # 交易状态,1 确认中, 2 成功, 3 失败 status = db.Column(db.SmallInteger) create_time = db.Column(db.TIMESTAMP(), default=datetime.now) update_time = db.Column(db.TIMESTAMP(), default=datetime.now) # 是否发送到汇聚地址, 0, 未发送, 1, 发送 is_pushed = db.Column(db.SmallInteger) __table_args__ = (db.UniqueConstraint('account_key', 'tx_id', name='account_key_unite_tx_id'), { 'extend_existing': True }) def __init__(self, account_key, amount, coin_name, tx_id, block_number, need_confirm_num, confirmed=0): self.account_key = account_key self.amount = float(amount) self.coin_name = coin_name self.tx_id = tx_id self.status = DEPOSIT_CONFIRMING self.is_pushed = 0 self.block_number = block_number self.need_confirm_num = need_confirm_num self.confirmed = confirmed self.pushed_confirm = -1 def __setattr__(self, key, value): super(DepositTranscation, self).__setattr__(key, value) if key != "update_time": self.update_time = datetime.now() def to_dict(self): transaction_dict = { "account_key": self.account_key, "amount": self.amount, "coin_name": self.coin_name, "tx_id": self.tx_id, "block_number": self.block_number, "need_confirm_num": self.need_confirm_num, "confirmed": self.confirmed, "status": self.status, "create_time": str(self.create_time), "update_time": str(self.update_time), } return transaction_dict