From a29c567aad3d579ec8f62ad824a979f5248f5c4d Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Thu, 20 Apr 2023 10:00:09 +0300 Subject: Fix bugs: Add missing module. Make args mandatory Add missing `wqflask.oauth2.ui` module Make arguments to the menu-generation macro mandatory going forward. --- wqflask/wqflask/oauth2/ui.py | 23 +++++++++++++++++++++++ wqflask/wqflask/templates/oauth2/profile_nav.html | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 wqflask/wqflask/oauth2/ui.py (limited to 'wqflask') diff --git a/wqflask/wqflask/oauth2/ui.py b/wqflask/wqflask/oauth2/ui.py new file mode 100644 index 00000000..31b873be --- /dev/null +++ b/wqflask/wqflask/oauth2/ui.py @@ -0,0 +1,23 @@ +"""UI utilities""" +from flask import session, render_template + +from .client import oauth2_get + +def render_ui(templatepath: str, **kwargs): + """Handle repetitive UI rendering stuff.""" + logged_in = ("oauth2_token" in session and bool(session["oauth2_token"])) + roles = kwargs.get("roles", tuple()) # Get roles if already provided + if logged_in and not bool(roles): # If not, try fetching them + roles_results = oauth2_get("oauth2/user/roles").either( + lambda err: {"roles_error": process_error(err)}, + lambda roles: {"roles": roles}) + kwargs = {**kwargs, **roles_results} + roles = kwargs.get("roles", tuple()) + user_privileges = tuple( + privilege["privilege_id"] for role in roles + for privilege in role["privileges"]) + kwargs = { + **kwargs, "roles": roles, "user_privileges": user_privileges, + "logged_in": logged_in + } + return render_template(templatepath, **kwargs) diff --git a/wqflask/wqflask/templates/oauth2/profile_nav.html b/wqflask/wqflask/templates/oauth2/profile_nav.html index 7260ec16..c8d018fd 100644 --- a/wqflask/wqflask/templates/oauth2/profile_nav.html +++ b/wqflask/wqflask/templates/oauth2/profile_nav.html @@ -1,4 +1,4 @@ -{%macro profile_nav(calling_page, logged_in=false, user_privileges=[])%} +{%macro profile_nav(calling_page, logged_in, user_privileges)%}