1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-25 06:24:19 +00:00

Fixes and changes

Changed the way repos are deleted, fixed a few issues.
This commit is contained in:
Lorenzo Balugani 2021-05-28 11:19:40 +02:00
parent 1da9543a8c
commit 3d20f1a252
11 changed files with 33 additions and 62 deletions

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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())

View file

@ -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

View file

@ -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())

View file

@ -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

View file

@ -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']:

View file

@ -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())

View file

@ -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":