Exemple #1
0
class SSLSetting(Model):
    __tablename__ = 'ssl_setting'

    key = db.Column(db.String(50), unique=True)
    value = db.Column(db.Text)
    desc = db.Column(db.String(255), nullable=True)

    ssl_type_id = db.Column(db.Integer, db.ForeignKey('ssl_type.id'))
    ssl_type = db.relationship('SSLType', backref=db.backref('ssl_setting'), lazy='subquery', foreign_keys=[ssl_type_id])

    def __str__(self):
        return '<Setting %r>' % self.key
Exemple #2
0
class CartItem(db.Model):
    __tablename__ = 'cart_items'

    id = db.Column(db.Integer, primary_key=True)
    book_id = db.Column(db.Integer, db.ForeignKey('books.id'), nullable=False)
    cart_id = db.Column(db.Integer, db.ForeignKey('cart.id'), nullable=False)
    book = db.relationship(
        'Book',
        backref=db.backref('cart_items', uselist=False))
    created_at = db.Column(db.DateTime, default=datetime.now(pytz.utc))

    def __repr__(self):
        return '<CartItem {}>'.format(self.id)
Exemple #3
0
class Deploy(Model):
    __tablename__ = 'deploys'

    app_id = db.Column(db.Integer, db.ForeignKey('apps.id'))
    env_id = db.Column(db.Integer, db.ForeignKey('environments.id'))
    host_ids = db.Column(db.Text)
    extend = db.Column(db.Integer, db.ForeignKey('deploy_extends.id'))
    is_audit = db.Column(db.Boolean)
    rst_notify = db.Column(db.String(255), nullable=True)

    app = db.relationship('App',
                          backref=db.backref('deploy'),
                          foreign_keys=[app_id])
    env = db.relationship('Environment',
                          backref=db.backref('deploy'),
                          foreign_keys=[env_id])

    create_at = db.Column(db.String(20), default=human_datetime)
    create_by = db.Column(db.Integer, db.ForeignKey('users.id'))
    updated_at = db.Column(db.String(20))
    updated_by = db.Column(db.Integer, db.ForeignKey('users.id'))

    def __str__(self):
        return '<Deoploy %r>' % self.app

    @property
    def extend_obj(self):
        cls = DeployExtend1 if self.extend == '1' else DeployExtend2
        return cls.query.filter(deploy=self).first()

    def to_dict(self, *args, **kwargs):
        deploy = super().to_dict(*args, **kwargs)
        deploy['app_name'] = self.app.name if hasattr(self,
                                                      'app_name') else None
        deploy['host_ids'] = json.loads(self.host_ids)
        deploy['rst_notify'] = json.loads(self.rst_notify)
        deploy.update(self.extend_obj.to_dict())
        return deploy
Exemple #4
0
class AcmeDns(Model):
    __tablename__ = 'acme_dns'
    
    user = db.Column(db.String(50),nullable=True)
    key  = db.Column(db.String(50),nullable=True)
    acme_dns_type_id = db.Column(db.Integer, db.ForeignKey('acme_dns_type.id'))
    acme_dns_type = db.relationship('AcmeDnsType', backref=db.backref('acme_dns'), lazy='subquery', foreign_keys=[acme_dns_type_id])

    created_at = db.Column(db.String(20), default=human_datetime)
    created_by = db.Column(db.Integer, db.ForeignKey('users.id'))
    deleted_at = db.Column(db.String(20), nullable=True)
    deleted_by = db.Column(db.Integer, db.ForeignKey('users.id'))

    def __str__(self):
        return '<Dns %r>' % self.user
Exemple #5
0
class Image(db.Model):
    """
    This model holds information about an image in a project
    """
    __tablename__ = "image"

    id = db.Column(db.Integer, primary_key=True)
    imagename = db.Column(
        db.String(128),
        nullable=False,
    )
    image_url = db.Column(
        db.String(80),
        nullable=False,
    )
    height = db.Column(db.Integer)
    width = db.Column(db.Integer)
    labelled = db.Column(db.Boolean, default=False)
    project_id = db.Column(db.Integer,
                           db.ForeignKey('project.id'),
                           nullable=False)
    labelsdata = db.relationship('LabelData',
                                 backref='image',
                                 lazy=True,
                                 cascade="all, delete-orphan")
    labels = db.relationship('Label',
                             secondary=labels,
                             lazy='subquery',
                             backref=db.backref('image', lazy=True))

    def __init__(self, imagename, image_url, height, width, labelled,
                 project_id):
        """
        Initializes the image instance
        """
        self.imagename = imagename
        self.image_url = image_url
        self.height = height
        self.width = width
        self.labelled = labelled
        self.project_id = project_id

    def __repr__(self):
        """
        Returns the object reprensentation
        """
        return "<Image %r>" % self.imagename
Exemple #6
0
class SSL(Model):
    __tablename__ = 'ssls'

    name = db.Column(db.String(20),nullable=False)
    ssl_type_id = db.Column(db.Integer, db.ForeignKey('ssl_type.id'))
    ssl_type = db.relationship('SSLType', backref=db.backref('ssl'), lazy='subquery', foreign_keys=[ssl_type_id])
    cer = db.Column(db.Text)
    key = db.Column(db.Text)
    expiration =  db.Column(db.String(20), nullable=True)

    created_at = db.Column(db.String(20), default=human_datetime)
    created_by = db.Column(db.Integer, db.ForeignKey('users.id'))
    deleted_at = db.Column(db.String(20), nullable=True)
    deleted_by = db.Column(db.Integer, db.ForeignKey('users.id'))

    def __str__(self):
        return '<SSL %r>' % self.name
Exemple #7
0
class Label(db.Model):
    """
    This model holds information about a label
    """
    __tablename__ = "label"

    id = db.Column(db.Integer, primary_key=True)
    labelname = db.Column(
        db.String(128),
        nullable=False,
    )
    label_type = db.Column(
        db.String(80),
        nullable=False,
    )
    count = db.Column(db.Integer, default=0)
    project_id = db.Column(db.Integer,
                           db.ForeignKey('project.id'),
                           nullable=False)
    labelsdata = db.relationship('LabelData',
                                 backref='label',
                                 lazy=True,
                                 cascade="all, delete-orphan")
    models = db.relationship("MLModel",
                             secondary=model_has_label,
                             backref=db.backref("labels", lazy="dynamic"))

    def __init__(self, labelname, label_type, count, project_id):
        """
        Initializes the label instance
        """
        self.labelname = labelname
        self.label_type = label_type
        self.count = count
        self.project_id = project_id

    def __repr__(self):
        """
        Returns the object reprensentation
        """
        return "<Label %r>" % self.labelname

    @classmethod
    def find_by_id_in_project(cls, _id, project_id):
        result = cls.query.filter_by(id=_id, project_id=project_id).first()
        return result
Exemple #8
0
class Label(db.Model):
    """
    This model holds information about a label
    """
    __tablename__ = "label"

    id = db.Column(db.Integer, primary_key=True)
    label_name = db.Column(
        db.String(128),
        nullable=False,
    )
    label_type = db.Column(
        db.String(80),
        nullable=False,
    )
    created_at = db.Column(db.DateTime, default=datetime.now())
    count = db.Column(db.Integer, default=0)
    project_id = db.Column(db.Integer,
                           db.ForeignKey('project.id',
                                         ondelete="cascade",
                                         onupdate="cascade"),
                           nullable=False)
    labeldata = db.relationship('LabelData',
                                backref='label',
                                lazy=True,
                                cascade="all, save-update, delete",
                                passive_deletes=True)
    models = db.relationship("MLClassifier",
                             secondary=model_has_label,
                             backref=db.backref("labels", lazy="dynamic"))

    def __init__(self, label_name, label_type, project_id):
        """
        Initializes the label instance
        """
        self.label_name = label_name
        self.label_type = label_type
        self.project_id = project_id

    def __repr__(self):
        """
        Returns the object reprensentation
        """
        return "<Label %r>" % self.label_name
Exemple #9
0
class User(BaseModel):

    email = db.Column(db.String(255), unique=True, nullable=False)
    username = db.Column(db.String(30), unique=True, nullable=False)
    password = db.Column(db.String(255), nullable=False)
    active = db.Column(db.Boolean(), default=True)
    last_login_at = db.Column(db.DateTime())
    current_login_at = db.Column(db.DateTime())
    last_login_ip = db.Column(db.String(45))
    current_login_ip = db.Column(db.String(45))
    login_count = db.Column(db.Integer(), nullable=False, default=0)

    feeds = db.relationship('Feed',
                            secondary=user_feed,
                            lazy='dynamic',
                            backref=db.backref('users', lazy='dynamic'))

    custom_topics = db.relationship('CustomTopic', lazy=True)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.password = (bcrypt.generate_password_hash(
            self.password).decode('utf-8'))
Exemple #10
0
class User(Model):
    """
        用户表
    """
    __tablename__ = 'users'
    query_class = UserQuery

    username = db.Column(db.String(32), unique=True)
    email = db.Column(db.String(32), unique=True)
    nickname = db.Column(db.String(20), nullable=True)
    _password = db.Column("password", db.String(80))
    type = db.Column(db.String(20), default='default')
    is_supper = db.Column(db.Boolean, default=False)
    is_active = db.Column(db.Boolean, default=True)
    access_token = db.Column(db.String(32))
    token_expired = db.Column(db.Integer, nullable=True)
    last_login = db.Column(db.String(20))
    last_ip = db.Column(db.String(50))
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'), nullable=True)

    role = db.relationship('Role',
                           backref=db.backref('users'),
                           lazy='subquery',
                           foreign_keys=[role_id])

    created_at = db.Column(db.String(20), default=human_datetime)
    created_by = db.Column(db.Integer,
                           db.ForeignKey('users.id'),
                           nullable=True)
    deleted_at = db.Column(db.String(20), nullable=True)
    deleted_by = db.Column(db.Integer,
                           db.ForeignKey('users.id'),
                           nullable=True)

    def get_id(self):
        return self.id

    @staticmethod
    def is_authenticated():
        return True

    def _get_password(self):
        return self._password

    def _set_password(self, password):
        self._password = hashlib.md5(password.encode('utf-8')).hexdigest()

    password = db.synonym("_password",
                          descriptor=property(_get_password, _set_password))

    def check_password(self, password):

        if self.password is None:
            return False
        return self.password == password

    @property
    def page_perms(self):
        '''
            前端页面权限
        '''
        if self.role and self.role.page_perms:
            data = []
            perms = json.loads(self.role.page_perms)
            for m, v in perms.items():
                for p, d in v.items():
                    if type(d) == dict:
                        for j, z in d.items():
                            data.extend(f'{m}.{p}.{j}.{x}' for x in z)
                    else:
                        data.extend(f'{m}.{p}.{x}' for x in d)
            return data
        else:
            return []

    @property
    def deploy_perms(self):
        '''
            发布页面权限
        '''
        perms = json.loads(
            self.role.deploy_perms) if self.role.deploy_perms else {}
        perms.setdefault('apps', [])
        perms.setdefault('envs', [])
        return perms

    @property
    def host_perms(self):
        '''
            返回用户主机权限
        '''
        return json.loads(self.role.host_perms
                          ) if self.role and self.role.host_perms else []

    @property
    def ssl_perms(self):
        '''
            返回证书权限
        '''
        return json.loads(
            self.role.ssl_perms) if self.role and self.role.ssl_perms else []

    @property
    def acme_dns_perms(self):
        '''
            返回acme_dns权限
        '''
        return json.loads(self.role.acme_dns_perms
                          ) if self.role and self.role.acme_perms else []

    def has_host_perm(self, host_id):
        '''
            返回用户主机权限
        '''
        if isinstance(host_id, (list, set, tuple)):
            return self.is_supper or set(host_id).issubset(set(
                self.host_perms))
        return self.is_supper or int(host_id) in self.host_perms

    def has_ssl_perm(self, ssl_id):
        if isinstance(ssl_id, (list, set, tuple)):
            return self.is_supper or set(ssl_id).issubset(set(self.ssl_perms))
        return self.is_supper or int(ssl_id) in self.ssl_perms

    def has_perms(self, codes):
        # return self.is_supper or self.role in codes
        return self.is_supper

    def __str__(self):
        return '<User %r>' % self.username