From 63474180915d3d39d5d649dd2eef8d5fa54773c4 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Tue, 7 Feb 2023 13:02:07 +0300 Subject: oauth2: groups: Enable user to request to be added to group. --- wqflask/wqflask/oauth2/users.py | 26 +++++++++++++++++----- .../templates/oauth2/group_join_or_create.html | 2 +- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/wqflask/wqflask/oauth2/users.py b/wqflask/wqflask/oauth2/users.py index 4b267c9f..67d6b27f 100644 --- a/wqflask/wqflask/oauth2/users.py +++ b/wqflask/wqflask/oauth2/users.py @@ -1,8 +1,9 @@ -from flask import Blueprint, render_template +from flask import ( + flash, request, url_for, redirect, Response, Blueprint, render_template) from .checks import require_oauth2 -from .client import oauth2_get, oauth2_client -from .request_utils import user_details, request_error +from .client import oauth2_get, oauth2_post, oauth2_client +from .request_utils import user_details, request_error, process_error users = Blueprint("user", __name__) @@ -19,5 +20,20 @@ def user_profile(): @users.route("/request-add-to-group", methods=["POST"]) @require_oauth2 -def request_add_to_group(): - return "WOULD SEND MESSAGE TO HAVE YOU ADDED TO GROUP..." +def request_add_to_group() -> Response: + """Request to be added to a group.""" + form = request.form + group_id = form["group"] + + def __error__(error): + err = process_error(error) + flash(f"{err['error']}: {err['error_message']}", "alert-danger") + return redirect(url_for("oauth2.user.user_profile")) + + def __success__(response): + flash(f"{response['message']} (Response ID: {response['request_id']})", + "alert-success") + return redirect(url_for("oauth2.user.user_profile")) + + return oauth2_post(f"oauth2/group/requests/join/{group_id}", + data=form).either(__error__, __success__) diff --git a/wqflask/wqflask/templates/oauth2/group_join_or_create.html b/wqflask/wqflask/templates/oauth2/group_join_or_create.html index 0ec5674b..73f7a5de 100644 --- a/wqflask/wqflask/templates/oauth2/group_join_or_create.html +++ b/wqflask/wqflask/templates/oauth2/group_join_or_create.html @@ -27,7 +27,7 @@