about summary refs log tree commit diff
path: root/gn3/auth/authorisation/resources/views.py
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2023-02-21 16:21:23 +0300
committerFrederick Muriuki Muriithi2023-02-21 16:24:30 +0300
commita5f83724d3075680e6d200086e3400ac938cc525 (patch)
tree4bc983fef56554708b333dcbbafe78ea7a6dcd1e /gn3/auth/authorisation/resources/views.py
parent08e8e36e256d1893967c98b366395279b39c1e72 (diff)
downloadgenenetwork3-a5f83724d3075680e6d200086e3400ac938cc525.tar.gz
auth: resources: Attach resource data and list unlinked data.
Load the data that is attached to a particular resource together with the
resource.

List any unlinked data: useful when linking data to resources.
Diffstat (limited to 'gn3/auth/authorisation/resources/views.py')
-rw-r--r--gn3/auth/authorisation/resources/views.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/gn3/auth/authorisation/resources/views.py b/gn3/auth/authorisation/resources/views.py
index b45a9fc..ad39df7 100644
--- a/gn3/auth/authorisation/resources/views.py
+++ b/gn3/auth/authorisation/resources/views.py
@@ -6,6 +6,8 @@ from .models import (
     resource_by_id, resource_categories, resource_category_by_id,
     create_resource as _create_resource)
 
+from ..groups.models import user_group, DUMMY_GROUP
+
 from ... import db
 from ...dictify import dictify
 from ...authentication.oauth2.resource_server import require_oauth
@@ -46,3 +48,52 @@ def view_resource(resource_id: uuid.UUID) -> Response:
         with db.connection(db_uri) as conn:
             return jsonify(dictify(resource_by_id(
                 conn, the_token.user, resource_id)))
+
+@resources.route("/<string:resource_type>/unlinked-data")
+@require_oauth("profile group resource")
+def unlinked_data(resource_type: str) -> Response:
+    """View unlinked data"""
+    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:
+            ugroup = user_group(cursor, the_token.user).maybe(# type: ignore[misc]
+                DUMMY_GROUP, lambda grp: grp)
+            if ugroup == DUMMY_GROUP:
+                return jsonify(tuple())
+            type_filter = {
+                "all": "",
+                "mrna": 'WHERE dataset_type="mRNA"',
+                "genotype": 'WHERE dataset_type="Genotype"',
+                "phenotype": 'WHERE dataset_type="Phenotype"'
+            }[resource_type]
+
+            except_filter = (
+                "SELECT group_id, dataset_type, "
+                "dataset_id AS dataset_or_trait_id FROM mrna_resources "
+                "UNION "
+                "SELECT group_id, dataset_type, "
+                "trait_id AS dataset_or_trait_id FROM genotype_resources "
+                "UNION "
+                "SELECT group_id, dataset_type, "
+                "trait_id AS dataset_or_trait_id FROM phenotype_resources")
+
+            ids_query = ("SELECT group_id, dataset_type, dataset_or_trait_id "
+                         "FROM linked_group_data "
+                         f"{type_filter} "
+                         f"EXCEPT {except_filter} ")
+            cursor.execute(ids_query)
+            ids = cursor.fetchall()
+
+            if ids:
+                clause = ", ".join(["(?, ?, ?)"] * len(ids))
+                data_query = (
+                    "SELECT * FROM linked_group_data "
+                    "WHERE (group_id, dataset_type, dataset_or_trait_id) "
+                    f"IN (VALUES {clause})")
+                params = tuple(item for sublist in
+                               ((row[0], row[1], row[2]) for row in ids)
+                               for item in sublist)
+                cursor.execute(data_query, params)
+                return jsonify(tuple(dict(row) for row in cursor.fetchall()))
+
+    return jsonify(tuple())