From 5c6a28deb9bd82bde9cf813babef027fd2bdc6ca Mon Sep 17 00:00:00 2001 From: gerwoud Date: Mon, 11 Mar 2024 19:44:08 +0100 Subject: [PATCH] fixed not checking for tuple type anymore --- backend/project/endpoints/projects/projects.py | 2 +- backend/project/utils/query_agent.py | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/backend/project/endpoints/projects/projects.py b/backend/project/endpoints/projects/projects.py index 5fde6a53..d6091a2e 100644 --- a/backend/project/endpoints/projects/projects.py +++ b/backend/project/endpoints/projects/projects.py @@ -60,7 +60,7 @@ def post(self): "course_id", "visible_for_students", "archieved"] - ) + )[0] project_upload_directory = os.path.join(f"{UPLOAD_FOLDER}", f"{new_project.project_id}") diff --git a/backend/project/utils/query_agent.py b/backend/project/utils/query_agent.py index 41cb9e86..2398d9be 100644 --- a/backend/project/utils/query_agent.py +++ b/backend/project/utils/query_agent.py @@ -72,7 +72,7 @@ def create_model_instance(model: DeclarativeMeta, db.session.add(new_instance) db.session.commit() - return new_instance + return new_instance, 201 def insert_into_model(model: DeclarativeMeta, @@ -95,18 +95,20 @@ def insert_into_model(model: DeclarativeMeta, """ try: new_instance = create_model_instance(model, data, response_url_base, required_fields) + model_instance = new_instance[0] + status_code = new_instance[1] # if its a tuple the model instance couldn't be created so it already # is the right format of error message and we just need to return - if isinstance(new_instance, tuple): - return new_instance + if status_code == 400: + return model_instance, status_code return (jsonify({ - "data": new_instance, + "data": model_instance, "message": "Object created succesfully.", "url": urljoin(response_url_base + "/", - str(getattr(new_instance, url_id_field)))}), - 201) + str(getattr(model_instance, url_id_field)))}), + status_code) except SQLAlchemyError: db.session.rollback() return jsonify({"error": "Something went wrong while inserting into the database.",