From 390ee441c0ddf8057b71a5a40e7ec8d66fe6acb4 Mon Sep 17 00:00:00 2001 From: Lorenzo Balugani Date: Tue, 11 May 2021 18:45:40 +0200 Subject: [PATCH] Fixed issues --- code/backend/nest_backend/database/tables/Alert.py | 4 ++-- .../nest_backend/database/tables/BoolOperation.py | 4 ++-- .../backend/nest_backend/database/tables/Composed.py | 2 +- .../nest_backend/database/tables/Condition.py | 4 ++-- .../backend/nest_backend/database/tables/Contains.py | 4 ++-- .../nest_backend/database/tables/Notification.py | 2 +- .../nest_backend/database/tables/Repository.py | 6 +++--- .../nest_backend/routes/repository/repository.py | 12 ++++++------ code/backend/nest_backend/routes/users/user.py | 4 ++-- 9 files changed, 21 insertions(+), 21 deletions(-) diff --git a/code/backend/nest_backend/database/tables/Alert.py b/code/backend/nest_backend/database/tables/Alert.py index 1c573dc..6afe6b1 100644 --- a/code/backend/nest_backend/database/tables/Alert.py +++ b/code/backend/nest_backend/database/tables/Alert.py @@ -15,8 +15,8 @@ class Alert(ext.Model): repository_id = ext.Column(ext.Integer, ext.ForeignKey("repository.id", ondelete="CASCADE"), nullable=False) # Relationships repository = ext.relationship("Repository", back_populates="alerts") - notifications = ext.relationship("Notification", back_populates="alert", cascade="all, delete") - operations = ext.relationship("BoolOperation", back_populates="alert", cascade="all, delete") + notifications = ext.relationship("Notification", back_populates="alert") + operations = ext.relationship("BoolOperation", back_populates="alert") def to_json(self): return { diff --git a/code/backend/nest_backend/database/tables/BoolOperation.py b/code/backend/nest_backend/database/tables/BoolOperation.py index a2be376..5e6ef69 100644 --- a/code/backend/nest_backend/database/tables/BoolOperation.py +++ b/code/backend/nest_backend/database/tables/BoolOperation.py @@ -12,12 +12,12 @@ class BoolOperation(ext.Model): id = ext.Column(ext.Integer, primary_key=True) operation = ext.Column(ext.Enum(OperationType), nullable=False) - isRoot = ext.Column(ext.Boolean, default=False, nullable=False) + is_root = ext.Column(ext.Boolean, default=False, nullable=False) # Foreign Keys condition_id = ext.Column(ext.Integer, ext.ForeignKey("condition.id")) 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")) - alert_id = ext.Column(ext.Integer, ext.ForeignKey("alert.id")) + alert_id = ext.Column(ext.Integer, ext.ForeignKey("alert.id", ondelete="CASCADE")) # Relationships condition = ext.relationship("Condition", back_populates="operations") node_1 = ext.relationship("BoolOperation", primaryjoin=("bool_operation.c.node_1_id==bool_operation.c.id"), diff --git a/code/backend/nest_backend/database/tables/Composed.py b/code/backend/nest_backend/database/tables/Composed.py index 9da3d0a..d97681b 100644 --- a/code/backend/nest_backend/database/tables/Composed.py +++ b/code/backend/nest_backend/database/tables/Composed.py @@ -7,7 +7,7 @@ from ..base import ext class Composed(ext.Model): __tablename__ = "composed" - rid = ext.Column(ext.Integer, ext.ForeignKey("repository.id"), primary_key=True) + rid = ext.Column(ext.Integer, ext.ForeignKey("repository.id", ondelete="CASCADE"), primary_key=True) snowflake = ext.Column(ext.String, ext.ForeignKey("tweet.snowflake"), primary_key=True) # Relationships repository = ext.relationship("Repository", back_populates="tweets") diff --git a/code/backend/nest_backend/database/tables/Condition.py b/code/backend/nest_backend/database/tables/Condition.py index ff15aba..4871c03 100644 --- a/code/backend/nest_backend/database/tables/Condition.py +++ b/code/backend/nest_backend/database/tables/Condition.py @@ -9,12 +9,12 @@ from .Enums import ConditionType class Condition(ext.Model): __tablename__ = "condition" id = ext.Column(ext.Integer, primary_key=True) - type = ext.Column(ext.Enum(ConditionType), nullable=False) + type = ext.Column(ext.Enum(ConditionType), nullable=False, default=ConditionType.hashtag) content = ext.Column(ext.String, nullable=False) # FK repository_id = ext.Column(ext.Integer, ext.ForeignKey("repository.id", ondelete="CASCADE")) # Relationships - repository = ext.relationship("Repository", back_populates="conditions", cascade="all, delete") + repository = ext.relationship("Repository", back_populates="conditions") tweets = ext.relationship("Contains", back_populates="condition") operations = ext.relationship("BoolOperation", back_populates="condition") diff --git a/code/backend/nest_backend/database/tables/Contains.py b/code/backend/nest_backend/database/tables/Contains.py index dbbaf50..b1b0f87 100644 --- a/code/backend/nest_backend/database/tables/Contains.py +++ b/code/backend/nest_backend/database/tables/Contains.py @@ -7,8 +7,8 @@ from ..base import ext class Contains(ext.Model): __tablename__ = "contains" - cid = ext.Column(ext.Integer, ext.ForeignKey("condition.id"), primary_key=True) - snowflake = ext.Column(ext.String, ext.ForeignKey("tweet.snowflake"), primary_key=True) + cid = ext.Column(ext.Integer, ext.ForeignKey("condition.id", ondelete="CASCADE"), primary_key=True) + snowflake = ext.Column(ext.String, ext.ForeignKey("tweet.snowflake", ondelete="CASCADE"), primary_key=True) # Relationships condition = ext.relationship("Condition", back_populates="tweets") tweet = ext.relationship("Tweet", back_populates="conditions") \ No newline at end of file diff --git a/code/backend/nest_backend/database/tables/Notification.py b/code/backend/nest_backend/database/tables/Notification.py index 823eafd..4fa1db6 100644 --- a/code/backend/nest_backend/database/tables/Notification.py +++ b/code/backend/nest_backend/database/tables/Notification.py @@ -10,7 +10,7 @@ class Notification(ext.Model): id = ext.Column(ext.Integer, primary_key=True) ora = ext.Column(ext.DateTime, nullable=False) # Foreign Key - alert_id = ext.Column(ext.Integer, ext.ForeignKey("alert.id"), nullable=False) + alert_id = ext.Column(ext.Integer, ext.ForeignKey("alert.id", ondelete="CASCADE"), nullable=False) # Relationships alert = ext.relationship("Alert", back_populates="notifications") diff --git a/code/backend/nest_backend/database/tables/Repository.py b/code/backend/nest_backend/database/tables/Repository.py index a3e1803..ea93297 100644 --- a/code/backend/nest_backend/database/tables/Repository.py +++ b/code/backend/nest_backend/database/tables/Repository.py @@ -22,9 +22,9 @@ class Repository(ext.Model): # Relationships owner = ext.relationship("User", back_populates="owner_of") - authorizations = ext.relationship("Authorization", back_populates="repository", cascade="all, delete") - tweets = ext.relationship("Composed", back_populates="repository", cascade="all, delete") - alerts = ext.relationship("Alert", back_populates="repository", cascade="all, delete") + authorizations = ext.relationship("Authorization", back_populates="repository") + tweets = ext.relationship("Composed", back_populates="repository") + alerts = ext.relationship("Alert", back_populates="repository") conditions = ext.relationship("Condition", back_populates="repository") def to_json(self): diff --git a/code/backend/nest_backend/routes/repository/repository.py b/code/backend/nest_backend/routes/repository/repository.py index 47c42d6..e21c7e1 100644 --- a/code/backend/nest_backend/routes/repository/repository.py +++ b/code/backend/nest_backend/routes/repository/repository.py @@ -191,9 +191,10 @@ def page_repository(rid): return json_error("Missing one or more parameters in repository json."), 400 # Users will be tolerated if they change parameters they're not supposed to touch. We'll ignore them for now. try: - repository.evaluation_mode = request.json['evaluation_mode'] + evaluation_mode = ConditionMode(request.json['evaluation_mode']) except KeyError: return json_error("Unknown `type` specified."), 400 + repository.evaluation_mode = evaluation_mode repository.name = request.json['name'] repository.is_active = request.json['is_active'] ids = [c['id'] for c in request.json['conditions'] if c['id']] @@ -205,11 +206,10 @@ def page_repository(rid): # Create brand new conditions for c in request.json['conditions']: if not c['id']: - if (type_ := c['type']) is not None: - try: - type_ = ConditionType(type_) - except KeyError: - return json_error("Unknown `type` specified."), 400 + try: + type_ = ConditionType(c['type']) + except KeyError: + return json_error("Unknown `type` specified."), 400 ext.session.add(Condition(type=type_, content=c['content'], repository_id=rid)) ext.session.commit() return json_success(repository.to_json()), 200 diff --git a/code/backend/nest_backend/routes/users/user.py b/code/backend/nest_backend/routes/users/user.py index b8895f4..4cbb6da 100644 --- a/code/backend/nest_backend/routes/users/user.py +++ b/code/backend/nest_backend/routes/users/user.py @@ -133,7 +133,7 @@ def page_user(email): except Exception: ext.session.rollback() return json_error("Could not delete the user."), 500 - return json_success("The user has been deleted.") + return json_success("The user has been deleted."), 200 elif request.method == "PATCH": if not email == user.email and not user.isAdmin: return json_error("Thou art not authorized."), 403 @@ -143,4 +143,4 @@ def page_user(email): if request.json.get("password"): target.password = gen_password(request.json.get("password")) ext.session.commit() - return json_success(target.to_json()) + return json_success(target.to_json()), 200