aboutsummaryrefslogtreecommitdiff
path: root/gn_auth/auth/authorisation/resources/phenotype_resource.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2023-09-13 08:04:55 +0300
committerFrederick Muriuki Muriithi2023-09-26 03:44:29 +0300
commit4b36031859d1f2e0d4a255741b543afecfe3cecd (patch)
treefbe29b6dfd2bdf63f0dc6bac9542ca630b4c7312 /gn_auth/auth/authorisation/resources/phenotype_resource.py
parent9a3b628e2744b658ade8902c1cd33deccbd48b63 (diff)
downloadgn-auth-4b36031859d1f2e0d4a255741b543afecfe3cecd.tar.gz
Extract resource-type-specific code into separate modules
For easier maintenance, extract the code that relates to specific resource types/categories into separate modules, each dealing with a single resource type/category.
Diffstat (limited to 'gn_auth/auth/authorisation/resources/phenotype_resource.py')
-rw-r--r--gn_auth/auth/authorisation/resources/phenotype_resource.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/gn_auth/auth/authorisation/resources/phenotype_resource.py b/gn_auth/auth/authorisation/resources/phenotype_resource.py
new file mode 100644
index 0000000..cf33579
--- /dev/null
+++ b/gn_auth/auth/authorisation/resources/phenotype_resource.py
@@ -0,0 +1,69 @@
+"""Phenotype data resources functions and utilities."""
+import uuid
+from typing import Optional, Sequence
+
+import sqlite3
+
+import gn_auth.auth.db.sqlite3 as db
+
+from .base import Resource
+from .data import __attach_data__
+
+def resource_data(
+ cursor: db.DbCursor,
+ resource_id: uuid.UUID,
+ offset: int = 0,
+ limit: Optional[int] = None) -> Sequence[sqlite3.Row]:
+ """Fetch data linked to a Phenotype resource"""
+ cursor.execute(
+ ("SELECT * FROM phenotype_resources AS pr "
+ "INNER JOIN linked_phenotype_data AS lpd "
+ "ON pr.data_link_id=lpd.data_link_id "
+ "WHERE pr.resource_id=?") + (
+ f" LIMIT {limit} OFFSET {offset}" if bool(limit) else ""),
+ (str(resource_id),))
+ return cursor.fetchall()
+
+def link_data_to_resource(
+ conn: db.DbConnection,
+ resource: Resource,
+ data_link_id: uuid.UUID) -> dict:
+ """Link Phenotype data with a resource."""
+ with db.cursor(conn) as cursor:
+ params = {
+ "group_id": str(resource.group.group_id),
+ "resource_id": str(resource.resource_id),
+ "data_link_id": str(data_link_id)
+ }
+ cursor.execute(
+ "INSERT INTO phenotype_resources VALUES"
+ "(:group_id, :resource_id, :data_link_id)",
+ params)
+ return params
+
+def unlink_data_from_resource(
+ conn: db.DbConnection,
+ resource: Resource,
+ data_link_id: uuid.UUID) -> dict:
+ """Unlink data from Phenotype resources"""
+ with db.cursor(conn) as cursor:
+ cursor.execute("DELETE FROM phenotype_resources "
+ "WHERE resource_id=? AND data_link_id=?",
+ (str(resource.resource_id), str(data_link_id)))
+ return {
+ "resource_id": str(resource.resource_id),
+ "dataset_type": resource.resource_category.resource_category_key,
+ "data_link_id": str(data_link_id)
+ }
+
+def attach_resources_data(
+ cursor, resources: Sequence[Resource]) -> Sequence[Resource]:
+ """Attach linked data to Phenotype resources"""
+ placeholders = ", ".join(["?"] * len(resources))
+ cursor.execute(
+ "SELECT * FROM phenotype_resources AS pr "
+ "INNER JOIN linked_phenotype_data AS lpd "
+ "ON pr.data_link_id=lpd.data_link_id "
+ f"WHERE pr.resource_id IN ({placeholders})",
+ tuple(str(resource.resource_id) for resource in resources))
+ return __attach_data__(cursor.fetchall(), resources)