From c236eded0a4316a7a3e61d4af9b745860d70e2d2 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Mon, 6 Feb 2023 14:21:17 +0300 Subject: oauth2: resources: View resource. Handle creation error/success * Handle the resource creation success or error in a better way than simply dropping the user to a plaintext/json results page. * Enable viewing a particular resource's details. --- wqflask/wqflask/oauth2/resources.py | 18 ++++++--- wqflask/wqflask/templates/oauth2/resources.html | 4 +- .../wqflask/templates/oauth2/view-resource.html | 47 ++++++++++++++++++++++ 3 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 wqflask/wqflask/templates/oauth2/view-resource.html diff --git a/wqflask/wqflask/oauth2/resources.py b/wqflask/wqflask/oauth2/resources.py index 28a5a362..db23fc75 100644 --- a/wqflask/wqflask/oauth2/resources.py +++ b/wqflask/wqflask/oauth2/resources.py @@ -1,6 +1,6 @@ import uuid -from flask import request, Blueprint, render_template +from flask import flash, request, url_for, redirect, Blueprint, render_template from .checks import require_oauth2 from .client import oauth2_get, oauth2_post @@ -36,11 +36,13 @@ def create_resource(): from flask import jsonify def __perr__(error): - print(f"ERROR: {process_error(error)}") - return jsonify(process_error(error)) + err = process_error(error) + print(f"THE ERROR: {err}") + flash(f"{err['error']}: {err['error_message']}", "alert-danger") + return redirect(url_for("oauth2.resource.user_resources")) def __psuc__(succ): - print(f"SUCCESS: {succ.json()}") - return jsonify(succ.json()) + flash("Resource created successfully", "alert-success") + return redirect(url_for("oauth2.resource.user_resources")) return oauth2_post( "oauth2/resource/create", data=request.form).either( __perr__, __psuc__) @@ -52,7 +54,11 @@ def view_resource(resource_id: uuid.UUID): # Display the resource's details # Provide edit/delete options # Metadata edit maybe? - return "WOULD DISPLAY THE GIVEN RESOURCE'S DETAILS" + return oauth2_get(f"oauth2/resource/view/{resource_id}").either( + lambda err: render_template("oauth2/view-resource.html", + resource=None, error=process_error(err)), + lambda resource: render_template( + "oauth2/view-resource.html", resource=resource, error=None)) @resources.route("/edit/", methods=["GET"]) @require_oauth2 diff --git a/wqflask/wqflask/templates/oauth2/resources.html b/wqflask/wqflask/templates/oauth2/resources.html index 19730f6b..08233921 100644 --- a/wqflask/wqflask/templates/oauth2/resources.html +++ b/wqflask/wqflask/templates/oauth2/resources.html @@ -30,7 +30,9 @@ {%for resource in resources %} - {{resource.resource_name}} diff --git a/wqflask/wqflask/templates/oauth2/view-resource.html b/wqflask/wqflask/templates/oauth2/view-resource.html new file mode 100644 index 00000000..0ebd7f7a --- /dev/null +++ b/wqflask/wqflask/templates/oauth2/view-resource.html @@ -0,0 +1,47 @@ +{%extends "base.html"%} +{%from "oauth2/profile_nav.html" import profile_nav%} +{%block title%}View User{%endblock%} +{%block content%} +
+ {{profile_nav("resources")}} +

Resources

+ + {{flash_me()}} + +
+ +
+ {%if error %} + + +   + {{error.error}} + {{error.error_description}} + {%else%} + + + + + + + + + + + + + + + + + +
Resource: {{resource.resource_name}}
NameCategoryGroup
{{resource.resource_name}}{{resource.resource_category.resource_category_description}}{{resource.group.group_name}}
+ +

List the resource's data here...

+ {%endif%} +
+ +
+ +
+{%endblock%} -- cgit v1.2.3