class AnsibleTasks(db.Model): """ansible任务表""" __tablename__ = 'ansibletask' id = db.Column(db.Integer, nullable=False, primary_key=True, autoincrement=True) ansible_id = db.Column(db.String(80), unique=True, nullable=True) celery_id = db.Column(db.String(80), unique=True, nullable=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True) group_name = db.Column(db.String(80), nullable=True) playbook = db.Column(db.String(80), unique=False, nullable=True) extra_vars = db.Column(db.Text, nullable=True) option_id = db.Column(db.Integer, db.ForeignKey('options.id', ondelete='SET NULL'), nullable=True) # True: 任务执行完成 False: 任务执行中 status_choices = ( (1, '执行中'), (2, '已完成'), (3, '任务失败'), (4, '任务取消') ) state = db.Column(ChoiceType(status_choices, db.Integer()), nullable=False, default=0, comment="任务状态") # state = db.Column(db.Boolean, default=False, nullable=False) ansible_result = db.Column(db.Text(16777216)) celery_result = db.Column(db.Text) create_time = db.Column(db.DateTime, default=datetime.datetime.now) cancelled = db.Column(db.Boolean, nullable=False, default=False) user = db.relationship('User') option = db.relationship('Options') # 排序 __mapper_args__ = { "order_by": create_time.desc(), } def __repr__(self): return "<{}: {}>".format(self.__class__.__name__, self.ansible_id)
class Post(db.Model): """wiki文章表""" id = db.Column(db.Integer, nullable=False, primary_key=True, autoincrement=True) title = db.Column(db.String(60), nullable=False, comment="文章标题") desc = db.Column(db.String(180), comment="文章摘要") body = db.Column(db.Text) published = db.Column(db.Boolean, nullable=False, default=False, comment="文档发布状态") create_time = db.Column(db.DateTime, default=datetime.datetime.now, comment="创建时间") update_time = db.Column(db.DateTime, default=datetime.datetime.now, comment="更新时间") # 与分类表关联 category_id = db.Column(db.Integer, db.ForeignKey('category.id')) category = db.relationship('Category', back_populates='posts') # 作者 author_id = db.Column(db.Integer, db.ForeignKey('user.id')) author = db.relationship('User', back_populates='posts')
class Options(db.Model): """playbook选项参数""" __tablename__ = 'options' id = db.Column(db.Integer, nullable=False, primary_key=True, autoincrement=True) name = db.Column(db.String(48), nullable=False) content = db.Column(db.Text, nullable=True) playbook_id = db.Column(db.Integer, db.ForeignKey('playbook.id'), nullable=False) env_id = db.Column(db.Integer, db.ForeignKey('environment.id')) url = db.Column(db.String(128), nullable=True) playbook = db.relationship('PlayBook') env = db.relationship('Environment') def __repr__(self): return '<Options %r>' % self.name
class InventoryModel(db.Model): __tablename__ = 'inventory' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String) cost = db.Column(db.Float) retail = db.Column(db.Float) sku = db.Column(db.String) description = db.Column(db.Text) buy_button = db.Column(db.Text) image_id = db.Column(db.Integer, db.ForeignKey('image.id')) image = db.relationship("ImageModel", back_populates="inventory") def __str__(self): return self.name @hybrid_property def path(self): return form.thumbgen_filename(self.image.path) @hybrid_property def quantity(self): return round( InventoryStockModel.get_quantity(self.id) - SalesModel.get_quantity(self.id), 2)
class PlayBookDetail(db.Model): __tablename__ = 'playbook_detail' id = db.Column(db.Integer, nullable=False, primary_key=True, autoincrement=True) playbook_id = db.Column(db.Integer, db.ForeignKey('playbook.id'), nullable=False) content = db.Column(db.Text) playbook = db.relationship('PlayBook', back_populates='detail') def __repr__(self): return '<PlayBookDetail %r>' % self.id
class PosterImage(db.Model): id = db.Column(db.Integer, primary_key=True) large = db.Column(db.Text) original = db.Column(db.Text) small = db.Column(db.Text) tiny = db.Column(db.Text) series_id = db.Column(db.Integer, db.ForeignKey('series.id'), nullable=False)
class DrawGroup(db.Model): __tablename__ = 'draw_groups' id = db.Column(db.Integer, primary_key=True) game_id = db.Column(db.Integer, db.ForeignKey('games.id')) game = db.relationship("Game", back_populates='draw_groups') players = db.relationship('Player', secondary=association_draw_groups_players, back_populates='draw_groups') def __init__(self, game): self.game = game
class Level(db.Model): __tablename__ = "levels" id = db.Column(db.Integer, primary_key=True) date_valid = db.Column(db.Date) level = db.Column(db.Integer) player_id = db.Column(db.Integer, db.ForeignKey('players.id')) player = db.relationship('Player', back_populates='levels') def __init__(self, player, date_valid, level): self.player = player self.date_valid = date_valid self.level = level
class Host(db.Model): """主机表""" __tablename__ = 'host' id = db.Column(db.Integer, nullable=False, primary_key=True, autoincrement=True) hostname = db.Column(db.String(60), nullable=False, unique=True) ip = db.Column(db.String(20), nullable=False, unique=True) port = db.Column(db.Integer, nullable=False, default=22) group_id = db.Column(db.Integer, db.ForeignKey('host_group.id'), nullable=True) # 关系标量 group = db.relationship('HostGroup', back_populates='hosts') def __repr__(self): return '<Host %r>' % self.hostname
class FileRepository(db.Model): """文件库""" __tablename__ = 'file_repository' id = db.Column(db.Integer, nullable=False, primary_key=True, autoincrement=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) file_type_choices = ( (1, '文件'), (2, '文件夹') ) file_type = db.Column(ChoiceType(file_type_choices, db.Integer()), nullable=True, comment="文件类型") name = db.Column(db.String(64), nullable=False, comment="文件/文件夹名称") key = db.Column(db.String(128), nullable=True, comment="文件存储在OSS中的KEY") file_size = db.Column(db.Integer, nullable=True, comment="文件大小/字节") file_path = db.Column(db.String(255), nullable=True, comment="文件路径") parent_id = db.Column(db.Integer, db.ForeignKey('file_repository.id'), nullable=True, comment="父级目录id") update_datetime = db.Column(db.DateTime, default=datetime.datetime.now, comment="更新时间") user = db.relationship('User') parent = db.relationship('FileRepository', remote_side=[id], back_populates='childs') childs = db.relationship('FileRepository', back_populates='parent', cascade='all') def __repr__(self): return "<{}: {}>".format(self.__class__.__name__, self.id)
class Example(db.Model): """Example - to be changed """ id = db.Column(db.Integer, primary_key=True) search_query = db.Column(db.String(1500), nullable=True) search_tags = db.Column(db.Text, nullable=True) user_id = db.Column(db.Integer, db.ForeignKey("user.id")) def __init__(self, **kwargs): super(Example, self).__init__(**kwargs) def __repr__(self): return "<Example %s>" % self.id
class SalesModel(db.Model): __tablename__ = "sales" id = db.Column(db.Integer, primary_key=True) quantity = db.Column(db.Integer) sale_date = db.Column(db.DateTime(), default=datetime.datetime.utcnow()) type_id = db.Column(db.Integer, db.ForeignKey("inventory.id")) type = db.relationship("InventoryModel") @classmethod def get_quantity(cls, foreign_id): quantity = cls.session.query(func.sum( cls.quantity)).filter(cls.type_id == foreign_id).scalar() return quantity if quantity else 0
class Attribute(db.Model): id = db.Column(db.Integer, primary_key=True) synopsis = db.Column(db.Text, nullable=True) rating = db.Column(db.Integer, nullable=True) rating_rank = db.Column(db.Integer) favorites_count = db.Column(db.Integer) popularity_rank = db.Column(db.Integer) start_date = db.Column(db.String(15), nullable=True) end_date = db.Column(db.String(15)) status = db.Column(db.Text) episode_count = db.Column(db.Integer) episode_length = db.Column(db.Integer) nsfw = db.Column(db.Boolean) series_id = db.Column(db.Integer, db.ForeignKey('series.id'), nullable=False)
def foreign_key(model_or_table_name, fk_col=None, primary_key=False, **kwargs): """Helper method to add a foreign key Column to a model. For example:: class Post(Model): category_id = foreign_key('Category') category = relationship('Category', back_populates='posts') Is equivalent to:: class Post(Model): category_id = Column(BigInteger, ForeignKey('category.id'), nullable=False) category = relationship('Category', back_populates='posts') :param model_or_table_name: the model or table name to link to If given a lowercase string, it's treated as an explicit table name. If there are any uppercase characters, it's assumed to be a model name, and will be converted to snake case using the same automatic conversion as Flask-SQLAlchemy does itself. If given an instance of :class:`flask_sqlalchemy.Model`, use its :attr:`__tablename__` attribute. :param str fk_col: column name of the primary key (defaults to "id") :param bool primary_key: Whether or not this Column is a primary key :param dict kwargs: any other kwargs to pass the Column constructor """ model = model_or_table_name table_name = model_or_table_name fk_col = fk_col or 'id' if inspect.isclass(model) and issubclass(model, db.Model): table_name = model_or_table_name.__tablename__ elif table_name != table_name.lower(): table_name = camel_to_snake_case(table_name) return Column(db.BigInteger, db.ForeignKey(f'{table_name}.{fk_col}'), primary_key=primary_key, **kwargs)
class Incident(db.Model): __tablename__ = 'incidents' # Prefer JSONB if we are using Postgres b/c it's a faster, binary, prettier format _JSON_COLUMN_TYPE = JSONB if 'postgresql' in BaseConfig.SQLALCHEMY_DATABASE_URI else db.JSON id = db.Column(db.Integer, primary_key=True) external_id = db.Column(db.String) links = db.Column(_JSON_COLUMN_TYPE) state = db.Column(db.String) city = db.Column(db.String) description = db.Column(db.String) tags = db.Column(_JSON_COLUMN_TYPE) name = db.Column(db.String, nullable=False) date = db.Column(db.String) date_text = db.Column(db.String) # many-to-one relationship, as many incidents can occur in a single location location_id = db.Column(db.Integer, db.ForeignKey('locations.id'), index=True) location = db.relationship('Location')
class TokenBlacklist(db.Model): """Blacklist representation """ id = db.Column(db.Integer, primary_key=True) jti = db.Column(db.String(36), nullable=False, unique=True) token_type = db.Column(db.String(10), nullable=False) user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False) revoked = db.Column(db.Boolean, nullable=False) expires = db.Column(db.DateTime, nullable=False) user = db.relationship("User", lazy="joined") def to_dict(self): return { "token_id": self.id, "jti": self.jti, "token_type": self.token_type, "user_identity": self.user_identity, "revoked": self.revoked, "expires": self.expires, }
class GameDetails(db.Model): __tablename__ = "games_details" id = db.Column(db.Integer, primary_key=True) max_hcp_man = db.Column(db.Integer) max_hcp_woman = db.Column(db.Integer) tee_off = db.Column(db.Time) fee = db.Column(db.Integer) game_id = db.Column(db.Integer, db.ForeignKey('games.id')) game = db.relationship("Game", back_populates="game_details", uselist=False) def __init__(self, game, max_hcp_man=None, max_hcp_woman=None, tee_off=None, fee=None): self.game = game self.max_hcp_man = max_hcp_man self.max_hcp_woman = max_hcp_woman self.tee_off = tee_off self.fee = fee
password = Column(String(100)) def __init__(self, username, password): self.username = username self.set_password(password) def set_password(self, password): self.password = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password, password) participant_hacknight = db.Table( 'participant_hacknight', db.Column('participant_id', db.Integer, db.ForeignKey('participant.id')), db.Column('hacknight_id', db.Integer, db.ForeignKey('hacknight.id'))) class Participant(db.Model): """Participant model.""" __tablename__ = 'participant' id = Column(Integer, autoincrement=True, primary_key=True) name = Column(String(50)) lastname = Column(String(50)) email = Column(String(200)) github = Column(String(200), default="") phone = Column(String(13)) class Hacknight(db.Model):
password = Column(String(100)) def __init__(self, username, password): self.username = username self.set_password(password) def set_password(self, password): self.password = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password, password) participant_hacknight = db.Table( "participant_hacknight", db.Column("participant_id", db.Integer, db.ForeignKey("participant.id")), db.Column("hacknight_id", db.Integer, db.ForeignKey("hacknight.id")), ) class Participant(db.Model): """Participant model.""" __tablename__ = "participant" id = Column(Integer, autoincrement=True, primary_key=True) first_name = Column(String(50), nullable=False) last_name = Column(String(50), nullable=False) email = Column(String(200), unique=True, nullable=False) github = Column(String(200), unique=True, nullable=False) phone = Column(String(13)) slack = Column(String(21))
from backend.extensions import db from backend.utils import get_class_by_tablename user_model = get_class_by_tablename("user") type_association = db.Table( 'type_association', db.metadata, db.Column("id", db.Integer, primary_key=True), db.Column('left_id', db.Integer, db.ForeignKey('post.id')), db.Column('right_id', db.Integer, db.ForeignKey('post_type.id'))) user_association = db.Table( 'user_association', db.metadata, db.Column("id", db.Integer, primary_key=True), db.Column('left_id', db.Integer, db.ForeignKey('user.id')), db.Column('right_id', db.Integer, db.ForeignKey('post.id'))) class PostType(db.Model): __tablename__ = "post_type" id = db.Column(db.Integer, primary_key=True) type = db.Column(db.String, unique=True) active = db.Column(db.Boolean, default=True) def __str__(self): return str(self.type) class Post(db.Model): __tablename__ = "post"
from backend.extensions import db association_table = db.Table( 'watch_queue', db.Column('user_id', db.Integer, db.ForeignKey('user.id')), db.Column('series_id', db.Integer, db.ForeignKey('series.id'))) class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) password = db.Column(db.String(60), nullable=False) watch_queue = db.relationship('Series', secondary=association_table, backref=db.backref('queue', lazy='dynamic')) class Series(db.Model): __tablename__ = 'series' id = db.Column(db.Integer, primary_key=True) kitsu_id = db.Column(db.Integer, nullable=True) canonical_title = db.Column(db.String(120), nullable=True) en_title = db.Column(db.String(120), nullable=True) en_jp_title = db.Column(db.String(120), nullable=True) attributes = db.relationship('Attribute', uselist=False, backref='series') cover_image = db.relationship('CoverImage', uselist=False, backref='series') poster_image = db.relationship('PosterImage', uselist=False, backref='series')
# coding=utf-8 from backend.extensions import db association_draw_groups_players = db.Table( 'association_draw_groups_players', db.Model.metadata, db.Column('player_id', db.Integer, db.ForeignKey('players.id')), db.Column('draw_group_id', db.Integer, db.ForeignKey('draw_groups.id')), ) class DrawGroup(db.Model): __tablename__ = 'draw_groups' id = db.Column(db.Integer, primary_key=True) game_id = db.Column(db.Integer, db.ForeignKey('games.id')) game = db.relationship("Game", back_populates='draw_groups') players = db.relationship('Player', secondary=association_draw_groups_players, back_populates='draw_groups') def __init__(self, game): self.game = game