From 0c64b1f87739bd9298a182a7a7fca241b33d10cf Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Fri, 24 Feb 2023 12:37:01 +0300 Subject: oauth2: resources: Enable linking data to resources --- wqflask/wqflask/oauth2/data.py | 4 +-- wqflask/wqflask/oauth2/resources.py | 33 +++++++++++++++++++++- .../wqflask/templates/oauth2/view-resource.html | 13 +++++---- 3 files changed, 42 insertions(+), 8 deletions(-) (limited to 'wqflask') diff --git a/wqflask/wqflask/oauth2/data.py b/wqflask/wqflask/oauth2/data.py index b080d4df..cb9c1000 100644 --- a/wqflask/wqflask/oauth2/data.py +++ b/wqflask/wqflask/oauth2/data.py @@ -41,7 +41,7 @@ def list_data(): return __render__(**{**groups, **roles}) data_items = oauth2_get( - f"oauth2/resource/{dataset_type}/ungrouped-data?offset={offset}").either( + f"oauth2/group/{dataset_type}/ungrouped-data?offset={offset}").either( lambda err: {"data_items_error": process_error(err)}, lambda data: {"data_items": data}) return __render__(**{ @@ -69,7 +69,7 @@ def link_data(): "dataset_type": form["dataset_type"], "offset": form.get("offset", 0)} return oauth2_post( - "oauth2/resource/data/link", + "oauth2/group/data/link", data={ "dataset_type": form["dataset_type"], "dataset_id": form["dataset_id"], diff --git a/wqflask/wqflask/oauth2/resources.py b/wqflask/wqflask/oauth2/resources.py index 2a79f6cd..1551dc8a 100644 --- a/wqflask/wqflask/oauth2/resources.py +++ b/wqflask/wqflask/oauth2/resources.py @@ -56,7 +56,7 @@ def view_resource(resource_id: uuid.UUID): # Metadata edit maybe? def __resource_success__(resource): dataset_type = resource["resource_category"]["resource_category_key"] - return oauth2_get(f"oauth2/resource/{dataset_type}/unlinked-data").either( + return oauth2_get(f"oauth2/group/{dataset_type}/unlinked-data").either( lambda err: render_template( "oauth2/view-resource.html", resource=resource, unlinked_error=process_error(err)), @@ -69,6 +69,37 @@ def view_resource(resource_id: uuid.UUID): resource=None, error=process_error(err)), __resource_success__) +@resources.route("/data/link", methods=["POST"]) +@require_oauth2 +def link_data_to_resource(): + """Link group data to a resource""" + form = request.form + try: + assert "resource_id" in form, "Resource ID not provided." + assert "dataset_id" in form, "Dataset ID not provided." + assert "dataset_type" in form, "Dataset type not specified" + assert form["dataset_type"].lower() in ( + "mrna", "genotype", "phenotype"), "Invalid dataset type provided." + resource_id = form["resource_id"] + + def __error__(error): + err = process_error(error) + flash(f"{err['error']}: {err['error_description']}", "alert-danger") + return redirect(url_for( + "oauth2.resource.view_resource", resource_id=resource_id)) + + def __success__(success): + flash(f"Data linked to resource successfully", "alert-success") + return redirect(url_for( + "oauth2.resource.view_resource", resource_id=resource_id)) + return oauth2_post("oauth2/resource/data/link", data=dict(form)).either( + __error__, + __success__) + except AssertionError as aserr: + flash(aserr.args[0], "alert-danger") + return redirect(url_for( + "oauth2.resource.view_resource", resource_id=form["resource_id"])) + @resources.route("/edit/", methods=["GET"]) @require_oauth2 def edit_resource(resource_id: uuid.UUID): diff --git a/wqflask/wqflask/templates/oauth2/view-resource.html b/wqflask/wqflask/templates/oauth2/view-resource.html index 4ae276f8..04be2a4c 100644 --- a/wqflask/wqflask/templates/oauth2/view-resource.html +++ b/wqflask/wqflask/templates/oauth2/view-resource.html @@ -65,7 +65,7 @@
-
@@ -96,7 +96,7 @@ {%if unlinked_error is defined%} - display_error("Unlinked Data Error", unlinked_error) + {{display_error("Unlinked Data Error", unlinked_error)}} {%else%} {%for data_item in unlinked_data:%} @@ -107,13 +107,16 @@ -
+ - +
-- cgit v1.2.3