From 86a1da54a1157f17fd5d6b62fc5c42c0ed6f342c Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Thu, 20 Apr 2023 09:53:19 +0300 Subject: oauth2: Render UI uniformly across all templates. --- wqflask/wqflask/oauth2/data.py | 19 +++++----------- wqflask/wqflask/oauth2/groups.py | 26 +++++++++++----------- wqflask/wqflask/oauth2/resources.py | 24 ++++++++++---------- wqflask/wqflask/oauth2/roles.py | 15 +++++++------ wqflask/wqflask/oauth2/users.py | 14 +++++++----- .../wqflask/templates/oauth2/create-resource.html | 2 +- wqflask/wqflask/templates/oauth2/create-role.html | 2 +- .../templates/oauth2/data-list-genotype.html | 2 +- .../wqflask/templates/oauth2/data-list-mrna.html | 2 +- .../templates/oauth2/data-list-phenotype.html | 2 +- wqflask/wqflask/templates/oauth2/data-list.html | 2 +- wqflask/wqflask/templates/oauth2/group.html | 2 +- .../templates/oauth2/group_join_or_create.html | 2 ++ .../wqflask/templates/oauth2/join-requests.html | 2 +- wqflask/wqflask/templates/oauth2/list_roles.html | 2 +- wqflask/wqflask/templates/oauth2/profile_nav.html | 10 ++++++++- .../wqflask/templates/oauth2/request_error.html | 2 +- wqflask/wqflask/templates/oauth2/resources.html | 2 +- wqflask/wqflask/templates/oauth2/role.html | 2 +- .../wqflask/templates/oauth2/view-group-role.html | 2 +- .../wqflask/templates/oauth2/view-resource.html | 2 +- wqflask/wqflask/templates/oauth2/view-user.html | 2 +- 22 files changed, 73 insertions(+), 67 deletions(-) diff --git a/wqflask/wqflask/oauth2/data.py b/wqflask/wqflask/oauth2/data.py index 68a98d98..a9d389dc 100644 --- a/wqflask/wqflask/oauth2/data.py +++ b/wqflask/wqflask/oauth2/data.py @@ -8,22 +8,15 @@ from urllib.parse import urljoin from redis import Redis from flask import ( flash, request, jsonify, url_for, redirect, Response, Blueprint, - render_template, current_app as app) + current_app as app) from jobs import jobs +from .ui import render_ui from .request_utils import process_error from .client import oauth2_get, oauth2_post data = Blueprint("data", __name__) -def __render_template__(templatepath, **kwargs): - roles = kwargs.get("roles", tuple()) - user_privileges = tuple( - privilege["privilege_id"] for role in roles - for privilege in role["privileges"]) - return render_template( - templatepath, **kwargs, user_privileges=user_privileges) - def __search_mrna__(query, template, **kwargs): species_name = kwargs["species_name"] search_uri = urljoin(app.config["GN_SERVER_URL"], "oauth2/data/search") @@ -37,7 +30,7 @@ def __search_mrna__(query, template, **kwargs): }).either( lambda err: {"datasets_error": process_error(err)}, lambda datasets: {"datasets": datasets}) - return __render_template__(template, search_uri=search_uri, **datasets, **kwargs) + return render_ui(template, search_uri=search_uri, **datasets, **kwargs) def __selected_datasets__(): if bool(request.json): @@ -61,7 +54,7 @@ def __search_genotypes__(query, template, **kwargs): }).either( lambda err: {"datasets_error": process_error(err)}, lambda datasets: {"datasets": datasets}) - return __render_template__(template, search_uri=search_uri, **datasets, **kwargs) + return render_ui(template, search_uri=search_uri, **datasets, **kwargs) def __search_phenotypes__(query, template, **kwargs): page = int(request.args.get("page", 1)) @@ -71,7 +64,7 @@ def __search_phenotypes__(query, template, **kwargs): raise Exception(error) def __search_success__(search_results): job_id = uuid.UUID(search_results["job_id"]) - return __render_template__( + return render_ui( template, traits=[], per_page=per_page, query=query, selected_traits=selected_traits, search_results=search_results, search_endpoint=urljoin( @@ -178,7 +171,7 @@ def list_data(): user_privileges = tuple( privilege["privilege_id"] for role in roles for privilege in role["privileges"]) - return render_template( + return render_ui( "oauth2/data-list.html", groups=kwargs.get("groups", []), data_items=kwargs.get("data_items", []), diff --git a/wqflask/wqflask/oauth2/groups.py b/wqflask/wqflask/oauth2/groups.py index 3bc5fb6d..76731793 100644 --- a/wqflask/wqflask/oauth2/groups.py +++ b/wqflask/wqflask/oauth2/groups.py @@ -3,9 +3,9 @@ import datetime from functools import partial from flask import ( - flash, session, request, url_for, redirect, Response, Blueprint, - render_template) + flash, session, request, url_for, redirect, Response, Blueprint) +from .ui import render_ui from .checks import require_oauth2 from .client import oauth2_get, oauth2_post from .request_utils import ( @@ -19,21 +19,21 @@ def user_group(): """Get the user's group.""" def __get_join_requests__(group, users): return oauth2_get("oauth2/group/requests/join/list").either( - lambda error: render_template( + lambda error: render_ui( "oauth2/group.html", group=group, users=users, group_join_requests_error=process_error(error)), - lambda gjr: render_template( + lambda gjr: render_ui( "oauth2/group.html", group=group, users=users, group_join_requests=gjr)) def __success__(group): return oauth2_get(f"oauth2/group/members/{group['group_id']}").either( - lambda error: render_template( + lambda error: render_ui( "oauth2/group.html", group=group, user_error=process_error(error)), partial(__get_join_requests__, group)) def __group_error__(err): - return render_template( + return render_ui( "oauth2/group.html", group_error=process_error(err)) return oauth2_get("oauth2/user/group").either( @@ -60,18 +60,18 @@ def join_or_create(): flash("You are already a member of a group.", "alert-info") return redirect(url_for("oauth2.user.user_profile")) def __group_error__(err): - return render_template( + return render_ui( "oauth2/group_join_or_create.html", groups=[], groups_error=process_error(err)) def __group_success__(groups): return oauth2_get("oauth2/user/group/join-request").either( __gjr_error__, partial(__gjr_success__, groups=groups)) def __gjr_error__(err): - return render_template( + return render_ui( "oauth2/group_join_or_create.html", groups=[], gjr_error=process_error(err)) def __gjr_success__(gjr, groups): - return render_template( + return render_ui( "oauth2/group_join_or_create.html", groups=groups, group_join_request=gjr) return oauth2_get("oauth2/group/list").either( @@ -95,11 +95,11 @@ def list_join_requests() -> Response: def __ts_to_dt_str__(timestamp): return datetime.datetime.fromtimestamp(timestamp).isoformat() def __fail__(error): - return render_template( + return render_ui( "oauth2/join-requests.html", error=process_error(error), requests=[]) def __success__(requests): - return render_template( + return render_ui( "oauth2/join-requests.html", error=False, requests=requests, datetime_string=__ts_to_dt_str__) return oauth2_get("oauth2/group/requests/join/list").either( @@ -142,10 +142,10 @@ def reject_join_request(): def group_role(group_role_id: uuid.UUID): """View the details of a particular role.""" def __render_error(**kwargs): - return render_template("oauth2/view-group-role.html", **kwargs) + return render_ui("oauth2/view-group-role.html", **kwargs) def __gprivs_success__(role, group_privileges): - return render_template( + return render_ui( "oauth2/view-group-role.html", group_role=role, group_privileges=tuple( priv for priv in group_privileges diff --git a/wqflask/wqflask/oauth2/resources.py b/wqflask/wqflask/oauth2/resources.py index 1bce85ef..c4f3744b 100644 --- a/wqflask/wqflask/oauth2/resources.py +++ b/wqflask/wqflask/oauth2/resources.py @@ -1,8 +1,8 @@ import uuid -from flask import ( - flash, request, url_for, redirect, Response, Blueprint, render_template) +from flask import flash, request, url_for, redirect, Response, Blueprint +from .ui import render_ui from .checks import require_oauth2 from .client import oauth2_get, oauth2_post from .request_utils import ( @@ -15,7 +15,7 @@ resources = Blueprint("resource", __name__) def user_resources(): """List the resources the user has access to.""" def __success__(resources): - return render_template("oauth2/resources.html", resources=resources) + return render_ui("oauth2/resources.html", resources=resources) return oauth2_get("oauth2/user/resources").either( request_error, __success__) @@ -25,7 +25,7 @@ def user_resources(): def create_resource(): """Create a new resource.""" def __render_template__(categories=[], error=None): - return render_template( + return render_ui( "oauth2/create-resource.html", resource_categories=categories, resource_category_error=error, @@ -60,7 +60,7 @@ def view_resource(resource_id: uuid.UUID): def __users_success__( resource, unlinked_data, users_n_roles, this_user, group_roles, users): - return render_template( + return render_ui( "oauth2/view-resource.html", resource=resource, unlinked_data=unlinked_data, users_n_roles=users_n_roles, this_user=this_user, group_roles=group_roles, users=users) @@ -68,7 +68,7 @@ def view_resource(resource_id: uuid.UUID): def __group_roles_success__( resource, unlinked_data, users_n_roles, this_user, group_roles): return oauth2_get("oauth2/user/list").either( - lambda err: render_template( + lambda err: render_ui( "oauth2/view-resource.html", resource=resource, unlinked_data=unlinked_data, users_n_roles=users_n_roles, this_user=this_user, group_roles=group_roles, @@ -79,7 +79,7 @@ def view_resource(resource_id: uuid.UUID): def __this_user_success__(resource, unlinked_data, users_n_roles, this_user): return oauth2_get("oauth2/group/roles").either( - lambda err: render_template( + lambda err: render_ui( "oauth2/view-resources.html", resource=resource, unlinked_data=unlinked_data, users_n_roles=users_n_roles, this_user=this_user, group_roles_error=process_error(err)), @@ -88,7 +88,7 @@ def view_resource(resource_id: uuid.UUID): def __users_n_roles_success__(resource, unlinked_data, users_n_roles): return oauth2_get("oauth2/user").either( - lambda err: render_template( + lambda err: render_ui( "oauth2/view-resources.html", this_user_error=process_error(err)), lambda usr_dets: __this_user_success__( @@ -96,26 +96,26 @@ def view_resource(resource_id: uuid.UUID): def __unlinked_success__(resource, unlinked_data): return oauth2_get(f"oauth2/resource/{resource_id}/user/list").either( - lambda err: render_template( + lambda err: render_ui( "oauth2/view-resource.html", resource=resource, unlinked_data=unlinked_data, users_n_roles_error=process_error(err)), lambda users_n_roles: __users_n_roles_success__( resource, unlinked_data, users_n_roles)) - return render_template( + return render_ui( "oauth2/view-resource.html", resource=resource, error=None, unlinked_data=unlinked) def __resource_success__(resource): dataset_type = resource["resource_category"]["resource_category_key"] return oauth2_get(f"oauth2/group/{dataset_type}/unlinked-data").either( - lambda err: render_template( + lambda err: render_ui( "oauth2/view-resource.html", resource=resource, unlinked_error=process_error(err)), lambda unlinked: __unlinked_success__(resource, unlinked)) return oauth2_get(f"oauth2/resource/view/{resource_id}").either( - lambda err: render_template("oauth2/view-resource.html", + lambda err: render_ui("oauth2/view-resource.html", resource=None, error=process_error(err)), __resource_success__) diff --git a/wqflask/wqflask/oauth2/roles.py b/wqflask/wqflask/oauth2/roles.py index cabfdfac..652719a8 100644 --- a/wqflask/wqflask/oauth2/roles.py +++ b/wqflask/wqflask/oauth2/roles.py @@ -1,8 +1,9 @@ """Handle role endpoints""" import uuid -from flask import flash, request, url_for, redirect, Blueprint, render_template +from flask import flash, request, url_for, redirect, Blueprint +from .ui import render_ui from .checks import require_oauth2 from .client import oauth2_get, oauth2_post from .request_utils import request_error, process_error @@ -13,13 +14,13 @@ roles = Blueprint("role", __name__) @require_oauth2 def user_roles(): def __grerror__(roles, user_privileges, error): - return render_template( + return render_ui( "oauth2/list_roles.html", roles=roles, user_privileges=user_privileges, group_roles_error=process_error(error)) def __grsuccess__(roles, user_privileges, group_roles): - return render_template( + return render_ui( "oauth2/list_roles.html", roles=roles, user_privileges=user_privileges, group_roles=group_roles) @@ -38,7 +39,7 @@ def user_roles(): @require_oauth2 def role(role_id: uuid.UUID): def __success__(the_role): - return render_template("oauth2/role.html", role=the_role) + return render_ui("oauth2/role.html", role=the_role) return oauth2_get(f"oauth2/role/view/{role_id}").either( request_error, __success__) @@ -48,11 +49,11 @@ def role(role_id: uuid.UUID): def create_role(): """Create a new role.""" def __roles_error__(error): - return render_template( + return render_ui( "oauth2/create-role.html", roles_error=process_error(error)) def __gprivs_error__(roles, error): - return render_template( + return render_ui( "oauth2/create-role.html", roles=roles, group_privileges_error=process_error(error)) @@ -60,7 +61,7 @@ def create_role(): uprivs = tuple( privilege["privilege_id"] for role in roles for privilege in role["privileges"]) - return render_template( + return render_ui( "oauth2/create-role.html", roles=roles, user_privileges=uprivs, group_privileges=gprivs, prev_role_name=request.args.get("role_name")) diff --git a/wqflask/wqflask/oauth2/users.py b/wqflask/wqflask/oauth2/users.py index d1bf0e5b..a82eb5c6 100644 --- a/wqflask/wqflask/oauth2/users.py +++ b/wqflask/wqflask/oauth2/users.py @@ -4,8 +4,9 @@ from urllib.parse import urljoin from authlib.integrations.base_client.errors import OAuthError from flask import ( flash, request, session, url_for, redirect, Response, Blueprint, - render_template, current_app as app) + current_app as app) +from .ui import render_ui from .checks import require_oauth2, user_logged_in from .client import oauth2_get, oauth2_post, oauth2_client from .request_utils import user_details, request_error, process_error @@ -19,7 +20,7 @@ def user_profile(): usr_dets = user_details() client = oauth2_client() def __render__(usr_dets, roles=[], **kwargs): - return render_template( + return render_ui( "oauth2/view-user.html", user_details=usr_dets, roles=roles, user_privileges = tuple( privilege["privilege_id"] for role in roles @@ -76,7 +77,7 @@ def login(): session["oauth2_token"] = token except OAuthError as _oaerr: flash(_oaerr.args[0], "alert-danger") - return render_template( + return render_ui( "oauth2/login.html", next_endpoint=next_endpoint, email=form.get("email_address")) @@ -85,7 +86,7 @@ def login(): return redirect(url_for(next_endpoint)) return redirect("/") - return render_template("oauth2/login.html", next_endpoint=next_endpoint) + return render_ui("oauth2/login.html", next_endpoint=next_endpoint) @users.route("/logout", methods=["GET", "POST"]) def logout(): @@ -97,8 +98,9 @@ def logout(): keys = tuple(key for key in session.keys() if not key.startswith("_")) for key in keys: session.pop(key, default=None) + flash("Successfully logged out.", "alert-success") - return redirect("/") + return redirect(url_for("oauth2.user.login")) @users.route("/register", methods=["GET", "POST"]) def register_user(): @@ -110,7 +112,7 @@ def register_user(): return redirect(next_endpoint) if request.method == "GET": - return render_template("oauth2/register_user.html") + return render_ui("oauth2/register_user.html") config = app.config form = request.form diff --git a/wqflask/wqflask/templates/oauth2/create-resource.html b/wqflask/wqflask/templates/oauth2/create-resource.html index 1b4263ed..817f18fe 100644 --- a/wqflask/wqflask/templates/oauth2/create-resource.html +++ b/wqflask/wqflask/templates/oauth2/create-resource.html @@ -8,7 +8,7 @@ {%block content%}