From 8110aba2b1d36a15b315873fc6881845836c4a2f Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Wed, 8 Feb 2023 17:23:29 +0300 Subject: oauth2: groups: Handle join requests. --- wqflask/wqflask/oauth2/groups.py | 50 +++++++++++++-- wqflask/wqflask/oauth2/request_utils.py | 5 +- .../wqflask/templates/oauth2/display_error.html | 10 +++ wqflask/wqflask/templates/oauth2/group.html | 11 ++++ .../wqflask/templates/oauth2/join-requests.html | 72 ++++++++++++++++++++++ 5 files changed, 142 insertions(+), 6 deletions(-) create mode 100644 wqflask/wqflask/templates/oauth2/display_error.html create mode 100644 wqflask/wqflask/templates/oauth2/join-requests.html diff --git a/wqflask/wqflask/oauth2/groups.py b/wqflask/wqflask/oauth2/groups.py index f91a872b..b49cb550 100644 --- a/wqflask/wqflask/oauth2/groups.py +++ b/wqflask/wqflask/oauth2/groups.py @@ -1,5 +1,9 @@ +import datetime +from functools import partial + from flask import ( - flash, session, request, url_for, redirect, Blueprint, render_template) + flash, session, request, url_for, redirect, Response, Blueprint, + render_template) from .checks import require_oauth2 from .client import oauth2_get, oauth2_post @@ -12,14 +16,20 @@ groups = Blueprint("group", __name__) @groups.route("/", methods=["GET"]) 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( + "oauth2/group.html", group=group, users=users, + group_join_requests_error=process_error(error)), + lambda gjr: render_template( + "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( "oauth2/group.html", group=group, user_error=process_error(error)), - lambda users: render_template( - "oauth2/group.html", group=group, users=users)) + partial(__get_join_requests__, group)) return oauth2_get("oauth2/user/group").either( request_error, __success__) @@ -59,3 +69,35 @@ def delete_group(group_id): def edit_group(group_id): """Edit the user's group.""" return "WOULD EDIT GROUP." + +@groups.route("/list-join-requests", methods=["GET"]) +@require_oauth2 +def list_join_requests() -> Response: + def __ts_to_dt_str__(timestamp): + return datetime.datetime.fromtimestamp(timestamp).isoformat() + def __fail__(error): + return render_template( + "oauth2/join-requests.html", error=process_error(error), + requests=[]) + def __success__(requests): + return render_template( + "oauth2/join-requests.html", error=False, requests=requests, + datetime_string=__ts_to_dt_str__) + return oauth2_get("oauth2/group/requests/join/list").either( + __fail__, __success__) + +@groups.route("/accept-join-requests", methods=["POST"]) +@require_oauth2 +def accept_join_request(): + def __fail__(error): + err=process_error() + flash("{}", "alert-danger") + return redirect(url_for("oauth2.group.list_join_requests")) + def __success__(requests): + flash("Request was accepted successfully.", "alert-success") + return redirect(url_for("oauth2.group.list_join_requests")) + return oauth2_post( + "oauth2/group/requests/join/accept", + data=request.form).either( + handle_error("oauth2.group.list_join_requests"), + __success__) diff --git a/wqflask/wqflask/oauth2/request_utils.py b/wqflask/wqflask/oauth2/request_utils.py index aa073266..d4b2c23f 100644 --- a/wqflask/wqflask/oauth2/request_utils.py +++ b/wqflask/wqflask/oauth2/request_utils.py @@ -22,7 +22,8 @@ def process_error(error: Response, if error.status_code == 404: return { "error": "NotFoundError", - "error_message": message + "error_message": message, + "error_description": message } return error.json() @@ -32,7 +33,7 @@ def request_error(response): def handle_error(redirect_uri: Optional[str] = None, **kwargs): def __handler__(error): - error_json = error.json() + error_json = process_error(error)# error.json() msg = error_json.get( "error_message", error_json.get( "error_description", "undefined error")) diff --git a/wqflask/wqflask/templates/oauth2/display_error.html b/wqflask/wqflask/templates/oauth2/display_error.html new file mode 100644 index 00000000..9abe02c4 --- /dev/null +++ b/wqflask/wqflask/templates/oauth2/display_error.html @@ -0,0 +1,10 @@ +{%macro display_error(title, the_error)%} + +{{title}}: + + + +{{the_error.error}}: +{{the_error.error_description}} + +{%endmacro%} diff --git a/wqflask/wqflask/templates/oauth2/group.html b/wqflask/wqflask/templates/oauth2/group.html index e7a79933..a6a3e6c3 100644 --- a/wqflask/wqflask/templates/oauth2/group.html +++ b/wqflask/wqflask/templates/oauth2/group.html @@ -1,5 +1,6 @@ {%extends "base.html"%} {%from "oauth2/profile_nav.html" import profile_nav%} +{%from "oauth2/display_error.html" import display_error%} {%block title%}View User{%endblock%} {%block content%}
Name | +Request Date/Time | +Status | +Message | +Actions | +||
---|---|---|---|---|---|---|
{{request.name}} | +{{request.email}} | +{{datetime_string(request.timestamp)}} | +{{request.status}} | +{{request.message}} | ++ + | ++ + | +
+ {%if error is defined %} + {{display_error("Join Requests", error)}} + {%else%} + No one has requested to join your group yet. + {%endif%} + | +