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

fixato un po' di smell code segnalato da sonarqube

This commit is contained in:
stefanogoldoni 2021-05-28 19:56:42 +02:00
parent 02b22140eb
commit 12e79f241e
5 changed files with 43 additions and 43 deletions

View file

@ -4,7 +4,7 @@ from flask_jwt_extended import jwt_required, get_jwt_identity
from nest_backend.gestione import * from nest_backend.gestione import *
from flask_cors import cross_origin from flask_cors import cross_origin
import datetime import datetime
from nest_backend.errors import * import nest_backend.errors as errors
@cross_origin() @cross_origin()
@ -150,16 +150,16 @@ def page_alert(aid):
user = find_user(get_jwt_identity()) user = find_user(get_jwt_identity())
alert = Alert.query.filter_by(id=aid).first() alert = Alert.query.filter_by(id=aid).first()
if not alert or alert.repository.is_deleted: if not alert or alert.repository.is_deleted:
return json_error("Could not find alert.", ALERT_NOT_FOUND), 404 return json_error("Could not find alert.", errors.ALERT_NOT_FOUND), 404
if alert.repository not in [a.repository for a in user.authorizations] + user.owner_of: 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 return json_error("You are not authorized to proceed.", errors.USER_NOT_AUTHORIZED), 403
if request.method == "GET": if request.method == "GET":
return json_success(alert.to_json()), 200 return json_success(alert.to_json()), 200
if alert.repository not in user.owner_of: if alert.repository not in user.owner_of:
return json_error("You are not authorized to proceed.", REPOSITORY_NOT_OWNER), 403 return json_error("You are not authorized to proceed.", errors.REPOSITORY_NOT_OWNER), 403
if request.method == "PATCH": if request.method == "PATCH":
if request.json is None: if request.json is None:
return json_error("Missing json content.", GENERIC_NO_JSON), 400 return json_error("Missing json content.", errors.GENERIC_NO_JSON), 400
if 'name' in request.json: if 'name' in request.json:
alert.name = request.json['name'] alert.name = request.json['name']
@ -171,9 +171,9 @@ def page_alert(aid):
try: try:
alert.evaluation_mode = ConditionMode(request.json['evaluation_mode']) alert.evaluation_mode = ConditionMode(request.json['evaluation_mode'])
except KeyError: except KeyError:
return json_error("Unknown `evaluation_mode` specified.", GENERIC_ENUM_INVALID), 400 return json_error("Unknown `evaluation_mode` specified.", errors.GENERIC_ENUM_INVALID), 400
except Exception as e: except Exception as e:
return json_error("Unknown error:" + str(e), GENERIC_UFO), 400 return json_error("Unknown error:" + str(e), errors.GENERIC_UFO), 400
ext.session.commit() ext.session.commit()
return json_success(alert.to_json()), 200 return json_success(alert.to_json()), 200
elif request.method == "DELETE": elif request.method == "DELETE":
@ -191,14 +191,14 @@ def page_alert(aid):
ext.session.delete(alert) ext.session.delete(alert)
ext.session.commit() ext.session.commit()
except Exception as e: except Exception as e:
return json_error("Something went wrong while deleting alert.", ALERT_DELETION_FAILURE), 500 return json_error("Something went wrong while deleting alert.", errors.ALERT_DELETION_FAILURE), 500
return json_success("Deletion completed."), 204 return json_success("Deletion completed."), 204
elif request.method == "PUT": elif request.method == "PUT":
if request.json is None: if request.json is None:
return json_error("Missing json content.", GENERIC_NO_JSON), 400 return json_error("Missing json content.", errors.GENERIC_NO_JSON), 400
if not json_request_authorizer(request.json, alert): if not json_request_authorizer(request.json, alert):
return json_error("Missing one or more parameters in alert json.", GENERIC_MISSING_FIELDS), 400 return json_error("Missing one or more parameters in alert json.", errors.GENERIC_MISSING_FIELDS), 400
alert.limit = request.json['limit'] alert.limit = request.json['limit']
alert.name = request.json['name'] alert.name = request.json['name']
alert.window_size = request.json['window_size'] alert.window_size = request.json['window_size']
@ -206,9 +206,9 @@ def page_alert(aid):
try: try:
alert.evaluation_mode = ConditionMode(mode) alert.evaluation_mode = ConditionMode(mode)
except KeyError: except KeyError:
return json_error("Unknown `evaluation_mode` specified.", GENERIC_ENUM_INVALID), 400 return json_error("Unknown `evaluation_mode` specified.", errors.GENERIC_ENUM_INVALID), 400
except Exception as e: except Exception as e:
return json_error("Unknown error:" + str(e), GENERIC_UFO), 400 return json_error("Unknown error:" + str(e), errors.GENERIC_UFO), 400
if request.json['conditions'] is not None: if request.json['conditions'] is not None:
# Wow very pythonic so much wow # Wow very pythonic so much wow
# Obtain list of no longer needed connections # Obtain list of no longer needed connections
@ -223,15 +223,15 @@ def page_alert(aid):
for c in request.json['conditions']: for c in request.json['conditions']:
if not c.get("id"): if not c.get("id"):
if (type_ := c.get("type")) is None: if (type_ := c.get("type")) is None:
return json_error("Missing `type` parameter.", GENERIC_MISSING_FIELDS), 400 return json_error("Missing `type` parameter.", errors.GENERIC_MISSING_FIELDS), 400
try: try:
type_ = ConditionType(type_) type_ = ConditionType(type_)
except KeyError: except KeyError:
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400 return json_error("Unknown `type` specified.", errors.GENERIC_ENUM_INVALID), 400
except Exception as e: except Exception as e:
return json_error("Unknown error: " + str(e)), 400 return json_error("Unknown error: " + str(e)), 400
if not (content := c.get("content")): if not (content := c.get("content")):
return json_error("Missing `content` parameter.", GENERIC_MISSING_FIELDS), 400 return json_error("Missing `content` parameter.", errors.GENERIC_MISSING_FIELDS), 400
if type_ == ConditionType.hashtag: if type_ == ConditionType.hashtag:
content = hashtag_validator(content) content = hashtag_validator(content)
con = Condition(content=content, type=type_, repository_id=alert.repository_id) con = Condition(content=content, type=type_, repository_id=alert.repository_id)

View file

@ -3,7 +3,7 @@ from nest_backend.database import *
from flask_jwt_extended import jwt_required, get_jwt_identity from flask_jwt_extended import jwt_required, get_jwt_identity
from nest_backend.gestione import * from nest_backend.gestione import *
from flask_cors import cross_origin from flask_cors import cross_origin
from nest_backend.errors import * import nest_backend.errors as errors
@cross_origin() @cross_origin()
@ -73,30 +73,30 @@ def page_repository_alerts(rid):
repository = Repository.query.filter_by(id=rid, is_deleted=False).first() repository = Repository.query.filter_by(id=rid, is_deleted=False).first()
if not repository: if not repository:
return json_error("Could not find repository", REPOSITORY_NOT_FOUND), 404 return json_error("Could not find repository", errors.REPOSITORY_NOT_FOUND), 404
user = find_user(get_jwt_identity()) user = find_user(get_jwt_identity())
if user.email != repository.owner_id: if user.email != repository.owner_id:
return json_error("You are not authorized.", REPOSITORY_NOT_OWNER), 403 return json_error("You are not authorized.", errors.REPOSITORY_NOT_OWNER), 403
if request.method == "GET": if request.method == "GET":
return json_success([alert.to_json() for alert in repository.alerts]) return json_success([alert.to_json() for alert in repository.alerts])
if request.method == "POST": if request.method == "POST":
if 'name' not in request.json: if 'name' not in request.json:
return json_error("Missing name.", ALERT_NO_NAME), 400 return json_error("Missing name.", errors.ALERT_NO_NAME), 400
if 'limit' not in request.json: if 'limit' not in request.json:
return json_error('Missing limit', ALERT_NO_LIMIT), 400 return json_error('Missing limit', errors.ALERT_NO_LIMIT), 400
if 'window_size' not in request.json: if 'window_size' not in request.json:
return json_error('Missing window size', ALERT_NO_WINDOW), 400 return json_error('Missing window size', errors.ALERT_NO_WINDOW), 400
if (mode := request.json.get("evaluation_mode")) is not None: if (mode := request.json.get("evaluation_mode")) is not None:
try: try:
mode = ConditionMode(mode) mode = ConditionMode(mode)
except KeyError: except KeyError:
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400 return json_error("Unknown `type` specified.", errors.GENERIC_ENUM_INVALID), 400
except Exception as e: except Exception as e:
return json_error("Unknown error:" + str(e), GENERIC_UFO), 400 return json_error("Unknown error:" + str(e), errors.GENERIC_UFO), 400
else: else:
return json_error("Evaluation mode was not provided.", ALERT_NO_EVALUATION), 400 return json_error("Evaluation mode was not provided.", errors.ALERT_NO_EVALUATION), 400
alert = Alert(name=request.json['name'], limit=request.json['limit'], window_size=request.json['window_size'], alert = Alert(name=request.json['name'], limit=request.json['limit'], window_size=request.json['window_size'],
repository_id=rid, evaluation_mode=mode) repository_id=rid, evaluation_mode=mode)
@ -105,15 +105,15 @@ def page_repository_alerts(rid):
if request.json['conditions'] is not None: if request.json['conditions'] is not None:
for condition in request.json['conditions']: for condition in request.json['conditions']:
if (type_ := condition.get("type")) is None: if (type_ := condition.get("type")) is None:
return json_error("Missing `type` parameter.", GENERIC_MISSING_FIELDS), 400 return json_error("Missing `type` parameter.", errors.GENERIC_MISSING_FIELDS), 400
try: try:
type_ = ConditionType(type_) type_ = ConditionType(type_)
except KeyError: except KeyError:
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400 return json_error("Unknown `type` specified.", errors.GENERIC_ENUM_INVALID), 400
except Exception as e: except Exception as e:
return json_error("Unknown error: " + str(e)), 400 return json_error("Unknown error: " + str(e)), 400
if not (content := condition.get("content")): if not (content := condition.get("content")):
return json_error("Missing `content` parameter.", GENERIC_MISSING_FIELDS), 400 return json_error("Missing `content` parameter.", errors.GENERIC_MISSING_FIELDS), 400
if type_ == ConditionType.hashtag: if type_ == ConditionType.hashtag:
content = hashtag_validator(content) content = hashtag_validator(content)
c = Condition(content=content, type=type_) c = Condition(content=content, type=type_)

View file

@ -3,7 +3,7 @@ from nest_backend.database import *
from flask_jwt_extended import jwt_required, get_jwt_identity from flask_jwt_extended import jwt_required, get_jwt_identity
from nest_backend.gestione import * from nest_backend.gestione import *
from flask_cors import cross_origin from flask_cors import cross_origin
from nest_backend.errors import * import nest_backend.errors as errors
@cross_origin() @cross_origin()
@ -107,25 +107,25 @@ def page_condition(cid):
condition = Condition.query.filter_by(id=cid).first() condition = Condition.query.filter_by(id=cid).first()
user = find_user(get_jwt_identity()) user = find_user(get_jwt_identity())
if not condition or condition.repository.is_deleted: if not condition or condition.repository.is_deleted:
return json_error("Could not find the condition.", CONDITION_NOT_FOUND), 404 return json_error("Could not find the condition.", errors.CONDITION_NOT_FOUND), 404
if condition.repository not in [a.repository for a in user.authorizations] + user.owner_of and not user.isAdmin: 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 return json_error("You lack the authorization to proceed, pal.", errors.USER_NOT_AUTHORIZED), 403
if request.method == "GET": if request.method == "GET":
return json_success(condition.to_json()), 200 return json_success(condition.to_json()), 200
if condition.repository not in user.owner_of and not user.isAdmin: if condition.repository not in user.owner_of and not user.isAdmin:
return json_error("You lack the authorization to proceed, pal.", USER_NOT_AUTHORIZED), 403 return json_error("You lack the authorization to proceed, pal.", errors.USER_NOT_AUTHORIZED), 403
if request.method == "PATCH": if request.method == "PATCH":
if request.json is None: if request.json is None:
return json_error("Missing json content.", GENERIC_NO_JSON), 400 return json_error("Missing json content.", errors.GENERIC_NO_JSON), 400
if (type_ := request.json.get("type")) is not None: if (type_ := request.json.get("type")) is not None:
try: try:
type_ = ConditionType(type_) type_ = ConditionType(type_)
condition.type = type_ condition.type = type_
except KeyError: except KeyError:
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400 return json_error("Unknown `type` specified.", errors.GENERIC_ENUM_INVALID), 400
except Exception as e: except Exception as e:
return json_error("Unknown error:" + str(e), GENERIC_UFO), 400 return json_error("Unknown error:" + str(e), errors.GENERIC_UFO), 400
if content := request.json.get("content"): if content := request.json.get("content"):
condition.content = content condition.content = content

View file

@ -5,7 +5,7 @@ from nest_backend.gestione import repository_auth, json_error, json_success, Con
from nest_backend.database import ext from nest_backend.database import ext
from flask_cors import cross_origin from flask_cors import cross_origin
from nest_backend.gestione import hashtag_validator from nest_backend.gestione import hashtag_validator
from nest_backend.errors import * import nest_backend.errors as errors
@cross_origin() @cross_origin()
@ -75,34 +75,34 @@ def page_repository_conditions(rid):
repository = Repository.query.filter_by(id=rid, is_deleted=False).first() repository = Repository.query.filter_by(id=rid, is_deleted=False).first()
if not repository: if not repository:
return json_error("Could not find repository", REPOSITORY_NOT_FOUND), 404 return json_error("Could not find repository", errors.REPOSITORY_NOT_FOUND), 404
user = find_user(get_jwt_identity()) user = find_user(get_jwt_identity())
if request.method == "GET": if request.method == "GET":
try: try:
return json_success([u.to_json() for u in repository.conditions]) return json_success([u.to_json() for u in repository.conditions])
except Exception as e: except Exception as e:
return json_error("Unknown error:" + str(e), GENERIC_UFO), 400 return json_error("Unknown error:" + str(e), errors.GENERIC_UFO), 400
if user.email != repository.owner_id: if user.email != repository.owner_id:
return json_error("You are not authorized.", REPOSITORY_NOT_OWNER), 403 return json_error("You are not authorized.", errors.REPOSITORY_NOT_OWNER), 403
if request.method == "POST": if request.method == "POST":
if request.json is None: if request.json is None:
return json_error("Missing json content.", GENERIC_NO_JSON), 400 return json_error("Missing json content.", errors.GENERIC_NO_JSON), 400
if (type_ := request.json.get("type")) is None: if (type_ := request.json.get("type")) is None:
return json_error("Missing `type` parameter.", GENERIC_MISSING_FIELDS), 400 return json_error("Missing `type` parameter.", errors.GENERIC_MISSING_FIELDS), 400
try: try:
type_ = ConditionType(type_) type_ = ConditionType(type_)
except KeyError: except KeyError:
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400 return json_error("Unknown `type` specified.", errors.GENERIC_ENUM_INVALID), 400
except Exception as e: except Exception as e:
return json_error("Unknown error: " + str(e)), 400 return json_error("Unknown error: " + str(e)), 400
if not (content := request.json.get("content")): if not (content := request.json.get("content")):
return json_error("Missing `content` parameter.", GENERIC_MISSING_FIELDS), 400 return json_error("Missing `content` parameter.", errors.GENERIC_MISSING_FIELDS), 400
if type_ == ConditionType.hashtag: if type_ == ConditionType.hashtag:
content = hashtag_validator(content) content = hashtag_validator(content)
condition = Condition(content=content, type=type_, repository_id=rid) condition = Condition(content=content, type=type_, repository_id=rid)