class GatewayModel(CRUDMixin, HelperMixin, db.Model): __tablename__ = 'gateway_model' id = db.Column(db.Integer, primary_key=True) gateway_id = db.Column(db.Integer, db.ForeignKey('gateway.id', ondelete='CASCADE', onupdate='CASCADE'), unique=True, index=True, nullable=False) model_id = db.Column(db.Integer, db.ForeignKey('data_model.id', ondelete='CASCADE', onupdate='CASCADE'), nullable=False) created_at = db.Column(db.DateTime, default=utils.get_utc_now) gateway = db.relationship('Gateway', backref=db.backref('gateway_model', lazy='joined'), lazy='joined') data_model = db.relationship('DataModel', backref=db.backref('gateway_model', lazy='joined'), lazy='joined') def __init__(self, gateway_id=None, model_id=None): self.gateway_id = gateway_id self.model_id = model_id def __repr__(self): return "<GatewayModel(id='%s',gateway_id='%s',model_id='%s',created_at='%s')>" % ( str(self.id), str(self.gateway_id), str( self.model_id), str(self.created_at))
class User(db.Model, UserMixin): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(32), unique=True, nullable=False) email = db.Column(db.String(128), unique=True, nullable=False) password = db.Column(db.String(128), nullable=False) is_admin = db.Column(db.Integer, default=0, nullable=False) # 是否管理员 0 不是 1 是 status = db.Column(db.Integer, default=1, nullable=False) # 是否有效 0 无效 1有效 roles = db.relationship( "Role", secondary=user_role, lazy='subquery', backref=db.backref('users', lazy=True), ) # 功能模块-用户关联部分 # 定义一对多关系 # blogs: 一个用户有多个博客,字段:复数 # author:一篇博客只能属于一个用户,字段:单数 blogs = db.relationship( 'Blog', backref='author', ) def __repr__(self): return self.username
class Admin(db.Model, UserMixin): """Admin model for the admins. The admin model is extended by the UserMixin model supplied by the Flask-Login package. The UserMixin model contains several commonly used fields like is_authenticated, is_annonymous, and others which are commonly needed for authenticating a user. The application requires atleast one superadmin throughout its life- cycle as only they can create or remove other admins. Default value of is_superadmin is false. An Admin is related to two other models so far. First one is to a Staff through one-to-many relationship. An Admin can create many staffs. Another is to the GithubAccessHistory, also through a one- to-many relationship. An Admin can invoke or revoke permissions of many Staffs many times. Any history of invoking or revoking github permissions on any staff by the Admin can be accessed through github_actions field. """ __tablename__ = 'admins' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(128), nullable=False, unique=True) password = db.Column(db.String(128), nullable=False) is_superadmin = db.Column(db.Boolean(), default=True, nullable=False) added_staffs = db.relationship('Staff', backref='added_by') github_actions = db.relationship('GithubAccessHistory', backref='action_by') def __init__(self, email, password, is_super=False): self.email = email self.password = password self.is_superadmin = is_super
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(120)) text = db.Column(db.Text, nullable=False) date = db.Column(db.Date) # some sqlalchemy_utils data types (see https://sqlalchemy-utils.readthedocs.io/) background_color = db.Column(ColorType) created_at = db.Column(ArrowType, default=arrow.utcnow()) user_id = db.Column(UUIDType(binary=False), db.ForeignKey(User.id)) user = db.relationship(User, foreign_keys=[user_id], backref='posts') tags = db.relationship('Tag', secondary=post_tags_table) def __str__(self): return "{}".format(self.title)
class Staff(db.Model): """Staff model for the staffs. Staff is a simple model for the staffs, where each staff is identified by his email. It contains a github_status field which contains the current access status of the staff to the organization's github repositories. This field can contain one of the following values: 1. 'uninvited': the staff has not been invited yet 2. 'pending': the staff is invited but not accepted invitation 3. 'granted': the staff has access to the github repos This model is backreferenced by an Admin who created the Staff. During initialization, it is required to pass the Admin through added_by parameter. Any history of invoked or revoked permissions on this Staff can be accessed through github_actions field. """ __tablename__ = 'staffs' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(128), nullable=False, unique=True) github_status = db.Column(db.String(128), nullable=False) admin_id = db.Column(db.Integer, db.ForeignKey('admins.id')) github_actions = db.relationship('GithubAccessHistory', backref='action_on') def __init__(self, email, added_by, github_status='uninvited'): self.email = email self.github_status = github_status self.added_by = added_by
class DataModel(CRUDMixin,HelperMixin,db.Model): __tablename__='data_model' id = db.Column(db.Integer,primary_key=True) dataset_id = db.Column(db.Integer, db.ForeignKey('dataset.id', ondelete='CASCADE', onupdate='CASCADE'), nullable=False) name = db.Column(db.VARCHAR(100)) algorithm_type = db.Column(db.VARCHAR(50)) serialization = db.Column(db.Integer) description = db.Column(db.VARCHAR(200)) created_at = db.Column(db.DateTime) status = db.Column(db.VARCHAR(1)) dataset = db.relationship('DataSet', backref=db.backref('data_model', lazy='joined'), lazy='joined') def __init__(self,dataset_id=None,name=None,algorithm_type=None,serialization=None,description=None,created_at=None,status=None): self.dataset_id = dataset_id self.name = name self.algorithm_type = algorithm_type self.serialization = serialization self.description = description self.created_at = created_at self.status = status def __repr__(self): return "<DataModel(id='%s',dataset_id='%s',name='%s',algorithm_type='%s',serialization='%s',description='%s',created_at='%s',status='%s')>" % ( str(self.id),str(dataset_id),self.name,self.algorithm_type,self.serialization,self.description,str(self.created_at),str(self.status))
class Tree(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64)) # recursive relationship parent_id = db.Column(db.Integer, db.ForeignKey('tree.id')) parent = db.relationship('Tree', remote_side=[id], backref='children') def __str__(self): return "{}".format(self.name)
class JobSector(db.Model): __tablename__ = "jobsector" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(200)) skills = db.relationship('JobSkill', secondary=skills_sector_table) def __str__(self): return "{}".format(self.name) @property def get_data(self): dictionary = self.__dict__ try: del dictionary['_sa_instance_state'] except: pass return dictionary
class User(db.Model): id = db.Column(UUIDType(binary=False), default=uuid.uuid4, primary_key=True) # use a regular string field, for which we can specify a list of available choices later on type = db.Column(db.String(100)) # fixed choices can be handled in a number of different ways: enum_choice_field = db.Column(db.Enum(EnumChoices), nullable=True) sqla_utils_choice_field = db.Column(ChoiceType(AVAILABLE_USER_TYPES), nullable=True) sqla_utils_enum_choice_field = db.Column(ChoiceType(EnumChoices, impl=db.Integer()), nullable=True) first_name = db.Column(db.String(100)) last_name = db.Column(db.String(100)) # some sqlalchemy_utils data types (see https://sqlalchemy-utils.readthedocs.io/) email = db.Column(EmailType, unique=True, nullable=False) website = db.Column(URLType) ip_address = db.Column(IPAddressType) currency = db.Column(CurrencyType, nullable=True, default=None) timezone = db.Column(TimezoneType(backend='pytz')) dialling_code = db.Column(db.Integer()) local_phone_number = db.Column(db.String(10)) featured_post_id = db.Column(db.Integer, db.ForeignKey('post.id')) featured_post = db.relationship('Post', foreign_keys=[featured_post_id]) @hybrid_property def phone_number(self): if self.dialling_code and self.local_phone_number: number = str(self.local_phone_number) return "+{} ({}) {} {} {}".format(self.dialling_code, number[0], number[1:3], number[3:6], number[6::]) return @phone_number.expression def phone_number(cls): return sql.operators.ColumnOperators.concat(cast(cls.dialling_code, db.String), cls.local_phone_number) def __str__(self): return "{}, {}".format(self.last_name, self.first_name) def __repr__(self): return "{}: {}".format(self.id, self.__str__())
class Operation(db.Model): """ 可以把此看作权限表,但我将其定义为 操作某种资源(资源的定义参考 restfull api设计理念)的一系列动作, 如 查看 某个 资源, 如 新增 一个资源 而把 某动作 授予 某个角色,即完成了权限的管理(绑定) """ __tablename__ = 'operation' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) method = db.Column(db.String(32), default='GET', ) uri = db.Column(db.String(256), default='/', nullable=False) roles = db.relationship( "Role", secondary=role_operation, lazy='subquery', backref=db.backref('operations', lazy=True), ) description = db.Column(db.Text, comment=u'描述信息') def __repr__(self): return self.name
class User(db.Model): id = db.Column(UUIDType(binary=False), default=uuid.uuid4, primary_key=True) password = db.Column(db.String(128)) auth = db.Column(db.String(100)) email = db.Column(EmailType, unique=True, nullable=False) nickname = db.Column(db.String(100), nullable=False) thumbnail = db.Column(URLType, default='/static/img/wdticon.png') create_date = db.Column(db.DateTime) resume = db.relationship('Resume') @property def is_authenticated(self): return True @property def is_admin(self): if self.auth == u'admin': return True else: return False @property def is_active(self): return True @property def is_anonymous(self): return False def get_id(self): return self.id # Required for administrative interface def __unicode__(self): return self.username