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

fixato un po' di smell code segnalato da sonarqube

This commit is contained in:
stefanogoldoni 2021-05-28 19:51:12 +02:00
parent 2014aa9181
commit 4411f0310c
5 changed files with 25 additions and 25 deletions

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()
@ -42,12 +42,12 @@ def page_authorization(rid, email):
repository = Repository.query.filter_by(id=rid, is_deleted=False).first() repository = Repository.query.filter_by(id=rid, is_deleted=False).first()
user = find_user(get_jwt_identity()) user = find_user(get_jwt_identity())
if not repository: if not repository:
return json_error("Could not find the repository.", REPOSITORY_NOT_FOUND), 404 return json_error("Could not find the repository.", errors.REPOSITORY_NOT_FOUND), 404
if user != repository.owner: if user != repository.owner:
return json_error("You are not authorized.", USER_NOT_AUTHORIZED), 403 return json_error("You are not authorized.", errors.USER_NOT_AUTHORIZED), 403
authorization = Authorization.query.filter_by(rid=rid, email=email).first() authorization = Authorization.query.filter_by(rid=rid, email=email).first()
if not authorization: if not authorization:
return json_error("Could not find the authorization", AUTHORIZATION_NOT_FOUND), 404 return json_error("Could not find the authorization", errors.AUTHORIZATION_NOT_FOUND), 404
if request.method == "DELETE": if request.method == "DELETE":
ext.session.delete(authorization) ext.session.delete(authorization)
ext.session.commit() ext.session.commit()

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 nest_backend.database import ext, User, Authorization, Repository
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()
@ -114,24 +114,24 @@ def page_repository_authorizations(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":
try: try:
return json_success([a.to_json() for a in repository.authorizations]) return json_success([a.to_json() for a in repository.authorizations])
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 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 request.json.get("email"): if not request.json.get("email"):
return json_error("Missing user email.", GENERIC_MISSING_FIELDS), 400 return json_error("Missing user email.", errors.GENERIC_MISSING_FIELDS), 400
target = User.query.filter_by(email=request.json.get('email')).first() target = User.query.filter_by(email=request.json.get('email')).first()
if not target: if not target:
return json_error("User could not be located", USER_NOT_FOUND), 400 return json_error("User could not be located", errors.USER_NOT_FOUND), 400
if target == user: if target == user:
return json_error("Owner cannot be a spectator", GENERIC_ALREADY_EXISTS), 406 return json_error("Owner cannot be a spectator", errors.GENERIC_ALREADY_EXISTS), 406
if request.method == "POST": if request.method == "POST":
authorization = Authorization(email=request.json.get('email'), rid=repository.id) authorization = Authorization(email=request.json.get('email'), rid=repository.id)
ext.session.add(authorization) ext.session.add(authorization)

View file

@ -4,7 +4,7 @@ from nest_backend.gestione import *
from flask_jwt_extended import create_access_token from flask_jwt_extended import create_access_token
from flask_cors import cross_origin from flask_cors import cross_origin
from datetime import timedelta, datetime from datetime import timedelta, datetime
from nest_backend.errors import * import nest_backend.errors as errors
@cross_origin() @cross_origin()
@ -43,4 +43,4 @@ def page_login():
access_token = create_access_token(identity=email, expires_delta=delta) access_token = create_access_token(identity=email, expires_delta=delta)
user = find_user(email) user = find_user(email)
return json_success({"access_token": access_token, 'user': user.to_json(), "expiration": expiration}), 201 return json_success({"access_token": access_token, 'user': user.to_json(), "expiration": expiration}), 201
return json_error("Bad username or password.", USER_WRONG_CREDENTIALS), 401 return json_error("Bad username or password.", errors.USER_WRONG_CREDENTIALS), 401

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()
@ -118,16 +118,16 @@ def page_user(email):
user = find_user(get_jwt_identity()) user = find_user(get_jwt_identity())
target = find_user(email) target = find_user(email)
if not target: if not target:
return json_error("Could not locate the user.", USER_NOT_FOUND), 404 return json_error("Could not locate the user.", errors.USER_NOT_FOUND), 404
if request.method == "GET": if request.method == "GET":
if not email == user.email and not user.isAdmin: if not email == user.email and not user.isAdmin:
return json_error("Thou art not authorized.", USER_NOT_AUTHORIZED), 403 return json_error("Thou art not authorized.", errors.USER_NOT_AUTHORIZED), 403
return json_success(target.to_json()) return json_success(target.to_json())
elif request.method == "DELETE": elif request.method == "DELETE":
if not user.isAdmin: if not user.isAdmin:
return json_error("User is not admin.", USER_NOT_ADMIN), 403 return json_error("User is not admin.", errors.USER_NOT_ADMIN), 403
if user == target: if user == target:
return json_error("The user cant delete himself. Its a sin.", USER_PREVENT_SEPPUKU), 406 return json_error("The user cant delete himself. Its a sin.", errors.USER_PREVENT_SEPPUKU), 406
repos = target.owner_of repos = target.owner_of
for repository in repos: for repository in repos:
repository.owner_id = user.email repository.owner_id = user.email
@ -140,11 +140,11 @@ def page_user(email):
ext.session.commit() ext.session.commit()
except Exception as e: except Exception as e:
ext.session.rollback() ext.session.rollback()
return json_error("Could not delete the user.", USER_DELETION_ERROR), 500 return json_error("Could not delete the user.", errors.USER_DELETION_ERROR), 500
return json_success(""), 204 # "The user has been deleted." return json_success(""), 204 # "The user has been deleted."
elif request.method == "PATCH": elif request.method == "PATCH":
if not email == user.email and not user.isAdmin: if not email == user.email and not user.isAdmin:
return json_error("Thou art not authorized.", USER_NOT_AUTHORIZED), 403 return json_error("Thou art not authorized.", errors.USER_NOT_AUTHORIZED), 403
target = find_user(email) target = find_user(email)
if request.json.get("username"): if request.json.get("username"):
target.username = request.json.get("username") target.username = request.json.get("username")

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()
@ -69,11 +69,11 @@ def page_users():
return json_success([user.to_json() for user in users]), 200 return json_success([user.to_json() for user in users]), 200
if request.method == "POST": if request.method == "POST":
if not user.isAdmin: if not user.isAdmin:
return json_error("User is not admin. Thou art not authorized.", USER_NOT_ADMIN), 403 return json_error("User is not admin. Thou art not authorized.", errors.USER_NOT_ADMIN), 403
if not request.json.get("email") or not request.json.get("password") or not request.json.get("username"): if not request.json.get("email") or not request.json.get("password") or not request.json.get("username"):
return json_error("Missing required fields.", GENERIC_MISSING_FIELDS), 400 return json_error("Missing required fields.", errors.GENERIC_MISSING_FIELDS), 400
if User.query.filter_by(email=request.json.get("email")).first(): if User.query.filter_by(email=request.json.get("email")).first():
return json_error("User already exists.", GENERIC_ALREADY_EXISTS), 406 return json_error("User already exists.", errors.GENERIC_ALREADY_EXISTS), 406
new_user = User(email=request.json.get("email"), password=gen_password(request.json.get("password")), new_user = User(email=request.json.get("email"), password=gen_password(request.json.get("password")),
username=request.json.get("username")) username=request.json.get("username"))
ext.session.add(new_user) ext.session.add(new_user)