From 3d20f1a252e5eed29f21e4ac62c6231142f24f07 Mon Sep 17 00:00:00 2001 From: Lorenzo Balugani Date: Fri, 28 May 2021 11:19:40 +0200 Subject: [PATCH] Fixes and changes Changed the way repos are deleted, fixed a few issues. --- nest_backend/database/tables/Repository.py | 1 + .../routes/repository/alerts/alert.py | 2 +- .../repository/alerts/repository_alerts.py | 4 +- .../authorizations/authorization.py | 2 +- .../repository_authorizations.py | 2 +- .../routes/repository/conditions/condition.py | 2 +- .../conditions/repository_conditions.py | 2 +- .../routes/repository/repositories.py | 12 +++- nest_backend/routes/repository/repository.py | 62 +++++-------------- .../repository/tweets/repository_tweets.py | 2 +- nest_backend/routes/users/users.py | 4 +- 11 files changed, 33 insertions(+), 62 deletions(-) diff --git a/nest_backend/database/tables/Repository.py b/nest_backend/database/tables/Repository.py index 053474e..08486f3 100644 --- a/nest_backend/database/tables/Repository.py +++ b/nest_backend/database/tables/Repository.py @@ -15,6 +15,7 @@ class Repository(ext.Model): 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) # Foreign Keys diff --git a/nest_backend/routes/repository/alerts/alert.py b/nest_backend/routes/repository/alerts/alert.py index 315a800..a5f2e90 100644 --- a/nest_backend/routes/repository/alerts/alert.py +++ b/nest_backend/routes/repository/alerts/alert.py @@ -149,7 +149,7 @@ def page_alert(aid): """ user = find_user(get_jwt_identity()) alert = Alert.query.filter_by(id=aid).first() - if not alert: + if not alert or alert.repository.is_deleted: return json_error("Could not find alert.", ALERT_NOT_FOUND), 404 if alert.repository not in [a.repository for a in user.authorizations] + user.owner_of: return json_error("You are not authorized to proceed.", USER_NOT_AUTHORIZED), 403 diff --git a/nest_backend/routes/repository/alerts/repository_alerts.py b/nest_backend/routes/repository/alerts/repository_alerts.py index 89ac6c8..785d2f9 100644 --- a/nest_backend/routes/repository/alerts/repository_alerts.py +++ b/nest_backend/routes/repository/alerts/repository_alerts.py @@ -71,7 +71,7 @@ def page_repository_alerts(rid): - alert-related """ - repository = Repository.query.filter_by(id=rid).first() + repository = Repository.query.filter_by(id=rid, is_deleted=False).first() if not repository: return json_error("Could not find repository", REPOSITORY_NOT_FOUND), 404 user = find_user(get_jwt_identity()) @@ -116,7 +116,7 @@ def page_repository_alerts(rid): return json_error("Missing `content` parameter.", GENERIC_MISSING_FIELDS), 400 if type_ == ConditionType.hashtag: content = hashtag_validator(content) - c = Condition(content=content, type=type_, repository_id=rid) + c = Condition(content=content, type=type_) ext.session.add(c) ext.session.commit() conn = MadeOf(aid=alert.id, cid=c.id) diff --git a/nest_backend/routes/repository/authorizations/authorization.py b/nest_backend/routes/repository/authorizations/authorization.py index 94917f7..cf6b932 100644 --- a/nest_backend/routes/repository/authorizations/authorization.py +++ b/nest_backend/routes/repository/authorizations/authorization.py @@ -39,7 +39,7 @@ def page_authorization(rid, email): tags: - repository-related """ - repository = Repository.query.filter_by(id=rid).first() + repository = Repository.query.filter_by(id=rid, is_deleted=False).first() user = find_user(get_jwt_identity()) if not repository: return json_error("Could not find the repository.", REPOSITORY_NOT_FOUND), 404 diff --git a/nest_backend/routes/repository/authorizations/repository_authorizations.py b/nest_backend/routes/repository/authorizations/repository_authorizations.py index 2778a77..9afc0a2 100644 --- a/nest_backend/routes/repository/authorizations/repository_authorizations.py +++ b/nest_backend/routes/repository/authorizations/repository_authorizations.py @@ -112,7 +112,7 @@ def page_repository_authorizations(rid): - repository-related """ - repository = Repository.query.filter_by(id=rid).first() + repository = Repository.query.filter_by(id=rid, is_deleted=False).first() if not repository: return json_error("Could not find repository", REPOSITORY_NOT_FOUND), 404 user = find_user(get_jwt_identity()) diff --git a/nest_backend/routes/repository/conditions/condition.py b/nest_backend/routes/repository/conditions/condition.py index c8fe3a3..57a8e38 100644 --- a/nest_backend/routes/repository/conditions/condition.py +++ b/nest_backend/routes/repository/conditions/condition.py @@ -106,7 +106,7 @@ def page_condition(cid): """ condition = Condition.query.filter_by(id=cid).first() user = find_user(get_jwt_identity()) - if not condition: + if not condition or condition.repository.is_deleted: return json_error("Could not find the condition.", CONDITION_NOT_FOUND), 404 if condition.repository not in [a.repository for a in user.authorizations] + user.owner_of and not user.isAdmin: return json_error("You lack the authorization to proceed, pal.", USER_NOT_AUTHORIZED), 403 diff --git a/nest_backend/routes/repository/conditions/repository_conditions.py b/nest_backend/routes/repository/conditions/repository_conditions.py index 963c23b..24458c4 100644 --- a/nest_backend/routes/repository/conditions/repository_conditions.py +++ b/nest_backend/routes/repository/conditions/repository_conditions.py @@ -73,7 +73,7 @@ def page_repository_conditions(rid): - repository-related """ - repository = Repository.query.filter_by(id=rid).first() + repository = Repository.query.filter_by(id=rid, is_deleted=False).first() if not repository: return json_error("Could not find repository", REPOSITORY_NOT_FOUND), 404 user = find_user(get_jwt_identity()) diff --git a/nest_backend/routes/repository/repositories.py b/nest_backend/routes/repository/repositories.py index aba4a83..7e51c3a 100644 --- a/nest_backend/routes/repository/repositories.py +++ b/nest_backend/routes/repository/repositories.py @@ -5,6 +5,7 @@ from nest_backend.gestione import * import datetime from flask_cors import cross_origin from nest_backend.errors import * +from nest_crawler.repo_search import search_repo_conditions @cross_origin() @@ -62,7 +63,7 @@ def page_repositories(): user = find_user(get_jwt_identity()) if request.method == "GET": owner = Repository.query.filter_by(owner_id=user.email) - spectator = Authorization.query.filter_by(email=user.email).join(Repository) + spectator = Authorization.query.filter_by(email=user.email).join(Repository).filter_by(is_deleted=False) if request.args.get("onlyActive"): owner = owner.filter_by(is_active=True) spectator = spectator.filter(Repository.is_active) @@ -71,7 +72,7 @@ def page_repositories(): spectator = spectator.filter(not Repository.is_active) owner = owner.all() spectator = spectator.all() - return json_success([r.to_json() for r in owner] + [r.repository.to_json() for r in spectator]) + return json_success([r.to_json() for r in owner if not r.is_deleted] + [r.repository.to_json() for r in spectator if not r.is_deleted]) elif request.method == "POST": # Users will be tolerated if they change parameters they're not supposed to touch. We'll ignore them for now. if not request.json.get("name") or not request.json.get("conditions") or not str( @@ -88,7 +89,8 @@ def page_repositories(): ext.session.add(repository) ext.session.commit() conditions = [c for c in repository.conditions if c.id not in [a['id'] for a in request.json['conditions'] if - a['id'] in [b.id for b in repository.conditions]]] + a['id'] in [b.id for b in + repository.conditions]]] for c in conditions: ext.session.delete(c) ext.session.commit() @@ -104,4 +106,8 @@ def page_repositories(): repository.is_active = True repository.start = datetime.datetime.now() ext.session.commit() + try: + search_repo_conditions(repository.id) + except Exception: + return json_success(repository.to_json()), 201 return json_success(repository.to_json()), 201 diff --git a/nest_backend/routes/repository/repository.py b/nest_backend/routes/repository/repository.py index f14037c..22548d7 100644 --- a/nest_backend/routes/repository/repository.py +++ b/nest_backend/routes/repository/repository.py @@ -154,7 +154,7 @@ def page_repository(rid): - repository-related """ user = find_user(get_jwt_identity()) - repository = Repository.query.filter_by(id=rid).first() + repository = Repository.query.filter_by(id=rid, is_deleted=False).first() if not repository: return json_error("Could not find repository.", REPOSITORY_NOT_FOUND), 404 if request.method == "GET": @@ -181,50 +181,7 @@ def page_repository(rid): if repository.owner_id != user.email and not user.isAdmin: return json_error("You are not the owner of this repository.", REPOSITORY_NOT_OWNER), 403 try: - # Deleting Tweets... - tweets = [t.tweet for t in repository.tweets] - for tweet in tweets: - if len(tweet.repositories) < 2: - # Delete Tweets if only attached to this repo - for conn in tweet.repositories: - ext.session.delete(conn) - for conn in tweet.conditions: - ext.session.delete(conn) - ext.session.delete(tweet) - ext.session.commit() - else: - conns = [conn for conn in tweet.repositories if conn.rid == rid] - for conn in conns: - ext.session.delete(conn) - ext.session.commit() - # Deleting authorizations... - for auth in repository.authorizations: - ext.session.delete(auth) - ext.session.commit() - # Deleting conditions... - for condition in repository.conditions: - for c in condition.tweets: - ext.session.delete(c) - for c in condition.alerts: - ext.session.delete(c) - ext.session.commit() - ext.session.delete(condition) - ext.session.commit() - # Deleting Alerts... - for alert in repository.alerts: - for elem in alert.conditions: - condition = elem.condition - ext.session.delete(elem) - ext.session.commit() - if not condition.repository_id: - ext.session.delete(condition) - ext.session.commit() - for notification in alert.notifications: - ext.session.delete(notification) - ext.session.commit() - ext.session.delete(alert) - ext.session.commit() - ext.session.delete(repository) + repository.is_deleted = True ext.session.commit() except Exception as e: ext.session.rollback() @@ -244,9 +201,18 @@ def page_repository(rid): ext.session.commit() ids = [c['id'] for c in request.json['conditions'] if c['id']] # Delete no longer needed conditions. - for c in repository.conditions: - if c.id not in ids: - ext.session.delete(c) + try: + for c in repository.conditions: + if c.id not in ids: + for t in c.tweets: + ext.session.delete(t) + for a in c.alerts: + ext.session.delete(a) + ext.session.commit() + ext.session.delete(c) + ext.session.commit() + except Exception as e: + return json_error("Could not delete conditions.", GENERIC_UFO), 500 # Create brand new conditions for c in request.json['conditions']: if not c['id']: diff --git a/nest_backend/routes/repository/tweets/repository_tweets.py b/nest_backend/routes/repository/tweets/repository_tweets.py index beb4e71..b882e41 100644 --- a/nest_backend/routes/repository/tweets/repository_tweets.py +++ b/nest_backend/routes/repository/tweets/repository_tweets.py @@ -43,7 +43,7 @@ def page_repository_tweets(rid): - repository-related """ - repository = Repository.query.filter_by(id=rid).first() + repository = Repository.query.filter_by(id=rid, is_deleted=False).first() if not repository: return json_error("Could not find repository", REPOSITORY_NOT_FOUND), 404 user = find_user(get_jwt_identity()) diff --git a/nest_backend/routes/users/users.py b/nest_backend/routes/users/users.py index 45a1670..3225876 100644 --- a/nest_backend/routes/users/users.py +++ b/nest_backend/routes/users/users.py @@ -29,7 +29,7 @@ def page_users(): application/json: schema: Error tags: - - admin-only + - user-related post: summary: Creates a user. security: @@ -65,8 +65,6 @@ def page_users(): """ user = find_user(get_jwt_identity()) if request.method == "GET": - if not user.isAdmin: - return json_error("User is not admin. Thou art not authorized", USER_NOT_ADMIN), 403 users = User.query.all() return json_success([user.to_json() for user in users]), 200 if request.method == "POST":