aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2023-02-08 17:23:29 +0300
committerFrederick Muriuki Muriithi2023-02-08 17:23:29 +0300
commit8110aba2b1d36a15b315873fc6881845836c4a2f (patch)
treeb1da7892d92fdbbaf961b1150a84f1dcccc78fcb
parent63474180915d3d39d5d649dd2eef8d5fa54773c4 (diff)
downloadgenenetwork2-8110aba2b1d36a15b315873fc6881845836c4a2f.tar.gz
oauth2: groups: Handle join requests.
-rw-r--r--wqflask/wqflask/oauth2/groups.py50
-rw-r--r--wqflask/wqflask/oauth2/request_utils.py5
-rw-r--r--wqflask/wqflask/templates/oauth2/display_error.html10
-rw-r--r--wqflask/wqflask/templates/oauth2/group.html11
-rw-r--r--wqflask/wqflask/templates/oauth2/join-requests.html72
5 files changed, 142 insertions, 6 deletions
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)%}
+
+<strong>{{title}}</strong>:
+<span class="glyphicon glyphicon-exclamation-sign text-danger">
+</span>
+&nbsp;
+<strong class="text-danger">{{the_error.error}}:</strong>
+{{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%}
<div class="container" style="min-width: 1250px;">
@@ -9,6 +10,16 @@
<div class="container-fluid">
<div class="row">
+ {%if group_join_requests_error is defined %}
+ {{display_error("Join Requests", group_join_requests_error)}}
+ {%else%}
+ <a href="{{url_for('oauth2.group.list_join_requests')}}"
+ class="btn btn-info">
+ Requests ({{group_join_requests | count}})
+ </a>
+ {%endif%}
+ </div>
+ <div class="row">
<table class="table">
<caption>Group Information</caption>
<thead>
diff --git a/wqflask/wqflask/templates/oauth2/join-requests.html b/wqflask/wqflask/templates/oauth2/join-requests.html
new file mode 100644
index 00000000..996caf2e
--- /dev/null
+++ b/wqflask/wqflask/templates/oauth2/join-requests.html
@@ -0,0 +1,72 @@
+{%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%}
+<div class="container" style="min-width: 1250px;">
+ {{profile_nav("group")}}
+
+ {{flash_me()}}
+
+ <div class="container-fluid">
+ <div class="row">
+ <table class="table">
+ <caption>Join Requests</caption>
+ <thead>
+ <tr>
+ <th>Name</th>
+ <th>Email</th>
+ <th>Request Date/Time</th>
+ <th>Status</th>
+ <th>Message</th>
+ <th colspan="2" style="text-align: center;">Actions</th>
+ </tr>
+ </thead>
+
+ <tbody>
+ {%for request in requests%}
+ <tr>
+ <td>{{request.name}}</td>
+ <td>{{request.email}}</td>
+ <td>{{datetime_string(request.timestamp)}}</td>
+ <td>{{request.status}}</td>
+ <td>{{request.message}}</td>
+ <td>
+ <form method="POST"
+ action="{{url_for('oauth2.group.accept_join_request')}}">
+ <input type="hidden" name="request_id"
+ value="{{request.request_id}}" />
+ <input type="submit" class="btn btn-primary" value="Accept"
+ {%if request.status != "PENDING"%}
+ disabled="disabled"
+ {%endif%} />
+ </form>
+ </td>
+ <td>
+ <form method="POST" action="#reject">
+ <input type="hidden" name="request_id"
+ value="{{request.request_id}}" />
+ <input type="submit" class="btn btn-danger" value="Reject"
+ {%if request.status != "PENDING"%}
+ disabled="disabled"
+ {%endif%} />
+ </form>
+ </td>
+ </tr>
+ {%else%}
+ <tr>
+ <td colspan="3">
+ {%if error is defined %}
+ {{display_error("Join Requests", error)}}
+ {%else%}
+ No one has requested to join your group yet.
+ {%endif%}
+ </td>
+ </tr>
+ {%endfor%}
+ </tbody>
+ </table>
+ </div>
+ </div>
+</div>
+{%endblock%}