class User(db.Model, UserMixin): user_id = db.Column('id', db.Integer, primary_key=True) name = db.Column(db.String(20), unique=True) password = db.Column(db.String(50)) role = db.Column(db.String(200)) __tablename__ = 'tb_user' def __init__(self, user_id=None, role=None, password=None, name="anonymous"): self.user_id = user_id self.role = role self.password = password self.name = name def is_authenticated(self): return True def is_active(self): return True def is_anonymous(self): return False def get_id(self): return unicode(self.user_id) def __repr__(self): return '<User %r>' % (self.user_id)
class Membership(BaseModel): __tablename__ = 'memberships' user_id = db.Column(db.Integer, db.ForeignKey('users.id')) room_id = db.Column(db.Integer, db.ForeignKey('rooms.id')) level = db.Column(db.Integer, nullable=False, default=1) user = db.relationship("User", backref=db.backref('memberships', lazy='dynamic', cascade="all, delete-orphan")) room = db.relationship("Room", backref=db.backref('memberships', lazy='dynamic', cascade="all, delete-orphan")) __table_args__ = (db.UniqueConstraint('user_id', 'room_id', name='membership_id'), ) def serialize(self, include_results=False): return { 'id': self.id, 'created': self.created_as_string, 'level': self.level, } def __repr__(self): return "<Membership '{}'>".format(self.id)
class FacebookCredential(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(4096)) password = db.Column(db.String(4096)) logged_timestamp = db.Column(db.DateTime, default=datetime.utcnow)
class ToDoList(db.Model): __tablename__ = 'todolist' __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(120), nullable=False) taskname = db.Column(db.String(120), nullable=False) accomplish = db.Column(db.Boolean, nullable=False) def save_to_db(self): db.session.add(self) db.session.commit() @classmethod def find_task_by_username(cls, username): result = cls.query.filter_by(username=username) task_list = list() for i in result: task_list.append(i) return task_list @classmethod def update_task(cls, id, new_task_name, accomplish): result = cls.query.filter(cls.id == id).first() result.taskname = new_task_name result.accomplish = accomplish db.session.commit() @classmethod def del_task(cls, id): result = cls.query.filter_by(id=id).first() db.session.delete(result) db.session.commit()
class Mail(BaseModel): __tablename__ = 'mail' subject = db.Column(db.Text, nullable=False) content = db.Column(db.Text, nullable=False) sender_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) receiver_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) read = db.Column(db.Integer, nullable=False, default=0) def serialize(self): return { 'id': self.id, 'created': self.created_as_string, 'subject': self.subject, 'content': self.content, 'read': self.read, 'sender': self.sender.serialize(), 'receiver': self.receiver.serialize(), } def __repr__(self): return "<Mail '{}'>".format(self.id)
class Message(BaseModel): __tablename__ = 'messages' comment = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) room_id = db.Column(db.Integer, db.ForeignKey('rooms.id'), nullable=False) def serialize(self): return { 'id': self.id, 'created': self.created_as_string, 'comment': self.comment, 'author': { 'id': self.author.id, 'name': self.author.name, 'username': self.author.username, 'avatar': self.author.avatar_or_default, }, 'room': { 'id': self.room.id, 'name': self.room.name, }, } def __repr__(self): return "<Message '{}'>".format(self.id)
class User(db.Model, UserMixin): user_id = db.Column('user_id', db.Integer, primary_key=True) accountNumber = db.Column(db.String(200), unique=True) password = db.Column(db.String(50), unique=True) __tablename__ = 'data_user' def __init__(self, user_id=None, accountNumber=None, password=None, name="anonymous"): self.user_id = user_id self.accountNumber = accountNumber self.password = password def is_authenticated(self): return True def is_active(self): return True def is_anonymous(self): return False def get_id(self): return str(self.user_id) def __repr__(self): return '<User %r>' % (self.accountNumber)
class Post(db.Model): __tablename__ = 'post' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(140)) content = db.Column(db.Text) pub_date = db.Column(db.DateTime)
class Tool(BaseModel): __tablename__ = 'tools' name = db.Column(db.String(255), nullable=False) path = db.Column(db.String(255), nullable=False) description = db.Column(db.Text, nullable=False) def __repr__(self): return "<Tool '{}'>".format(self.name)
class Score(BaseModel): __tablename__ = 'scores' player = db.Column(db.String(255), nullable=False) score = db.Column(db.Integer, nullable=False) recid = db.Column(db.Integer) recording = db.Column(db.Text, nullable=False) def __repr__(self): return "<Score '{}:{}'>".format(self.player, self.score)
class Contact(db.Model): id = db.Column(db.Integer, primary_key=True) # Ici, on souhaite que chaque Contact dispose des champs: # name : un nom unique et obligatoire (type String) name = db.Column(db.String(25), unique=True, nullable=False) # email : une adresse mail optionnelle (type String) email = db.Column(db.String(50), nullable=True) # phone : un numero de telephone optionnel (type String) phone = db.Column(db.String(10), nullable=True)
class Friendship(db.Model): """A simple table of one-to-one friendships.""" __tablename__ = 'friendships' __table_args__ = (UniqueConstraint('sender_id', 'receiver_id'), ) VALID = 0 REQUEST = 1 PENDING = 2 id = db.Column(db.Integer, primary_key=True) sender_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) receiver_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) sender = db.relationship('User', uselist=False, foreign_keys='Friendship.sender_id') receiver = db.relationship('User', uselist=False, foreign_keys='Friendship.receiver_id') valid = db.Column(db.Boolean, nullable=False, default=False) @staticmethod def create(sender, receiver): """Create an invalid friendship entry.""" assert not Friendship.get(sender, receiver) friendship = Friendship(sender=sender, receiver=receiver, valid=False) db.session.add(friendship) return friendship @staticmethod def get(first, second): """Get the existing friendship entry, regardless of validity.""" return Friendship.query.filter( or_( and_(Friendship.sender == first, Friendship.receiver == second), and_(Friendship.sender == second, Friendship.receiver == first))).first() @staticmethod def get_all(user): """Get all of a user's friendships.""" return Friendship.query.filter( or_(Friendship.sender == user, Friendship.receiver == user)).all() def set_valid(self, valid): """Change the validity of a friendship.""" self.valid = valid def __repr__(self): return '<friendship sender={} receiver={} valid={}>'.format( self.sender.email, self.receiver.email, self.valid)
class Optimization(db.Model, ModelBase): """ 门店优化结果关系表,根据生成的对应优化结果保存基本信息 """ __tablename__ = "optimization" store_id = db.Column(db.Integer, nullable=False) # 对应 forecast.version version = db.Column(db.String(64), nullable=False) # 当前库存周转天数 current_inventory_turnover_days = db.Column(db.Integer, nullable=False) # 补货后库存周转天数(门店维度) optimized_inventory_turnover_days = db.Column(db.Integer, nullable=False) # 安全库存天数 默认为 7 天 safe_inventory_days = db.Column(db.Integer, nullable=False, server_default=db.text("7")) order_amount_total = db.Column(db.Float) # 预估的货损率信息和业务配置目标预设的结果,json 字段保存信息. target_info = db.Column(db.Text(), nullable=True) # 当前优化的状态:初始化,修改中,完成 status = db.Column(TINYINT(display_width=1)) # 订单 id,提交订单时自动生成:store_id+年+月+日+当天第几个订单 eg. 1023432_20200531002 order_id = db.Column(db.String(64), nullable=True, index=True)
class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) email = db.Column(db.String(120), index=True, unique=True) password_hash = db.Column(db.String(128)) def __repr__(self): return '<User {}>'.format(self.username) def to_dict(self): return {"username": self.username, "id": self.id, "email": self.email}
class Chat(db.Model): id = db.Column(db.Integer, primary_key=True) user1 = db.Column(db.String(64), index=True) user2 = db.Column(db.String(64), index=True) chat = db.Column(db.String(640000), index=True) def __repr__(self): return '<Chat {0}:{1}>'.format(self.user1, self.user2) def to_dict(self): return {"user1": self.user1, "user2": self.user2, "chat": self.chat}
class Record(db.Model): __tablename__ = 'tb_record' index = db.Column(db.Integer, primary_key=True) timestamp = db.Column(db.String(200), nullable=False) #时间戳 pre_describe = db.Column(db.String(200), nullable=False) describe = db.Column(db.String(200), nullable=False) #区块描述对象 def __init__(self, index=None, timestamp=None, pre_describe=None, describe=None): self.index = index self.timestamp = timestamp self.pre_describe=pre_describe self.describe = describe
class PromotionPlan(db.Model, ModelBase): """ 促销计划表 """ __tablename__ = "promotion" __table_args__ = (db.Index("ix_store_id_sku_id", "store_id", "sku_id"), ) store_id = db.Column(db.Integer, nullable=False) sku_id = db.Column(db.Integer, nullable=False) start_dte = db.Column(db.DateTime) end_dte = db.Column(db.DateTime) discount = db.Column(db.Float, nullable=False)
class Hub(db.Model, DBBase): """ 仓库数据表 / 补货关系表 """ __tablename__ = "hub" hub_id = db.Column(db.String(16), primary_key=True, autoincrement=False, nullable=False) store_id = db.Column(db.String(16), nullable=False) hub_name = db.Column(db.String(16), nullable=False)
class User(db.Model, UserMixin): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(100), index=True, unique=True) password_hash = db.Column(db.String(128)) email = db.Column(db.String(100), index=True, unique=True) # posts = db.relationship('Post', backref='author', lazy='dynamic') def set_password(self, password): self.password_hash = generate_password_hash(password) def verify_password(self, password): return check_password_hash(self.password_hash, password)
class ProductInfo(db.Model): proid = db.Column(db.String, primary_key=True) proname = db.Column(db.String) category = db.Column(db.String) description = db.Column(db.String) def __init__(self, proid, name, category, description): self.proid = proid self.proname = name self.category = category self.description = description def to_dict(self): return { 'id': self.proid, 'name': self.proname, 'category': self.category, 'description': self.description } @staticmethod def get_pro_from_request(): keys = ['id', 'name', 'category', 'description'] proid, proname, category, description = get_info_from_request(keys) return ProductInfo(proid, proname, category, description) @staticmethod def add_item(item): db.session.add(item) db.session.commit() @staticmethod def get_newest_item(): item = ProductInfo.query.filter_by(proid=self.proid).first() return item.to_dict() @staticmethod def get_item_by_filter(offset=1, size=10): items = ProductInfo.query.filter('seq>:seq').params(seq=offset).limit(size) if items: print 'no....\n' print items return [item.to_dict() for item in items] else: return {'result': 'failed', 'message': '无数据!' }
class RevokedTokenModel(db.Model): __tablename__ = 'revoked_tokens' __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) jti = db.Column(db.String(120)) def add(self): db.session.add(self) db.session.commit() @classmethod def is_jti_blacklisted(cls, jti): query = cls.query.filter_by(jti=jti).first() return bool(query)
class Config(db.Model): __tablename__ = 'configs' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255), nullable=False) value = db.Column(db.Boolean, nullable=False) @staticmethod def get_by_name(name): return Config.query.filter_by(name=name).first() @staticmethod def get_value(name): return Config.query.filter_by(name=name).first().value def __repr__(self): return "<Config '{}'>".format(self.name)
class Tool(BaseModel): __tablename__ = 'tools' name = db.Column(db.String(255), nullable=False) path = db.Column(db.String(255), nullable=False) description = db.Column(db.Text, nullable=False) def serialize(self): return { 'id': self.id, 'name': self.name, 'path': self.path, 'description': self.description, } def __repr__(self): return "<Tool '{}'>".format(self.name)
class BaseModel(db.Model, AllFeaturesMixin): __abstract__ = True id: int = db.Column(db.Integer, primary_key=True) @classmethod def load(cls, data: dict, **kwargs) -> BaseModel: if not hasattr(cls, "Schema"): return None return cls.Schema().load(data, **kwargs) @classmethod def dump(cls, data: dict, **kwargs) -> dict: if not hasattr(cls, "Schema"): return None return cls.Schema().dump(data, **kwargs) def update(self, data: dict = None, **kwargs) -> BaseModel: if data is not None: if "id" not in data: data["id"] = self.id self.load(data) db.session.commit() return return super().update(**kwargs)
class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.Unicode(256), unique=True, index=True, nullable=False) password = db.Column(db.String(128), nullable=False) def __init__(self, email, password): self.email = email self.password = argon2.hash(password) def verify_password(self, password): return argon2.verify(password, self.password) def __repr__(self): return f'<User {self.email}>'
class HubInventory(db.Model, InventoryModelBase): __tablename__ = "hub_inventory" __table_args__ = (db.Index("ix_location_id_date", "location_id", "date"), ) # location_id is Foreign hub.hub_id location_id = db.Column(db.String(16), nullable=False)
class SellOut(db.Model, InvoicingModelBase): """ 销售数据表 """ __tablename__ = "sell_out" __table_args__ = (db.Index("ix_store_id_date", "store_id", "date"), ) store_id = db.Column(db.Integer, nullable=False) is_return = db.Column( db.Boolean, nullable=False, server_default=db.text( config.SELL_OUT_RETURN_STATUS.NORMAL_TEXT.value), ) price = db.Column(db.Float, nullable=False)
class Bug(BaseModel): __tablename__ = 'bugs' title = db.Column(db.String(255), nullable=False) vuln_id = db.Column(db.Integer, nullable=False, default=0) severity = db.Column(db.Integer, nullable=False, default=0) description = db.Column(db.Text, nullable=False) impact = db.Column(db.Text, nullable=False) status = db.Column(db.Integer, nullable=False, default=0) submitter_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) reviewer_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) @property def vulnerability_as_string(self): return VULNERABILITIES[self.vuln_id][0] @property def severity_as_string(self): return SEVERITY[self.severity] @property def status_as_string(self): return BUG_STATUSES[self.status] @property def bounty(self): return VULNERABILITIES[self.vuln_id][1] * self.severity @staticmethod def is_unique(signature): for bug in Bug.query.all(): s = ' '.join((bug.title, bug.description, bug.impact)) js = get_jaccard_sim(signature, s) if js > 0.5: return False return True @property def is_validated(self): # includes any validation result # rejected, confirmed, and fixed if self.status > 0: return True return False @property def is_accepted(self): # includes confirmed and fixed if self.status > 1: return True return False def __repr__(self): return "<Bug '{}'>".format(self.title)
class Note(BaseModel): __tablename__ = 'notes' name = db.Column(db.String(255), nullable=False) content = db.Column(db.Text) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) def serialize(self): return { 'id': self.id, 'name': self.name, 'content': self.content, 'created': self.created_as_string, 'modified': self.modified_as_string, } def __repr__(self): return "<Note '{}'>".format(self.name)
class Room(BaseModel): __tablename__ = 'rooms' name = db.Column(db.String(255), nullable=False, unique=True) private = db.Column(db.Boolean, nullable=False) messages = db.relationship('Message', backref='room', lazy='dynamic') members = db.relationship("User", secondary="memberships", viewonly=True, lazy='dynamic') @property def is_private(self): return self.private @property def is_public(self): return not self.private @staticmethod def get_public_rooms(): return Room.query.filter_by(private=False).all() @staticmethod def get_by_name(name): return Room.query.filter_by(name=name).first() def serialize_with_context(self, user): serialized_room = self.serialize() if self.is_private: peer = self.members.filter(User.id != user.id).first() serialized_room['display'] = f"@{peer.name}" else: serialized_room['display'] = f"#{self.name}" return serialized_room def serialize(self): return { 'id': self.id, 'created': self.created_as_string, 'name': self.name, 'display': self.name, 'private': self.private, } def __repr__(self): return "<Room '{}'>".format(self.name)