From acde4fc57250cf2ecc0bb12b34d523d8cf306c3e Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 10 Apr 2023 10:08:54 +0300 Subject: Enable search, filtering out selected, but not linked data When a user selects some datasets and does a new search, we filter out the selected datasets too, even though they are yet to be linked. --- gn3/auth/authorisation/data/genotypes.py | 16 +++++++++------- gn3/auth/authorisation/data/views.py | 23 +++++++++++++++++++---- 2 files changed, 28 insertions(+), 11 deletions(-) (limited to 'gn3/auth') diff --git a/gn3/auth/authorisation/data/genotypes.py b/gn3/auth/authorisation/data/genotypes.py index fe06726..8f901a5 100644 --- a/gn3/auth/authorisation/data/genotypes.py +++ b/gn3/auth/authorisation/data/genotypes.py @@ -21,14 +21,17 @@ def linked_genotype_data(conn: authdb.DbConnection) -> Iterable[dict]: "You do not have sufficient privileges to link data to (a) " "group(s)."), oauth2_scope="profile group resource") -def ungrouped_genotype_data( +def ungrouped_genotype_data(# pylint: disable=[too-many-arguments] authconn: authdb.DbConnection, gn3conn: gn3db.Connection, - search_query: str, limit: int = 10000, offset: int = 0) -> tuple[ + search_query: str, selected: tuple[dict, ...] = tuple(), + limit: int = 10000, offset: int = 0) -> tuple[ dict, ...]: """Retrieve genotype data that is not linked to any user group.""" params = tuple( (row["SpeciesId"], row["InbredSetId"], row["GenoFreezeId"]) - for row in linked_genotype_data(authconn)) + for row in linked_genotype_data(authconn)) + tuple( + (row["SpeciesId"], row["InbredSetId"], row["GenoFreezeId"]) + for row in selected) query = ( "SELECT s.SpeciesId, iset.InbredSetId, iset.InbredSetName, " "gf.Id AS GenoFreezeId, gf.Name AS dataset_name, " @@ -50,14 +53,13 @@ def ungrouped_genotype_data( if bool(search_query): query = query + ( - "CONCAT(gf.Name, ' ', gf.FullName, ' ', gf.ShortName) LIKE '%%?%%' ") - params = params + ((search_query,),)# type: ignore[operator] + "CONCAT(gf.Name, ' ', gf.FullName, ' ', gf.ShortName) LIKE %s ") + params = params + ((f"%{search_query}%",),)# type: ignore[operator] query = query + f"LIMIT {int(limit)} OFFSET {int(offset)}" - final_params = tuple(item for sublist in params for item in sublist) with gn3conn.cursor(DictCursor) as cursor: cursor.execute( - query, final_params) + query, tuple(item for sublist in params for item in sublist)) return tuple(row for row in cursor.fetchall()) @authorised_p( diff --git a/gn3/auth/authorisation/data/views.py b/gn3/auth/authorisation/data/views.py index 55a7373..9a924b4 100644 --- a/gn3/auth/authorisation/data/views.py +++ b/gn3/auth/authorisation/data/views.py @@ -313,13 +313,28 @@ def migrate_users_data() -> Response: def __search_mrna__(): pass +def __request_key__(key: str, default: Any = ""): + if bool(request.json): + return request.json.get(#type: ignore[union-attr] + key, request.args.get(key, request.form.get(key, default))) + return request.args.get(key, request.form.get(key, default)) + +def __request_key_list__(key: str, default: tuple[Any, ...] = tuple()): + if bool(request.json): + return (request.json.get(key,[])#type: ignore[union-attr] + or request.args.getlist(key) or request.form.getlist(key) + or list(default)) + return (request.args.getlist(key) + or request.form.getlist(key) or list(default)) + def __search_genotypes__(): - query = request.form.get("query", request.args.get("query", "")) - limit = int(request.form.get("limit", request.args.get("limit", 10000))) - offset = int(request.form.get("offset", request.args.get("offset", 0))) + query = __request_key__("query", "") + limit = int(__request_key__("limit", 10000)) + offset = int(__request_key__("offset", 0)) with gn3db.database_connection() as gn3conn: __ungrouped__ = partial( ungrouped_genotype_data, gn3conn=gn3conn, search_query=query, + selected=__request_key_list__("selected"), limit=limit, offset=offset) return jsonify(with_db_connection(__ungrouped__)) @@ -330,7 +345,7 @@ def __search_phenotypes__(): @require_oauth("profile group resource") def search_unlinked_data(): """Search for various unlinked data.""" - dataset_type = request.form["dataset_type"] + dataset_type = request.json["dataset_type"] search_fns = { "mrna": __search_mrna__, "genotype": __search_genotypes__, -- cgit v1.2.3