1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-25 14:34:19 +00:00
pds-2021-g2-nest/nest_backend/database/tables/Repository.py

43 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.
"""
2021-05-07 17:46:14 +00:00
from ..base import ext
2021-05-06 15:20:07 +00:00
from .Enums import ConditionMode
2021-05-07 17:46:14 +00:00
class Repository(ext.Model):
__tablename__ = "repository"
2021-04-29 02:02:08 +00:00
# Columns
2021-05-07 17:46:14 +00:00
id = ext.Column(ext.Integer, primary_key=True)
name = ext.Column(ext.String, nullable=False)
start = ext.Column(ext.DateTime, nullable=True)
end = ext.Column(ext.DateTime, nullable=True)
is_active = ext.Column(ext.Boolean, nullable=False, default=False)
is_deleted = ext.Column(ext.Boolean, nullable=False, default=False)
evaluation_mode = ext.Column(ext.Enum(ConditionMode), default=ConditionMode.all_or)
2021-04-29 02:02:08 +00:00
# Foreign Keys
2021-05-07 17:46:14 +00:00
owner_id = ext.Column(ext.String, ext.ForeignKey("user.email", ondelete="CASCADE"), nullable=False)
2021-04-29 02:02:08 +00:00
# Relationships
2021-05-07 17:46:14 +00:00
owner = ext.relationship("User", back_populates="owner_of")
2021-05-11 16:45:40 +00:00
authorizations = ext.relationship("Authorization", back_populates="repository")
tweets = ext.relationship("Composed", back_populates="repository")
alerts = ext.relationship("Alert", back_populates="repository")
2021-05-07 17:46:14 +00:00
conditions = ext.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(),
2021-05-20 11:56:23 +00:00
"spectators": [a.to_json() for a in self.authorizations],
"evaluation_mode": self.evaluation_mode.value,
2021-05-06 15:20:07 +00:00
"conditions": [c.to_json() for c in self.conditions]
2021-04-29 02:02:08 +00:00
}