aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2023-02-22 16:22:47 +0300
committerFrederick Muriuki Muriithi2023-02-22 16:22:47 +0300
commitc876c9f53018529fed1cb025a085b6e9d37dc3ef (patch)
tree0a3afededbc1ac8ee4a499e9b93142f76949d25f
parent90f2c1ae6c7a3321e6bc3373edbf560a21748e84 (diff)
downloadgenenetwork3-c876c9f53018529fed1cb025a085b6e9d37dc3ef.tar.gz
auth: List data not linked to any group.
-rw-r--r--gn3/auth/authorisation/errors.py6
-rw-r--r--gn3/auth/authorisation/resources/data.py96
-rw-r--r--gn3/auth/authorisation/resources/views.py24
-rw-r--r--main.py2
4 files changed, 126 insertions, 2 deletions
diff --git a/gn3/auth/authorisation/errors.py b/gn3/auth/authorisation/errors.py
index 99ee55d..2116ead 100644
--- a/gn3/auth/authorisation/errors.py
+++ b/gn3/auth/authorisation/errors.py
@@ -15,6 +15,12 @@ class NotFoundError(AuthorisationError):
"""Raised whenever we try fetching (a/an) object(s) that do(es) not exist."""
error_code: int = 404
+class InvalidData(AuthorisationError):
+ """
+ Exception if user requests invalid data
+ """
+ error_code: int = 400
+
class InconsistencyError(AuthorisationError):
"""
Exception raised due to data inconsistencies
diff --git a/gn3/auth/authorisation/resources/data.py b/gn3/auth/authorisation/resources/data.py
new file mode 100644
index 0000000..7598f34
--- /dev/null
+++ b/gn3/auth/authorisation/resources/data.py
@@ -0,0 +1,96 @@
+"""Handles the resource objects' data."""
+from typing import Any, Sequence
+
+from MySQLdb.cursors import DictCursor
+
+from gn3 import db_utils as gn3db
+from gn3.auth import db as authdb
+from gn3.auth.authorisation.errors import InvalidData
+
+def __fetch_grouped_data__(
+ conn: authdb.DbConnection, dataset_type: str) -> Sequence[dict[str, Any]]:
+ """Retrieve ids for all data that are linked to groups in the auth db."""
+ with authdb.cursor(conn) as cursor:
+ cursor.execute(
+ "SELECT dataset_type, dataset_or_trait_id FROM linked_group_data "
+ "WHERE LOWER(dataset_type)=?",
+ (dataset_type,))
+ return tuple(dict(row) for row in cursor.fetchall())
+
+def __fetch_ungrouped_mrna_data__(
+ conn: gn3db.Connection, grouped_data,
+ offset: int = 0) -> Sequence[dict]:
+ """Fetch ungrouped mRNA Assay data."""
+ query = ("SELECT psf.Id, psf.Name, psf.FullName, "
+ "ifiles.GN_AccesionId AS accession_id FROM ProbeSetFreeze AS psf "
+ "INNER JOIN InfoFiles AS ifiles ON psf.Name=ifiles.InfoPageName")
+ params: tuple[Any, ...] = tuple()
+ if bool(grouped_data):
+ clause = ", ".join(["%s"] * len(grouped_data))
+ query = f"{query} WHERE psf.Id NOT IN ({clause})"
+ params = tuple(item["dataset_or_trait_id"] for item in grouped_data)
+
+ query = f"{query} LIMIT 100 OFFSET %s"
+ with conn.cursor(cursorclass=DictCursor) as cursor:# type: ignore[call-arg]
+ cursor.execute(query, (params + (offset,)))
+ return tuple(dict(row) for row in cursor.fetchall())
+
+def __fetch_ungrouped_geno_data__(
+ conn: gn3db.Connection, grouped_data,
+ offset: int = 0) -> Sequence[dict]:
+ """Fetch ungrouped Genotype data."""
+ query = ("SELECT gf.Id, gf.Name, gf.FullName, "
+ "ifiles.GN_AccesionId AS accession_id FROM GenoFreeze AS gf "
+ "INNER JOIN InfoFiles AS ifiles ON gf.Name=ifiles.InfoPageName")
+ params: tuple[Any, ...] = tuple()
+ if bool(grouped_data):
+ clause = ", ".join(["%s"] * len(grouped_data))
+ query = f"{query} WHERE gf.Id NOT IN ({clause})"
+ params = tuple(item["dataset_or_trait_id"] for item in grouped_data)
+
+ query = f"{query} LIMIT 100 OFFSET %s"
+ with conn.cursor(cursorclass=DictCursor) as cursor:# type: ignore[call-arg]
+ cursor.execute(query, (params + (offset,)))
+ return tuple(dict(row) for row in cursor.fetchall())
+
+def __fetch_ungrouped_pheno_data__(
+ conn: gn3db.Connection, grouped_data,
+ offset: int = 0) -> Sequence[dict]:
+ """Fetch ungrouped Phenotype data."""
+ query = ("SELECT pf.Id, pf.Name, pf.FullName, "
+ "ifiles.GN_AccesionId AS accession_id FROM PublishFreeze AS pf "
+ "INNER JOIN InfoFiles AS ifiles ON pf.Name=ifiles.InfoPageName")
+ params: tuple[Any, ...] = tuple()
+ if bool(grouped_data):
+ clause = ", ".join(["%s"] * len(grouped_data))
+ query = f"{query} WHERE pf.Id NOT IN ({clause})"
+ params = tuple(item["dataset_or_trait_id"] for item in grouped_data)
+
+ query = f"{query} LIMIT 100 OFFSET %s"
+ with conn.cursor(cursorclass=DictCursor) as cursor:# type: ignore[call-arg]
+ cursor.execute(query, (params + (offset,)))
+ return tuple(dict(row) for row in cursor.fetchall())
+
+def __fetch_ungrouped_data__(
+ conn: gn3db.Connection, dataset_type: str,
+ ungrouped: Sequence[dict[str, Any]]) -> Sequence[dict[str, Any]]:
+ """Fetch any ungrouped data."""
+ fetch_fns = {
+ "mrna": __fetch_ungrouped_mrna_data__,
+ "genotype": __fetch_ungrouped_geno_data__,
+ "phenotype": __fetch_ungrouped_pheno_data__
+ }
+ return fetch_fns[dataset_type](conn, ungrouped)
+
+def retrieve_ungrouped_data(
+ authconn: authdb.DbConnection,
+ gn3conn: gn3db.Connection,
+ dataset_type: str) -> Sequence[dict]:
+ """Retrieve any data not linked to any group."""
+ if dataset_type not in ("mrna", "genotype", "phenotype"):
+ raise InvalidData(
+ "Requested dataset type is invalid. Expected one of "
+ "'mrna', 'genotype' or 'phenotype'.")
+ grouped_data = __fetch_grouped_data__(authconn, dataset_type)
+ print(f"GROUPED DATA: {grouped_data}")
+ return __fetch_ungrouped_data__(gn3conn, dataset_type, grouped_data)
diff --git a/gn3/auth/authorisation/resources/views.py b/gn3/auth/authorisation/resources/views.py
index ad39df7..44e2c4e 100644
--- a/gn3/auth/authorisation/resources/views.py
+++ b/gn3/auth/authorisation/resources/views.py
@@ -1,11 +1,18 @@
"""The views/routes for the resources package"""
import uuid
+from functools import partial
+
from flask import request, jsonify, Response, Blueprint, current_app as app
+from gn3 import db_utils as gn3dbutils
+from gn3.auth.db_utils import with_db_connection
+
+from .data import retrieve_ungrouped_data
from .models import (
resource_by_id, resource_categories, resource_category_by_id,
create_resource as _create_resource)
+from ..errors import AuthorisationError
from ..groups.models import user_group, DUMMY_GROUP
from ... import db
@@ -52,7 +59,10 @@ def view_resource(resource_id: uuid.UUID) -> Response:
@resources.route("/<string:resource_type>/unlinked-data")
@require_oauth("profile group resource")
def unlinked_data(resource_type: str) -> Response:
- """View unlinked data"""
+ """View data linked to the group but not linked to any resource."""
+ if resource_type not in ("all", "mrna", "genotype", "phenotype"):
+ raise AuthorisationError(f"Invalid resource type {resource_type}")
+
with require_oauth.acquire("profile group resource") as the_token:
db_uri = app.config["AUTH_DB"]
with db.connection(db_uri) as conn, db.cursor(conn) as cursor:
@@ -97,3 +107,15 @@ def unlinked_data(resource_type: str) -> Response:
return jsonify(tuple(dict(row) for row in cursor.fetchall()))
return jsonify(tuple())
+
+@resources.route("/<string:dataset_type>/ungrouped-data", methods=["GET"])
+@require_oauth("profile group resource")
+def ungrouped_data(dataset_type: str) -> Response:
+ """View data not linked to any group."""
+ if dataset_type not in ("all", "mrna", "genotype", "phenotype"):
+ raise AuthorisationError(f"Invalid dataset type {dataset_type}")
+
+ with gn3dbutils.database_connection() as gn3conn:
+ return jsonify(with_db_connection(partial(
+ retrieve_ungrouped_data, gn3conn=gn3conn,
+ dataset_type=dataset_type)))
diff --git a/main.py b/main.py
index 4866e14..2a47dbb 100644
--- a/main.py
+++ b/main.py
@@ -32,7 +32,7 @@ def __init_dev_users__():
dev_users_passwd = "INSERT INTO user_credentials VALUES (:user_id, :hash)"
dev_users = ({
"user_id": "0ad1917c-57da-46dc-b79e-c81c91e5b928",
- "email": "test@develpment.user",
+ "email": "test@development.user",
"name": "Test Development User",
"password": "testpasswd"},)