Ejemplo n.º 1
0
class Tag(TimestampMixin, db.Model):
    """
    represent the tag of a disruption
    """
    __tablename__ = 'tag'
    id = db.Column(UUID, primary_key=True)
    name = db.Column(db.Text, unique=False, nullable=False)
    is_visible = db.Column(db.Boolean, unique=False, nullable=False, default=True)
    client_id = db.Column(UUID, db.ForeignKey(Client.id), nullable=False)
    client = db.relationship('Client', backref='tags', lazy='joined')
    __table_args__ = (db.UniqueConstraint('name', 'client_id', name='tag_name_client_id_key'),)

    def __init__(self):
        self.id = str(uuid.uuid1())

    def __repr__(self):
        return '<Tag %r>' % self.id

    @classmethod
    def all(cls, client_id):
        return cls.query.filter_by(client_id=client_id,is_visible=True).all()

    @classmethod
    def get(cls, id, client_id):
        return cls.query.filter_by(id=id, client_id=client_id, is_visible=True).first_or_404()

    @classmethod
    def get_archived_by_name(cls, name, client_id):
        return cls.query.filter_by(name=name, client_id=client_id, is_visible=False).first()
Ejemplo n.º 2
0
class Property(TimestampMixin, db.Model):
    """
    represents the types of properties
    """
    __tablename__ = 'property'
    __table_args__ = (
        db.UniqueConstraint(
            'type',
            'key',
            'client_id',
            name='property_type_key_client_id_uc'
        ),
    )
    id = db.Column(UUID, primary_key=True)
    client_id = db.Column(UUID, db.ForeignKey(Client.id), nullable=False)
    client = db.relationship('Client', backref='properties', lazy='joined')
    key = db.Column(db.Text, nullable=False)
    type = db.Column(db.Text, nullable=False)
    disruptions = db.relationship(
        'AssociateDisruptionProperty',
        lazy='dynamic',
        back_populates='property',
        cascade='delete'
    )

    def __init__(self):
        self.id = str(uuid.uuid1())

    def __repr__(self):
        return '<%s: %s %s %s>' % (
            self.__class__.__name__, self.id, self.type, self.key
        )

    @classmethod
    def prepare_request(cls, client_id, key=None, type=None, id=None):
        request = {'client_id': client_id}
        if id:
            request['id'] = id
        if key:
            request['key'] = key
        if type:
            request['type'] = type

        return request

    @classmethod
    def all(cls, client_id, key=None, type=None):
        kargs = cls.prepare_request(client_id, key, type)

        return cls.query.filter_by(**kargs).all()

    @classmethod
    def get(cls, client_id, id=None, key=None, type=None):
        kargs = cls.prepare_request(client_id, key, type, id)

        return cls.query.filter_by(**kargs).first()