Exemplo n.º 1
0
class Task(BaseModel):
    __tablename__ = "tasks"

    key = db.Column(db.String(), nullable=False)
    status = db.Column(db.Enum(StatusType),
                       default=StatusType.pending,
                       nullable=False)
    previous = db.Column(JSONBType, default=[])
    result = db.Column(PickleType)

    # Relationship
    workflow_id = db.Column(UUIDType(binary=False),
                            db.ForeignKey("workflows.id"),
                            nullable=False)
    workflow = db.relationship("Workflow",
                               backref=db.backref("tasks", lazy=True))

    def __repr__(self):
        return f"<Task {self.key}>"

    def to_dict(self):
        d = super().to_dict()
        d.update({
            "key": self.key,
            "status": self.status.value,
            "task": self.id,
            "previous": self.previous,
            "result": self.result,
        })
        return d
Exemplo n.º 2
0
class BaseModel(db.Model):
    __abstract__ = True

    id = db.Column(
        UUIDType(binary=False), primary_key=True, nullable=False, default=get_uuid
    )
    created_at = db.Column(
        db.DateTime(timezone=True), default=db.func.now(), nullable=False, index=True
    )
    updated_at = db.Column(
        db.DateTime(timezone=True),
        default=db.func.now(),
        onupdate=db.func.now(),
        nullable=False,
    )

    def save(self):
        try:
            db.session.add(self)
            db.session.commit()
        except Exception:
            db.session.rollback()
            raise

    def to_dict(self):
        return {
            "id": self.id,
            "created": self.created_at.isoformat(),
            "updated": self.updated_at.isoformat(),
        }
Exemplo n.º 3
0
class User(BaseModel):
    __tablename__ = "users"

    username = db.Column(db.String(255), unique=True, nullable=False)
    password = db.Column(db.String(255), nullable=False)

    def __repr__(self):
        return f"<User {self.username}>"

    def update(self):
        user = self.query.filter_by(username=self.username).first()
        if not user:
            raise UserNotFound(f"User {self.username} not found")

        user.password = self.password

        self.commit()

    def delete(self):
        db.session.delete(self)

        self.commit()

    def to_dict(self):
        d = super().to_dict()
        d.update({"username": self.username, "password": self.password})
        return d
Exemplo n.º 4
0
class Workflow(BaseModel):
    __tablename__ = "workflows"

    name = db.Column(db.String(255), nullable=False)
    project = db.Column(db.String(255), nullable=False)
    status = db.Column(db.Enum(StatusType),
                       default=StatusType.pending,
                       nullable=False)
    payload = db.Column(JSONBType, default={})
    periodic = db.Column(db.Boolean, default=False)

    def __str__(self):
        return f"{self.project}.{self.name}"

    def __repr__(self):
        return f"<Workflow {self.project}.{self.name}>"

    def to_dict(self):
        d = super().to_dict()
        d.update({
            "name": self.name,
            "payload": self.payload,
            "project": self.project,
            "fullname": f"{self.project}.{self.name}",
            "status": self.status.value,
            "periodic": self.periodic,
        })
        return d