From fa4c05ccae770b689a407e09c3c3f10dda188767 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Sat, 18 Mar 2023 11:37:26 +0300 Subject: oauth2: resources: toggle whether a resource is public or not. --- wqflask/wqflask/oauth2/request_utils.py | 6 ++++ wqflask/wqflask/oauth2/resources.py | 22 ++++++++++++++- .../wqflask/templates/oauth2/view-resource.html | 32 ++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/wqflask/wqflask/oauth2/request_utils.py b/wqflask/wqflask/oauth2/request_utils.py index d4b2c23f..ed523614 100644 --- a/wqflask/wqflask/oauth2/request_utils.py +++ b/wqflask/wqflask/oauth2/request_utils.py @@ -58,3 +58,9 @@ def handle_success( return redirect(url_for(redirect_uri, **kwargs)) return __handler__ + +def flash_error(error): + flash(f"{error['error']}: {error['error_description']}", "alert-danger") + +def flash_success(success): + flash(f"{success['description']}", "alert-success") diff --git a/wqflask/wqflask/oauth2/resources.py b/wqflask/wqflask/oauth2/resources.py index aacde369..1bce85ef 100644 --- a/wqflask/wqflask/oauth2/resources.py +++ b/wqflask/wqflask/oauth2/resources.py @@ -5,7 +5,8 @@ from flask import ( from .checks import require_oauth2 from .client import oauth2_get, oauth2_post -from .request_utils import request_error, process_error +from .request_utils import ( + flash_error, flash_success, request_error, process_error) resources = Blueprint("resource", __name__) @@ -239,6 +240,25 @@ def unassign_role(resource_id: uuid.UUID) -> Response: flash(aserr.args[0], "alert-danger") return redirect(url_for("oauth2.resources.view_resource", resource_id=resource_id)) +@resources.route("/toggle/", methods=["POST"]) +@require_oauth2 +def toggle_public(resource_id: uuid.UUID): + """Toggle the given resource's public status.""" + def __handle_error__(err): + flash_error(process_error(err)) + return redirect(url_for( + "oauth2.resource.view_resource", resource_id=resource_id)) + + def __handle_success__(success): + flash_success(success) + return redirect(url_for( + "oauth2.resource.view_resource", resource_id=resource_id)) + + return oauth2_post( + f"oauth2/resource/{resource_id}/toggle-public", data={}).either( + lambda err: __handle_error__(err), + lambda suc: __handle_success__(suc)) + @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 14e7872b..a1e49d34 100644 --- a/wqflask/wqflask/templates/oauth2/view-resource.html +++ b/wqflask/wqflask/templates/oauth2/view-resource.html @@ -27,6 +27,7 @@ Name Category Group + Actions @@ -35,6 +36,37 @@ {{resource.resource_name}} {{resource.resource_category.resource_category_description}} {{resource.group.group_name}} + +
+ +
+ {%if resource.public%} + + {%else%} + + {%endif%} +
+
+ + + Edit + + + Delete + -- cgit v1.2.3