1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-23 05:24:18 +00:00
pds-2021-g2-nest/code/backend/nest_backend/database/tables/Repository.py

41 lines
1.6 KiB
Python
Raw Normal View History

"""
2021-04-29 02:02:08 +00:00
This module defines the :class:`.Repository` database class.
"""
from ..base import Base
2021-05-06 15:20:07 +00:00
from .Enums import ConditionMode
class Repository(Base.Model):
__tablename__ = "repository"
2021-04-29 02:02:08 +00:00
# Columns
id = Base.Column(Base.Integer, primary_key=True)
name = Base.Column(Base.String, nullable=False)
start = Base.Column(Base.DateTime, nullable=True)
end = Base.Column(Base.DateTime, nullable=True)
2021-05-06 15:20:07 +00:00
is_active = Base.Column(Base.Boolean, nullable=False, default=False)
evaluation_mode = Base.Column(Base.Enum(ConditionMode), default=ConditionMode.all_or.value)
2021-04-29 02:02:08 +00:00
# Foreign Keys
owner_id = Base.Column(Base.String, Base.ForeignKey("user.email", ondelete="CASCADE"), nullable=False)
2021-04-29 02:02:08 +00:00
# Relationships
owner = Base.relationship("User", back_populates="owner_of")
authorizations = Base.relationship("Authorization", back_populates="repository", cascade="all, delete")
tweets = Base.relationship("Composed", back_populates="repository", cascade="all, delete")
alerts = Base.relationship("Alert", back_populates="repository", cascade="all, delete")
conditions = Base.relationship("Condition", back_populates="repository")
def to_json(self):
2021-04-29 02:02:08 +00:00
return {
"id": self.id,
"name": self.name,
"start": self.start.isoformat() if self.start else None,
2021-05-06 15:20:07 +00:00
"is_active": self.is_active,
2021-04-29 02:02:08 +00:00
"end": self.end.isoformat() if self.end else None,
2021-05-06 15:20:07 +00:00
"owner": self.owner.to_json(),
"evaluation_mode": self.evaluation_mode,
"conditions": [c.to_json() for c in self.conditions]
2021-04-29 02:02:08 +00:00
}