From 351a8ba0ab8da015e9b8120ffe332e4991ae7403 Mon Sep 17 00:00:00 2001 From: Lorenzo Balugani Date: Tue, 11 May 2021 19:08:14 +0200 Subject: [PATCH] Updated how POST works, changed http response codes --- .../routes/repository/alerts/alert.py | 9 ++++--- .../repository/alerts/repository_alerts.py | 4 +-- .../routes/repository/conditions/condition.py | 8 +++--- .../conditions/repository_conditions.py | 4 +-- .../routes/repository/repositories.py | 27 ++++++++++++++----- .../routes/repository/repository.py | 8 +++--- .../backend/nest_backend/routes/users/user.py | 8 +++--- .../nest_backend/routes/users/users.py | 6 ++--- 8 files changed, 45 insertions(+), 29 deletions(-) diff --git a/code/backend/nest_backend/routes/repository/alerts/alert.py b/code/backend/nest_backend/routes/repository/alerts/alert.py index dea557f..de55f54 100644 --- a/code/backend/nest_backend/routes/repository/alerts/alert.py +++ b/code/backend/nest_backend/routes/repository/alerts/alert.py @@ -50,7 +50,7 @@ def page_alert(aid): security: - jwt: [] responses: - '200': + '204': description: The repository has been deleted successfully. '404': description: Could not find the requested repository. @@ -88,7 +88,7 @@ def page_alert(aid): schema: AlertParameterSchema responses: - '200': + '204': description: The alert has been updated successfully. content: application/json: @@ -131,14 +131,14 @@ def page_alert(aid): if 'window_size' in request.json: alert.window_size = request.json['window_size'] ext.session.commit() - return json_success(alert.to_json()), 200 + return json_success(alert.to_json()), 204 elif request.method == "DELETE": try: ext.session.delete(alert) ext.session.commit() except Exception: return json_error("Something went wrong while deleting alert."), 500 - return json_success("Deletion completed."), 200 + return json_success("Deletion completed."), 204 elif request.method == "PUT": if not json_request_authorizer(request.json, alert): return json_error("Missing one or more parameters in repository json."), 400 @@ -185,6 +185,7 @@ def page_alert(aid): return json_error("One of the provided IDs is incorrect"), 404 except KeyError: return json_error("Unknown field specified."), 400 + return json_success(alert.to_json()), 200 def create_node(node, ext, json, id): diff --git a/code/backend/nest_backend/routes/repository/alerts/repository_alerts.py b/code/backend/nest_backend/routes/repository/alerts/repository_alerts.py index bff0a20..ebee173 100644 --- a/code/backend/nest_backend/routes/repository/alerts/repository_alerts.py +++ b/code/backend/nest_backend/routes/repository/alerts/repository_alerts.py @@ -48,7 +48,7 @@ def page_repository_alerts(rid): application/json: schema: CreateAlert responses: - '200': + '201': description: The alert has been created successfully. content: application/json: @@ -89,4 +89,4 @@ def page_repository_alerts(rid): ext.session.add(alert) ext.session.commit() - return json_success(alert.to_json()), 200 + return json_success(alert.to_json()), 201 diff --git a/code/backend/nest_backend/routes/repository/conditions/condition.py b/code/backend/nest_backend/routes/repository/conditions/condition.py index 3754abb..b04ccad 100644 --- a/code/backend/nest_backend/routes/repository/conditions/condition.py +++ b/code/backend/nest_backend/routes/repository/conditions/condition.py @@ -48,7 +48,7 @@ def page_condition(cid): security: - jwt: [] responses: - '200': + '204': description: The deletion was successful. '401': description: The user is not logged in. @@ -80,7 +80,7 @@ def page_condition(cid): application/json: schema: CreateCondition responses: - '200': + '204': description: The user is not logged in. content: application/json: @@ -124,8 +124,8 @@ def page_condition(cid): if content := request.json.get("content"): condition.content = content ext.session.commit() - return json_success(condition.to_json()), 200 + return json_success(condition.to_json()), 204 if request.method == "DELETE": ext.session.delete(condition) ext.session.commit() - return json_success("Deleted."), 200 + return json_success("Deleted."), 204 diff --git a/code/backend/nest_backend/routes/repository/conditions/repository_conditions.py b/code/backend/nest_backend/routes/repository/conditions/repository_conditions.py index 5ed9e03..298fab2 100644 --- a/code/backend/nest_backend/routes/repository/conditions/repository_conditions.py +++ b/code/backend/nest_backend/routes/repository/conditions/repository_conditions.py @@ -49,7 +49,7 @@ def page_repository_conditions(rid): application/json: schema: CreateCondition responses: - '200': + '201': description: The user has been created successfully. content: application/json: @@ -95,4 +95,4 @@ def page_repository_conditions(rid): extension_sqlalchemy.session.add(condition) extension_sqlalchemy.session.commit() - return json_success(condition.to_json()), 200 + return json_success(condition.to_json()), 201 diff --git a/code/backend/nest_backend/routes/repository/repositories.py b/code/backend/nest_backend/routes/repository/repositories.py index 630c1bf..1596ba6 100644 --- a/code/backend/nest_backend/routes/repository/repositories.py +++ b/code/backend/nest_backend/routes/repository/repositories.py @@ -38,9 +38,9 @@ def page_repositories(): required: true content: application/json: - schema: CreateRepository + schema: Repository responses: - '200': + '201': description: The user has been created successfully. content: application/json: @@ -73,10 +73,25 @@ def page_repositories(): return json_success({"owner": [r.to_json() for r in owner], "spectator": [r.repository.to_json() for r in spectator]}) 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 request.json.get("evaluation_mode"): + return json_error("Missing arguments."), 400 name = request.json.get("name") - if not name: - return json_error("Missing one or more parameters"), 400 - repository = Repository(name=name, owner_id=user.email) + try: + evaluation_mode = ConditionMode(request.json['evaluation_mode']) + except KeyError: + return json_error("Unknown `type` specified."), 400 + repository = Repository(name=name, owner_id=user.email, is_active=False, evaluation_mode=evaluation_mode) ext.session.add(repository) ext.session.commit() - return json_success(repository.to_json()), 200 + ids = [c['id'] for c in request.json['conditions'] if c['id']] + # 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."), 400 + ext.session.add(Condition(type=type_, content=c['content'], repository_id=repository.id)) + ext.session.commit() + return json_success(repository.to_json()), 201 diff --git a/code/backend/nest_backend/routes/repository/repository.py b/code/backend/nest_backend/routes/repository/repository.py index e21c7e1..5a97927 100644 --- a/code/backend/nest_backend/routes/repository/repository.py +++ b/code/backend/nest_backend/routes/repository/repository.py @@ -50,7 +50,7 @@ def page_repository(rid): security: - jwt: [] responses: - '200': + '204': description: The repository has been deleted successfully. '404': description: Could not find the requested repository. @@ -88,7 +88,7 @@ def page_repository(rid): schema: IntegerParameterSchema responses: - '200': + '204': description: The repository has been updated successfully. content: application/json: @@ -175,7 +175,7 @@ def page_repository(rid): return json_error("Unknown `type` specified."), 400 repository.evaluation_mode = evaluation_mode ext.session.commit() - return json_success(repository.to_json()), 200 + return json_success(repository.to_json()), 204 elif request.method == "DELETE": if repository.owner_id != user.email and not user.isAdmin: return json_error("You are not the owner of this repository."), 403 @@ -185,7 +185,7 @@ def page_repository(rid): except Exception as e: ext.session.rollback() return json_error("Cant delete repository because of dependencies."), 500 - return json_success("Success"), 200 + 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."), 400 diff --git a/code/backend/nest_backend/routes/users/user.py b/code/backend/nest_backend/routes/users/user.py index 4cbb6da..e8c684d 100644 --- a/code/backend/nest_backend/routes/users/user.py +++ b/code/backend/nest_backend/routes/users/user.py @@ -48,7 +48,7 @@ def page_user(email): security: - jwt: [] responses: - '200': + '204': description: The user has been deleted successfully. '404': description: Could not find the requested user. @@ -86,7 +86,7 @@ def page_user(email): security: - jwt: [] responses: - '200': + '204': description: The user has been updated successfully. content: application/json: @@ -133,7 +133,7 @@ def page_user(email): except Exception: ext.session.rollback() return json_error("Could not delete the user."), 500 - return json_success("The user has been deleted."), 200 + return json_success("The user has been deleted."), 204 elif request.method == "PATCH": if not email == user.email and not user.isAdmin: return json_error("Thou art not authorized."), 403 @@ -143,4 +143,4 @@ def page_user(email): if request.json.get("password"): target.password = gen_password(request.json.get("password")) ext.session.commit() - return json_success(target.to_json()), 200 + return json_success(target.to_json()), 204 diff --git a/code/backend/nest_backend/routes/users/users.py b/code/backend/nest_backend/routes/users/users.py index e8764c4..04a83ad 100644 --- a/code/backend/nest_backend/routes/users/users.py +++ b/code/backend/nest_backend/routes/users/users.py @@ -39,7 +39,7 @@ def page_users(): application/json: schema: CreateUser responses: - '200': + '201': description: The user has been created successfully. content: application/json: @@ -62,7 +62,7 @@ def page_users(): if not user.isAdmin: return json_error("User is not admin. Thou art not authorized"), 403 users = User.query.all() - return json_success([user.to_json() for user in users]) + return json_success([user.to_json() for user in users]), 200 if request.method == "POST": if not user.isAdmin: return json_error("User is not admin. Thou art not authorized."), 403 @@ -70,4 +70,4 @@ def page_users(): username=request.json.get("username")) ext.session.add(new_user) ext.session.commit() - return json_success(new_user.to_json()) + return json_success(new_user.to_json()), 201