aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2023-01-31 05:03:15 +0300
committerFrederick Muriuki Muriithi2023-01-31 05:03:15 +0300
commitc6929888dd5dcbae1626a3a3709c51a1e5771a89 (patch)
tree7c3c0b3ffa1ba33b6b4c23fe9788e42c28992b52
parent5eb884453ba41776d91712e97ec8b8e4b6d4599b (diff)
downloadgenenetwork2-c6929888dd5dcbae1626a3a3709c51a1e5771a89.tar.gz
oauth2: Fix bugs with group creation. Rename functions.
Fix some bugs with group creation, mainly correctiong the urls. Rename importable functions to remove the double underscores.
-rw-r--r--wqflask/wqflask/oauth2/groups.py24
-rw-r--r--wqflask/wqflask/oauth2/request_utils.py29
-rw-r--r--wqflask/wqflask/oauth2/resources.py4
-rw-r--r--wqflask/wqflask/oauth2/roles.py6
-rw-r--r--wqflask/wqflask/oauth2/users.py6
-rw-r--r--wqflask/wqflask/templates/base_macro.html2
6 files changed, 38 insertions, 33 deletions
diff --git a/wqflask/wqflask/oauth2/groups.py b/wqflask/wqflask/oauth2/groups.py
index d2102404..eab0049c 100644
--- a/wqflask/wqflask/oauth2/groups.py
+++ b/wqflask/wqflask/oauth2/groups.py
@@ -1,8 +1,10 @@
-from flask import Blueprint, render_template
+from flask import (
+ flash, session, request, url_for, redirect, Blueprint, render_template)
from .checks import require_oauth2
from .client import oauth2_get, oauth2_post
-from .request_utils import __user_details__, __request_error__
+from .request_utils import (
+ user_details, handle_error, request_error, handle_success)
groups = Blueprint("group", __name__)
@@ -12,7 +14,7 @@ def user_group():
return render_template("oauth2/group.html", group=group)
return oauth2_get("oauth2/user-group").either(
- __request_error__, __success__)
+ request_error, __success__)
@groups.route("/create", methods=["POST"])
@require_oauth2
@@ -22,18 +24,18 @@ def create_group():
resp = oauth2_post("oauth2/create-group", data=dict(request.form))
return resp.either(
- __handle_error__("oauth2.group_join_or_create"),
- __handle_success__(
- "Created group", "oauth2.user_profile",
- response_handlers=__setup_group__))
+ handle_error("oauth2.group.join_or_create"),
+ handle_success(
+ "Created group", "oauth2.user.user_profile",
+ response_handlers=[__setup_group__]))
@groups.route("/join-or-create", methods=["GET"])
@require_oauth2
def join_or_create():
- user_details = __user_details__()
- if bool(user_details["group"]):
- flash("You are already a member of a group.", "alert info.")
- return redirect(url_for("oauth2.user_profile"))
+ usr_dets = user_details()
+ if bool(usr_dets["group"]):
+ flash("You are already a member of a group.", "alert-info")
+ return redirect(url_for("oauth2.user.user_profile"))
groups = oauth2_get("oauth2/groups").either(
lambda x: __raise_unimplemented__(), lambda x: x)
return render_template("oauth2/group_join_or_create.html", groups=groups)
diff --git a/wqflask/wqflask/oauth2/request_utils.py b/wqflask/wqflask/oauth2/request_utils.py
index 7aaad001..f471d133 100644
--- a/wqflask/wqflask/oauth2/request_utils.py
+++ b/wqflask/wqflask/oauth2/request_utils.py
@@ -2,26 +2,29 @@
from typing import Optional
from flask import (
- session, url_for, redirect, render_template, current_app as app)
+ flash, session, url_for, redirect, render_template, current_app as app)
-def __raise_unimplemented__():
+from .client import oauth2_get
+
+def raise_unimplemented():
raise Exception("NOT IMPLEMENTED")
-def __user_details__():
- return session.get("user_details", False) or get_endpoint(
- "oauth2/user").maybe(False, __id__)
+def user_details():
+ return oauth2_get("oauth2/user").either(
+ handle_error("oauth2.login"),
+ lambda usr_dets: usr_dets)
-def __request_error__(response):
+def request_error(response):
app.logger.error(f"{response}: {response.url} [{response.status_code}]")
return render_template("oauth2/request_error.html", response=response)
-
-def __handle_error__(redirect_uri: Optional[str] = None, **kwargs):
+def handle_error(redirect_uri: Optional[str] = None, **kwargs):
def __handler__(error):
- print(f"ERROR: {error}")
- msg = error.get(
- "error_message", error.get("error_description", "undefined error"))
- flash(f"{error['error']}: {msg}.",
+ error_json = error.json()
+ msg = error_json.get(
+ "error_message", error_json.get(
+ "error_description", "undefined error"))
+ flash(f"{error_json['error']}: {msg}.",
"alert-danger")
if "response_handlers" in kwargs:
for handler in kwargs["response_handlers"]:
@@ -31,7 +34,7 @@ def __handle_error__(redirect_uri: Optional[str] = None, **kwargs):
return __handler__
-def __handle_success__(
+def handle_success(
success_msg: str, redirect_uri: Optional[str] = None, **kwargs):
def __handler__(response):
flash(f"Success: {success_msg}.", "alert-success")
diff --git a/wqflask/wqflask/oauth2/resources.py b/wqflask/wqflask/oauth2/resources.py
index 578e0414..be814314 100644
--- a/wqflask/wqflask/oauth2/resources.py
+++ b/wqflask/wqflask/oauth2/resources.py
@@ -4,7 +4,7 @@ from flask import Blueprint, render_template
from .client import oauth2_get
from .checks import require_oauth2
-from .request_utils import __request_error__
+from .request_utils import request_error
resources = Blueprint("resource", __name__)
@@ -16,7 +16,7 @@ def user_resources():
return render_template("oauth2/resources.html", resources=resources)
return oauth2_get("oauth2/user-resources").either(
- __request_error__, __success__)
+ request_error, __success__)
@resources.route("/create", methods=["GET"])
@require_oauth2
diff --git a/wqflask/wqflask/oauth2/roles.py b/wqflask/wqflask/oauth2/roles.py
index 38058893..8a57cee8 100644
--- a/wqflask/wqflask/oauth2/roles.py
+++ b/wqflask/wqflask/oauth2/roles.py
@@ -5,7 +5,7 @@ from flask import Blueprint, render_template
from .checks import require_oauth2
from .client import oauth2_get, oauth2_post
-from .request_utils import __request_error__
+from .request_utils import request_error
roles = Blueprint("role", __name__)
@@ -16,7 +16,7 @@ def user_roles():
return render_template("oauth2/list_roles.html", roles=roles)
return oauth2_get("oauth2/user-roles").either(
- __request_error__, __success__)
+ request_error, __success__)
@roles.route("/role/<uuid:role_id>", methods=["GET"])
@require_oauth2
@@ -25,4 +25,4 @@ def role(role_id: uuid.UUID):
return render_template("oauth2/role.html", role=the_role)
return oauth2_get(f"oauth2/role/{role_id}").either(
- __request_error__, __success__)
+ request_error, __success__)
diff --git a/wqflask/wqflask/oauth2/users.py b/wqflask/wqflask/oauth2/users.py
index bd958049..1b2214eb 100644
--- a/wqflask/wqflask/oauth2/users.py
+++ b/wqflask/wqflask/oauth2/users.py
@@ -2,7 +2,7 @@ from flask import Blueprint, render_template
from .checks import require_oauth2
from .client import oauth2_get, oauth2_client
-from .request_utils import __user_details__, __request_error__
+from .request_utils import user_details, request_error
users = Blueprint("user", __name__)
@@ -10,12 +10,12 @@ users = Blueprint("user", __name__)
@require_oauth2
def user_profile():
__id__ = lambda the_val: the_val
- user_details = __user_details__()
+ usr_dets = user_details()
client = oauth2_client()
roles = oauth2_get("oauth2/user-roles").either(lambda x: "Error", lambda x: x)
return render_template(
- "oauth2/view-user.html", user_details=user_details, roles=roles)
+ "oauth2/view-user.html", user_details=usr_dets, roles=roles)
@users.route("/request-add-to-group", methods=["POST"])
@require_oauth2
diff --git a/wqflask/wqflask/templates/base_macro.html b/wqflask/wqflask/templates/base_macro.html
index c2905ebe..7fcb6fe7 100644
--- a/wqflask/wqflask/templates/base_macro.html
+++ b/wqflask/wqflask/templates/base_macro.html
@@ -3,7 +3,7 @@
{% if messages %}
<div class="container">
{% for category, message in messages %}
- <div class="alert {{ category }}">{{ message }}</div>
+ <div class="alert {{ category }}" role="alert">{{ message }}</div>
{% endfor %}
</div>
{% endif %}