From 1ff4d8ca6b8bedda7e9e5a304142aa43bf869d94 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Thu, 26 Sep 2024 11:16:08 -0500 Subject: Bugfix: Fix bugs that were preventing creation of geno resource Fix the bugs that were causing the creation of a genotype resource via API to fail. --- .../authorisation/resources/genotypes/models.py | 7 +++- .../authorisation/resources/genotypes/views.py | 45 ++++++++++++++-------- .../auth/authorisation/resources/request_utils.py | 2 +- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/gn_auth/auth/authorisation/resources/genotypes/models.py b/gn_auth/auth/authorisation/resources/genotypes/models.py index c64922e..e8dca9b 100644 --- a/gn_auth/auth/authorisation/resources/genotypes/models.py +++ b/gn_auth/auth/authorisation/resources/genotypes/models.py @@ -68,9 +68,10 @@ def attach_resources_data( return __attach_data__(cursor.fetchall(), resources) -def insert_and_link_data_to_resource( +def insert_and_link_data_to_resource(# pylint: disable=[too-many-arguments] cursor, resource_id: uuid.UUID, + group_id: uuid.UUID, species_id: int, population_id: int, dataset_id: int, @@ -81,6 +82,7 @@ def insert_and_link_data_to_resource( """Link the genotype identifier data to the genotype resource.""" params = { "resource_id": str(resource_id), + "group_id": str(group_id), "data_link_id": str(uuid.uuid4()), "species_id": species_id, "population_id": population_id, @@ -93,12 +95,13 @@ def insert_and_link_data_to_resource( "INSERT INTO linked_genotype_data " "VALUES (" ":data_link_id," + ":group_id," ":species_id," ":population_id," ":dataset_id," ":dataset_name," ":dataset_fullname," - ":dataset_shortname," + ":dataset_shortname" ")", params) cursor.execute( diff --git a/gn_auth/auth/authorisation/resources/genotypes/views.py b/gn_auth/auth/authorisation/resources/genotypes/views.py index 4908ff3..2beed58 100644 --- a/gn_auth/auth/authorisation/resources/genotypes/views.py +++ b/gn_auth/auth/authorisation/resources/genotypes/views.py @@ -1,10 +1,18 @@ """Genotype-resources-specific views.""" -from flask import jsonify, Blueprint +import uuid + from pymonad.either import Left, Right +from flask import jsonify, Blueprint, current_app as app from gn_auth.auth.db import sqlite3 as db +from gn_auth.auth.requests import request_json + +from gn_auth.auth.authorisation.resources.base import ResourceCategory from gn_auth.auth.authorisation.resources.request_utils import check_form +from gn_auth.auth.authorisation.resources.groups.models import user_group + from gn_auth.auth.authentication.oauth2.resource_server import require_oauth + from gn_auth.auth.authorisation.resources.models import create_resource from gn_auth.auth.authorisation.resources.common import ( assign_resource_owner_role) @@ -14,16 +22,19 @@ from .models import insert_and_link_data_to_resource genobp = Blueprint("genotypes", __name__) -@genobp.route("/create", methods=["POST"]) +@genobp.route("genotypes/create", methods=["POST"]) @require_oauth("profile group resource") def create_geno_resource(): """Create a new genotype resource.""" with (require_oauth.acquire("profile group resource") as _token, db.connection(app.config["AUTH_DB"]) as conn, db.cursor(conn) as cursor): + cursor.execute("SELECT * FROM resource_categories " + "WHERE resource_category_key='genotype'") + row = cursor.fetchone() return check_form( - request.form, + request_json(), "species_id", "population_id", "dataset_id", @@ -31,33 +42,37 @@ def create_geno_resource(): "dataset_fullname", "dataset_shortname" ).then( - lambda form: user_group(conn, _token.user).either( - lambda err: Left(err), + lambda form: user_group(conn, _token.user).maybe( + Left("No user group found!"), lambda group: Right({"formdata": form, "group": group})) ).then( lambda fdgrp: { **fdgrp, "resource": create_resource( cursor, - f"Geno — {fdgrp['formdata']['dataset_fullname']}", + f"Genotype — {fdgrp['formdata']['dataset_fullname']}", + ResourceCategory(uuid.UUID(row["resource_category_id"]), + row["resource_category_key"], + row["resource_category_description"]), _token.user, fdgrp["group"], - fdgrp["formdata"].get("public", "on") // "on")} + fdgrp["formdata"].get("public", "on") == "on")} ).then( lambda fdgrpres: { **fdgrpres, "owner_role": assign_resource_owner_role( cursor, - fdgrpres["resource"], - _token.user)} + fdgrpres["resource"].resource_id, + _token.user.user_id)} ).then( lambda fdgrpres: insert_and_link_data_to_resource( cursor, fdgrpres["resource"].resource_id, - fdgrpres["resource"]["species_id"], - fdgrpres["resource"]["population_id"], - fdgrpres["resource"]["dataset_id"], - fdgrpres["resource"]["dataset_name"], - fdgrpres["resource"]["dataset_fullname"], - fdgrpres["resource"]["dataset_shortname"]) + fdgrpres["group"].group_id, + fdgrpres["formdata"]["species_id"], + fdgrpres["formdata"]["population_id"], + fdgrpres["formdata"]["dataset_id"], + fdgrpres["formdata"]["dataset_name"], + fdgrpres["formdata"]["dataset_fullname"], + fdgrpres["formdata"]["dataset_shortname"]) ).either(lambda error: (jsonify(error), 400), jsonify) diff --git a/gn_auth/auth/authorisation/resources/request_utils.py b/gn_auth/auth/authorisation/resources/request_utils.py index 03d3c3b..5fe6a29 100644 --- a/gn_auth/auth/authorisation/resources/request_utils.py +++ b/gn_auth/auth/authorisation/resources/request_utils.py @@ -7,7 +7,7 @@ def check_form(form, *fields) -> Either: """Check form for errors""" def __check_field__(errors, field): if not bool(form.get(field)): - return errors + (f"Missing `{field}` value.") + return errors + (f"Missing `{field}` value.",) return errors errors = reduce(__check_field__, fields, tuple()) -- cgit v1.2.3