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:53:31 +02:00
parent 4411f0310c
commit 02b22140eb
3 changed files with 16 additions and 16 deletions

View file

@ -4,7 +4,7 @@ from flask_jwt_extended import jwt_required, get_jwt_identity
from nest_backend.gestione import *
import datetime
from flask_cors import cross_origin
from nest_backend.errors import *
import nest_backend.errors as errors
from nest_crawler.repo_search import search_repo_conditions
import threading
@ -85,12 +85,12 @@ def page_repositories():
# 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(
request.json.get("evaluation_mode")):
return json_error("Missing arguments.", GENERIC_MISSING_FIELDS), 400
return json_error("Missing arguments.", errors.GENERIC_MISSING_FIELDS), 400
name = request.json.get("name")
try:
evaluation_mode = ConditionMode(request.json['evaluation_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)), 400
repository = Repository(name=name, owner_id=user.email, is_active=False, evaluation_mode=evaluation_mode)
@ -108,7 +108,7 @@ def page_repositories():
try:
type_ = ConditionType(c['type'])
except KeyError:
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400
return json_error("Unknown `type` specified.", errors.GENERIC_ENUM_INVALID), 400
ext.session.add(Condition(type=type_, content=c['content'], repository_id=repository.id))
ext.session.commit()
repository.is_active = True

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()
@ -156,11 +156,11 @@ def page_repository(rid):
user = find_user(get_jwt_identity())
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
if request.method == "GET":
return json_success(repository.to_json()), 200
if user.email != repository.owner_id:
return json_error("You are not the owner of this repository.", REPOSITORY_NOT_OWNER), 403
return json_error("You are not the owner of this repository.", errors.REPOSITORY_NOT_OWNER), 403
elif request.method == "PATCH":
if 'name' in request.json:
repository.name = request.json['name']
@ -173,7 +173,7 @@ def page_repository(rid):
try:
evaluation_mode = ConditionMode(request.json['evaluation_mode'])
except KeyError:
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400
return json_error("Unknown `type` specified.", errors.GENERIC_ENUM_INVALID), 400
repository.evaluation_mode = evaluation_mode
ext.session.commit()
return json_success(repository.to_json()), 204
@ -183,16 +183,16 @@ def page_repository(rid):
ext.session.commit()
except Exception as e:
ext.session.rollback()
return json_error("Cant delete repository because of dependencies.", REPOSITORY_DEPENDENCY_FAILURE), 500
return json_error("Cant delete repository because of dependencies.", errors.REPOSITORY_DEPENDENCY_FAILURE), 500
return json_success("Success"), 204
elif request.method == "PUT":
if not json_request_authorizer(request.json, repository):
return json_error("Missing one or more parameters in repository json.", GENERIC_MISSING_FIELDS), 400
return json_error("Missing one or more parameters in repository json.", errors.GENERIC_MISSING_FIELDS), 400
# Users will be tolerated if they change parameters they're not supposed to touch. We'll ignore them for now.
try:
evaluation_mode = ConditionMode(request.json['evaluation_mode'])
except KeyError:
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400
return json_error("Unknown `type` specified.", errors.GENERIC_ENUM_INVALID), 400
repository.evaluation_mode = evaluation_mode
repository.name = request.json['name']
repository.is_active = request.json['is_active']
@ -210,14 +210,14 @@ def page_repository(rid):
ext.session.delete(c)
ext.session.commit()
except Exception as e:
return json_error("Could not delete conditions.", GENERIC_UFO), 500
return json_error("Could not delete conditions.", errors.GENERIC_UFO), 500
# Create brand new conditions
for c in request.json['conditions']:
if not c['id']:
try:
type_ = ConditionType(c['type'])
except KeyError:
return json_error("Unknown `type` specified.", GENERIC_ENUM_INVALID), 400
return json_error("Unknown `type` specified.", errors.GENERIC_ENUM_INVALID), 400
content = c['content']
if type_ == ConditionType.hashtag:
content = hashtag_validator(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()
@ -45,11 +45,11 @@ def page_repository_tweets(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 and user.email not in [a.email for a in repository.authorizations]:
return json_error("You are not authorized.", USER_NOT_AUTHORIZED), 403
return json_error("You are not authorized.", errors.USER_NOT_AUTHORIZED), 403
if request.method == "GET":
return json_success([t.tweet.to_json() for t in repository.tweets])