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

46 lines
2 KiB
Python
Raw Normal View History

"""
This module defines the BoolOperation database class.
"""
2021-05-07 17:46:14 +00:00
from ..base import ext
from .Enums import OperationType
2021-05-07 17:46:14 +00:00
from sqlalchemy.orm import backref
2021-05-07 17:46:14 +00:00
class BoolOperation(ext.Model):
__tablename__ = "bool_operation"
2021-05-07 17:51:03 +00:00
2021-05-07 17:46:14 +00:00
id = ext.Column(ext.Integer, primary_key=True)
operation = ext.Column(ext.Enum(OperationType), nullable=False)
2021-05-11 16:45:40 +00:00
is_root = ext.Column(ext.Boolean, default=False, nullable=False)
# Foreign Keys
2021-05-07 17:46:14 +00:00
condition_id = ext.Column(ext.Integer, ext.ForeignKey("condition.id"))
2021-05-10 09:00:21 +00:00
node_1_id = ext.Column(ext.Integer, ext.ForeignKey("bool_operation.id", ondelete="SET NULL"))
node_2_id = ext.Column(ext.Integer, ext.ForeignKey("bool_operation.id", ondelete="SET NULL"))
2021-05-11 16:45:40 +00:00
alert_id = ext.Column(ext.Integer, ext.ForeignKey("alert.id", ondelete="CASCADE"))
# Relationships
2021-05-07 17:46:14 +00:00
condition = ext.relationship("Condition", back_populates="operations")
node_1 = ext.relationship("BoolOperation", primaryjoin=("bool_operation.c.node_1_id==bool_operation.c.id"),
remote_side="BoolOperation.id", backref=backref("father_1", uselist=False))
node_2 = ext.relationship("BoolOperation", primaryjoin=("bool_operation.c.node_2_id==bool_operation.c.id"),
remote_side="BoolOperation.id", backref=backref("father_2", uselist=False))
alert = ext.relationship("Alert", back_populates="operations")
2021-05-07 17:15:14 +00:00
def to_json(self):
return {"id": self.id,
"operation": self.operation,
"is_root": self.is_root,
"alert_id": self.alert_id,
"condition": self.condition.to_json() if self.condition else None,
"node_1": self.node_1.to_json() if self.node_1 else None,
"node_2": self.node_2.to_json() if self.node_2 else None
}
2021-05-10 09:00:21 +00:00
2021-05-14 08:44:07 +00:00
def get_chain_ids(self, lista):
if self.id in lista:
2021-05-10 09:00:21 +00:00
# Loop detected!
return -1
2021-05-14 08:44:07 +00:00
lista.append(self.id)
self.node_1.get_chain_ids(lista)
self.node_1.get_chain_ids(lista)