From 19fb26a1021bb5f213de109258a389559d1bc0e1 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Fri, 3 Feb 2023 15:20:20 +0300 Subject: oauth2: resource: implement incomplete resource creation flow --- wqflask/wqflask/oauth2/groups.py | 12 +---- wqflask/wqflask/oauth2/request_utils.py | 14 ++++- wqflask/wqflask/oauth2/resources.py | 31 +++++++++-- .../wqflask/templates/oauth2/create-resource.html | 62 ++++++++++++++++++++++ wqflask/wqflask/templates/oauth2/group.html | 2 +- wqflask/wqflask/templates/oauth2/resources.html | 45 ++++++++++++---- 6 files changed, 138 insertions(+), 28 deletions(-) create mode 100644 wqflask/wqflask/templates/oauth2/create-resource.html diff --git a/wqflask/wqflask/oauth2/groups.py b/wqflask/wqflask/oauth2/groups.py index 4d0bc4f5..f91a872b 100644 --- a/wqflask/wqflask/oauth2/groups.py +++ b/wqflask/wqflask/oauth2/groups.py @@ -4,7 +4,7 @@ from flask import ( from .checks import require_oauth2 from .client import oauth2_get, oauth2_post from .request_utils import ( - user_details, handle_error, request_error, handle_success, + user_details, handle_error, request_error, process_error, handle_success, raise_unimplemented) groups = Blueprint("group", __name__) @@ -12,20 +12,12 @@ groups = Blueprint("group", __name__) @groups.route("/", methods=["GET"]) def user_group(): """Get the user's group.""" - def __process_error__(error): - if error.status_code == 404: - return { - "error": "NotFoundError", - "error_message": ("Requested endpoint was not found on the " - "API server.") - } - return error.json() def __success__(group): return oauth2_get(f"oauth2/group/members/{group['group_id']}").either( lambda error: render_template( "oauth2/group.html", group=group, - user_error=__process_error__(error)), + user_error=process_error(error)), lambda users: render_template( "oauth2/group.html", group=group, users=users)) diff --git a/wqflask/wqflask/oauth2/request_utils.py b/wqflask/wqflask/oauth2/request_utils.py index f471d133..aa073266 100644 --- a/wqflask/wqflask/oauth2/request_utils.py +++ b/wqflask/wqflask/oauth2/request_utils.py @@ -2,7 +2,8 @@ from typing import Optional from flask import ( - flash, session, url_for, redirect, render_template, current_app as app) + flash, session, url_for, redirect, Response, render_template, + current_app as app) from .client import oauth2_get @@ -14,6 +15,17 @@ def user_details(): handle_error("oauth2.login"), lambda usr_dets: usr_dets) +def process_error(error: Response, + message: str=("Requested endpoint was not found on the API " + "server.") + ) -> dict: + if error.status_code == 404: + return { + "error": "NotFoundError", + "error_message": message + } + return error.json() + def request_error(response): app.logger.error(f"{response}: {response.url} [{response.status_code}]") return render_template("oauth2/request_error.html", response=response) diff --git a/wqflask/wqflask/oauth2/resources.py b/wqflask/wqflask/oauth2/resources.py index be3e1622..28a5a362 100644 --- a/wqflask/wqflask/oauth2/resources.py +++ b/wqflask/wqflask/oauth2/resources.py @@ -1,10 +1,10 @@ import uuid -from flask import Blueprint, render_template +from flask import request, Blueprint, render_template -from .client import oauth2_get from .checks import require_oauth2 -from .request_utils import request_error +from .client import oauth2_get, oauth2_post +from .request_utils import request_error, process_error resources = Blueprint("resource", __name__) @@ -18,11 +18,32 @@ def user_resources(): return oauth2_get("oauth2/user/resources").either( request_error, __success__) -@resources.route("/create", methods=["GET"]) +@resources.route("/create", methods=["GET", "POST"]) @require_oauth2 def create_resource(): """Create a new resource.""" - return "WOULD CREATE A NEW RESOURCE." + def __render_template__(categories=[], error=None): + return render_template( + "oauth2/create-resource.html", + resource_categories=categories, + resource_category_error=error) + + if request.method == "GET": + return oauth2_get("oauth2/resource/categories").either( + lambda error: __render_template__(error=process_error( + error, "Could not retrieve resource categories")), + lambda cats: __render_template__(categories=cats)) + + from flask import jsonify + def __perr__(error): + print(f"ERROR: {process_error(error)}") + return jsonify(process_error(error)) + def __psuc__(succ): + print(f"SUCCESS: {succ.json()}") + return jsonify(succ.json()) + return oauth2_post( + "oauth2/resource/create", data=request.form).either( + __perr__, __psuc__) @resources.route("/view/", methods=["GET"]) @require_oauth2 diff --git a/wqflask/wqflask/templates/oauth2/create-resource.html b/wqflask/wqflask/templates/oauth2/create-resource.html new file mode 100644 index 00000000..9d9a8cd3 --- /dev/null +++ b/wqflask/wqflask/templates/oauth2/create-resource.html @@ -0,0 +1,62 @@ +{%extends "base.html"%} +{%from "oauth2/profile_nav.html" import profile_nav%} + +{%block title%}Create Resource{%endblock%} +{%block css%} + +{%endblock%} + +{%block content%} +
+ {{profile_nav("resources")}} + + {{flash_me()}} + +
+
+ {%if resource_category_error%} +

+ +   + {{resource_category_error.error}}: + {{resource_category_error.error_message}} +

+ {%else%} +
+ +
+ + + + The resource name, e.g. the experiment name. + +
+ + +
+ {%for category in resource_categories%} +
+ + + {{category.resource_category_description}} + +
+ {%endfor%} +
+ + + +
+ {%endif%} +
+
+ +
+{%endblock%} diff --git a/wqflask/wqflask/templates/oauth2/group.html b/wqflask/wqflask/templates/oauth2/group.html index 8db5b0d9..e7a79933 100644 --- a/wqflask/wqflask/templates/oauth2/group.html +++ b/wqflask/wqflask/templates/oauth2/group.html @@ -81,7 +81,7 @@   {{user_error.error}} - {{user_error.error_message}} + {{user_error.error_description}} {%else%} No users found for this group {%endif%} diff --git a/wqflask/wqflask/templates/oauth2/resources.html b/wqflask/wqflask/templates/oauth2/resources.html index 44774fda..19730f6b 100644 --- a/wqflask/wqflask/templates/oauth2/resources.html +++ b/wqflask/wqflask/templates/oauth2/resources.html @@ -3,7 +3,7 @@ {%block title%}View User{%endblock%} {%block content%}
- {{profile_nav("dashboard")}} + {{profile_nav("resources")}}

Resources

{{flash_me()}} @@ -17,16 +17,39 @@
- {%for resource in resources %} - {{resource.resource_name}} - {{resource.resource_category}} - {{resource.group}} - {%else%} -

-   - - The user has no access to any resource. - -

- {%endfor%} + + + + + + + + + + + {%for resource in resources %} + + + + + + {%else%} + + + + {%endfor%} + +
Resources
NameCategoryGroup
+ + {{resource.resource_name}} + + {{resource.resource_category.resource_category_key}}{{resource.group.group_name}}
+   + + The user has no access to any resource. + +
-- cgit v1.2.3