1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-22 04:54:18 +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 flask_cors import cross_origin
import datetime
from nest_backend.errors import *
import nest_backend.errors as errors
@cross_origin()
@ -150,16 +150,16 @@ def page_alert(aid):
user = find_user(get_jwt_identity())
alert = Alert.query.filter_by(id=aid).first()
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:
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":
return json_success(alert.to_json()), 200
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.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:
alert.name = request.json['name']
@ -171,9 +171,9 @@ def page_alert(aid):
try:
alert.evaluation_mode = ConditionMode(request.json['evaluation_mode'])
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:
return json_error("Unknown error:" + str(e), GENERIC_UFO), 400
return json_error("Unknown error:" + str(e), errors.GENERIC_UFO), 400
ext.session.commit()
return json_success(alert.to_json()), 200
elif request.method == "DELETE":
@ -191,14 +191,14 @@ def page_alert(aid):
ext.session.delete(alert)
ext.session.commit()
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
elif request.method == "PUT":
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):
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.name = request.json['name']
alert.window_size = request.json['window_size']
@ -206,9 +206,9 @@ def page_alert(aid):
try:
alert.evaluation_mode = ConditionMode(mode)
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:
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:
# Wow very pythonic so much wow
# Obtain list of no longer needed connections
@ -223,15 +223,15 @@ def page_alert(aid):
for c in request.json['conditions']:
if not c.get("id"):
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:
type_ = ConditionType(type_)
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:
return json_error("Unknown error: " + str(e)), 400
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:
content = hashtag_validator(content)
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 nest_backend.gestione import *
from flask_cors import cross_origin
from nest_backend.errors import *
import nest_backend.errors as errors
@cross_origin()
@ -73,30 +73,30 @@ def page_repository_alerts(rid):
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
return json_error("Could not find repository", errors.REPOSITORY_NOT_FOUND), 404
user = find_user(get_jwt_identity())
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":
return json_success([alert.to_json() for alert in repository.alerts])
if request.method == "POST":
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:
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:
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:
try:
mode = ConditionMode(mode)
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:
return json_error("Unknown error:" + str(e), GENERIC_UFO), 400
return json_error("Unknown error:" + str(e), errors.GENERIC_UFO), 400
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'],
repository_id=rid, evaluation_mode=mode)
@ -105,15 +105,15 @@ def page_repository_alerts(rid):
if request.json['conditions'] is not None:
for condition in request.json['conditions']:
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:
type_ = ConditionType(type_)
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:
return json_error("Unknown error: " + str(e)), 400
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:
content = hashtag_validator(content)
c = Condition(content=content, type=type_)

View file

@ -4,7 +4,7 @@ from nest_backend.gestione import repository_auth, json_error, json_success, fin
from nest_backend.database import ext, User, Authorization, Repository
from flask_cors import cross_origin
from nest_backend.gestione import hashtag_validator
import nest_backend.errors as errors
import nest_backend.errors as errors
@cross_origin()

View file

@ -3,7 +3,7 @@ from nest_backend.database import *
from flask_jwt_extended import jwt_required, get_jwt_identity
from nest_backend.gestione import *
from flask_cors import cross_origin
from nest_backend.errors import *
import nest_backend.errors as errors
@cross_origin()
@ -107,25 +107,25 @@ def page_condition(cid):
condition = Condition.query.filter_by(id=cid).first()
user = find_user(get_jwt_identity())
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:
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":
return json_success(condition.to_json()), 200
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.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:
try:
type_ = ConditionType(type_)
condition.type = type_
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:
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"):
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 flask_cors import cross_origin
from nest_backend.gestione import hashtag_validator
from nest_backend.errors import *
import nest_backend.errors as errors
@cross_origin()
@ -75,34 +75,34 @@ def page_repository_conditions(rid):
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
return json_error("Could not find repository", errors.REPOSITORY_NOT_FOUND), 404
user = find_user(get_jwt_identity())
if request.method == "GET":
try:
return json_success([u.to_json() for u in repository.conditions])
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:
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.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:
return json_error("Missing `type` parameter.", GENERIC_MISSING_FIELDS), 400
return json_error("Missing `type` parameter.", errors.GENERIC_MISSING_FIELDS), 400
try:
type_ = ConditionType(type_)
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:
return json_error("Unknown error: " + str(e)), 400
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:
content = hashtag_validator(content)
condition = Condition(content=content, type=type_, repository_id=rid)