From 7d28affb779cc76a258b326fa501562fbae9067e Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 9 Oct 2023 12:50:31 +0300 Subject: Fetch InbredSet group resource ID by SpeciesId and InbredSetId Get the resource used to control access to the InbredSet group by that group's SpeciesId and InbredSetId. --- .../authorisation/resources/inbredset/__init__.py | 1 + .../authorisation/resources/inbredset/views.py | 37 ++++++++++++++++++++++ gn_auth/auth/views.py | 2 ++ 3 files changed, 40 insertions(+) create mode 100644 gn_auth/auth/authorisation/resources/inbredset/__init__.py create mode 100644 gn_auth/auth/authorisation/resources/inbredset/views.py (limited to 'gn_auth') diff --git a/gn_auth/auth/authorisation/resources/inbredset/__init__.py b/gn_auth/auth/authorisation/resources/inbredset/__init__.py new file mode 100644 index 0000000..7ca14a0 --- /dev/null +++ b/gn_auth/auth/authorisation/resources/inbredset/__init__.py @@ -0,0 +1 @@ +"""InbredSet resources.""" diff --git a/gn_auth/auth/authorisation/resources/inbredset/views.py b/gn_auth/auth/authorisation/resources/inbredset/views.py new file mode 100644 index 0000000..c3669e6 --- /dev/null +++ b/gn_auth/auth/authorisation/resources/inbredset/views.py @@ -0,0 +1,37 @@ +"""Views for InbredSet resources.""" +from flask import jsonify, Response, Blueprint + +from gn_auth.auth.db import sqlite3 as db +from gn_auth.auth.db.sqlite3 import with_db_connection +from gn_auth.auth.authentication.oauth2.resource_server import require_oauth + +iset = Blueprint("inbredset", __name__) + +@iset.route("/resource-id//") +def resource_id_by_inbredset_id(speciesid: int, inbredsetid: int) -> Response: + """Retrieve the resource ID for resource attached to the inbredset.""" + def __res_by_iset_id__(conn): + with db.cursor(conn) as cursor: + cursor.execute( + "SELECT r.resource_id FROM linked_inbredset_groups AS lisg " + "INNER JOIN inbredset_group_resources AS isgr " + "ON lisg.data_link_id=isgr.data_link_id " + "INNER JOIN resources AS r ON isgr.resource_id=r.resource_id " + "WHERE lisg.SpeciesId=? AND lisg.InbredSetId=?", + (speciesid, inbredsetid)) + return cursor.fetchone() + + res = with_db_connection(__res_by_iset_id__) + if res: + resp = jsonify({"status": "success", "resource-id": res["resource_id"]}) + else: + resp = jsonify({ + "status": "not-found", + "error_description": ( + "Could not find resource handling InbredSet group with ID " + f"'{inbredsetid}' that belongs to Species with ID " + f"'{speciesid}'") + }) + resp.status_code = 404 + + return resp diff --git a/gn_auth/auth/views.py b/gn_auth/auth/views.py index 1060ea9..cf5c45f 100644 --- a/gn_auth/auth/views.py +++ b/gn_auth/auth/views.py @@ -10,6 +10,7 @@ from .authorisation.roles.views import roles from .authorisation.resources.views import resources from .authorisation.resources.groups.views import groups from .authorisation.resources.system.views import system +from .authorisation.resources.inbredset.views import iset oauth2 = Blueprint("oauth2", __name__) @@ -21,3 +22,4 @@ oauth2.register_blueprint(admin, url_prefix="/admin") oauth2.register_blueprint(groups, url_prefix="/group") oauth2.register_blueprint(system, url_prefix="/system") oauth2.register_blueprint(resources, url_prefix="/resource") +oauth2.register_blueprint(iset, url_prefix="/resource/inbredset") -- cgit v1.2.3