From a2c4047b8ff395d1cef852b8e51fa9bba9be8bd0 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Tue, 4 Apr 2023 15:55:49 +0300 Subject: oauth2: Provide UI for linking genotype datasets. --- wqflask/wqflask/oauth2/client.py | 5 +- wqflask/wqflask/oauth2/data.py | 68 ++++++--- .../templates/oauth2/data-list-genotype.html | 167 +++++++++++++++++++++ .../templates/oauth2/data-list-phenotype.html | 2 +- wqflask/wqflask/templates/oauth2/data-list.html | 2 +- 5 files changed, 221 insertions(+), 23 deletions(-) create mode 100644 wqflask/wqflask/templates/oauth2/data-list-genotype.html diff --git a/wqflask/wqflask/oauth2/client.py b/wqflask/wqflask/oauth2/client.py index 70cdfbe2..999bbfc8 100644 --- a/wqflask/wqflask/oauth2/client.py +++ b/wqflask/wqflask/oauth2/client.py @@ -15,14 +15,15 @@ def oauth2_client(): scope=SCOPE, token_endpoint_auth_method="client_secret_post", token=session.get("oauth2_token")) -def oauth2_get(uri_path: str) -> Either: +def oauth2_get(uri_path: str, data: dict = {}) -> Either: token = session.get("oauth2_token") config = app.config client = OAuth2Session( config["OAUTH2_CLIENT_ID"], config["OAUTH2_CLIENT_SECRET"], token=token, scope=SCOPE) resp = client.get( - urljoin(config["GN_SERVER_URL"], uri_path)) + urljoin(config["GN_SERVER_URL"], uri_path), + data=data) if resp.status_code == 200: return Right(resp.json()) diff --git a/wqflask/wqflask/oauth2/data.py b/wqflask/wqflask/oauth2/data.py index 963ab547..96acb2c2 100644 --- a/wqflask/wqflask/oauth2/data.py +++ b/wqflask/wqflask/oauth2/data.py @@ -18,6 +18,44 @@ def __render_template__(templatepath, **kwargs): return render_template( templatepath, **kwargs, user_privileges=user_privileges) +def __search_mrna__(query, template, **kwargs): + return __render_template__(template, **kwargs) + +def __search_genotypes__(query, template, **kwargs): + species_name = kwargs["species_name"] + datasets = oauth2_get( + "oauth2/data/search", + data = { + "query": query, + "dataset_type": "genotype", + "species_name": species_name + }).either( + lambda err: {"datasets_error": process_error(err)}, + lambda datasets: {"datasets": tuple({ + "index": idx, **dataset + } for idx,dataset in enumerate(datasets, start=1))}) + return __render_template__(template, **datasets, **kwargs) + +def __search_phenotypes__(query, template, **kwargs): + per_page = int(request.args.get("per_page", 500)) + species_name = kwargs["species_name"] + search_uri = (f"search/?type=phenotype&per_page={per_page}&query=" + f"species:{species_name}") + ( + f" AND ({query})" if bool(query) else "") + traits = oauth2_get(search_uri).either( + lambda err: {"traits_error": process_error(err)}, + lambda trts: {"traits": tuple({ + "index": idx, **trait + } for idx, trait in enumerate(trts, start=1))}) + + selected_traits = request.form.getlist("selected_traits") + + return __render_template__( + template, **traits, per_page=per_page, query=query, + selected_traits=selected_traits, + search_endpoint=urljoin(app.config["GN_SERVER_URL"], "search/"), + **kwargs) + @data.route("///list", methods=["GET", "POST"]) def list_data_by_species_and_dataset( @@ -25,31 +63,23 @@ def list_data_by_species_and_dataset( templates = { "mrna": "oauth2/data-list-mrna.html", "genotype": "oauth2/data-list-genotype.html", - "phenotype": "oauth2/data-list-phenotype.html"} + "phenotype": "oauth2/data-list-phenotype.html" + } + search_fns = { + "mrna": __search_mrna__, + "genotype": __search_genotypes__, + "phenotype": __search_phenotypes__ + } roles = oauth2_get("oauth2/user/roles").either( lambda err: {"roles_error": process_error(err)}, lambda roles: {"roles": roles}) - query = request.args.get("query", "") - per_page = int(request.args.get("per_page", 500)) - search_uri = (f"search/?type={dataset_type}&per_page={per_page}&query=" - f"species:{species_name}") + ( - f" AND ({query})" if bool(query) else "") - traits = oauth2_get(search_uri).either( - lambda err: {"traits_error": process_error(err)}, - lambda trts: {"traits": tuple({ - "index": idx, **trait - } for idx, trait in enumerate(trts, start=1))}) groups = oauth2_get("oauth2/group/list").either( lambda err: {"groups_error": process_error(err)}, lambda grps: {"groups": grps}) - - selected_traits = request.form.getlist("selected_traits") - - return __render_template__( - templates[dataset_type], **roles, **traits, **groups, - species_name=species_name, dataset_type=dataset_type, per_page=per_page, - query=query, selected_traits=selected_traits, - search_endpoint=urljoin(app.config["GN_SERVER_URL"], "search/")) + query = request.args.get("query", "") + return search_fns[dataset_type]( + query, templates[dataset_type], **roles, **groups, + species_name=species_name, dataset_type=dataset_type) @data.route("/list", methods=["GET", "POST"]) def list_data(): diff --git a/wqflask/wqflask/templates/oauth2/data-list-genotype.html b/wqflask/wqflask/templates/oauth2/data-list-genotype.html new file mode 100644 index 00000000..6dbcbd38 --- /dev/null +++ b/wqflask/wqflask/templates/oauth2/data-list-genotype.html @@ -0,0 +1,167 @@ +{%extends "base.html"%} +{%from "oauth2/profile_nav.html" import profile_nav%} +{%from "oauth2/display_error.html" import display_error%} + +{%block title%}Link Data: Genotype{%endblock%} + +{%block css%} + + + +{%endblock%} + +{%block content%} +
+ {{profile_nav("data", user_privileges)}} + + {{flash_me()}} + +
+ +
+ +
+ +
+ +
+ +
+ Search: Genotype + + + + +
+ + +
+
+
+ +
+ + + + + +
+
Loading...
+
+
+ + +
+{%endblock%} + +{%block js%} + + + + + + + + + + + +{%endblock%} diff --git a/wqflask/wqflask/templates/oauth2/data-list-phenotype.html b/wqflask/wqflask/templates/oauth2/data-list-phenotype.html index 0e94bcfa..53c6ce8c 100644 --- a/wqflask/wqflask/templates/oauth2/data-list-phenotype.html +++ b/wqflask/wqflask/templates/oauth2/data-list-phenotype.html @@ -2,7 +2,7 @@ {%from "oauth2/profile_nav.html" import profile_nav%} {%from "oauth2/display_error.html" import display_error%} -{%block title%}View User{%endblock%} +{%block title%}Link Data: Phenotype{%endblock%} {%block css%} {{profile_nav("data", user_privileges)}} -- cgit v1.2.3