From 0fb42e6f935c3a31196e5b529c1fcd355451188c Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Wed, 17 May 2023 11:15:33 +0300 Subject: auth: Integrate collections badge with auth Show the total number of collections available in the active session in the collections badge at the top of the page. Also do some minor UI tweaks to differentiate the user and anonymous collections. --- wqflask/wqflask/__init__.py | 4 +++- wqflask/wqflask/oauth2/collections.py | 16 ++++++++++++++++ wqflask/wqflask/templates/base.html | 2 +- wqflask/wqflask/templates/collections/list.html | 8 ++++++-- 4 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 wqflask/wqflask/oauth2/collections.py (limited to 'wqflask') diff --git a/wqflask/wqflask/__init__.py b/wqflask/wqflask/__init__.py index 38a69d33..13685324 100644 --- a/wqflask/wqflask/__init__.py +++ b/wqflask/wqflask/__init__.py @@ -32,6 +32,7 @@ from wqflask.api.markdown import news_blueprint from wqflask.api.jobs import jobs as jobs_bp from wqflask.oauth2.routes import oauth2 from wqflask.oauth2.checks import user_logged_in +from wqflask.oauth2.collections import num_collections from wqflask.oauth2.request_utils import user_details, authserver_authorise_uri from wqflask.jupyter_notebooks import jupyter_notebooks @@ -55,7 +56,8 @@ app.jinja_env.globals.update( numify=formatting.numify, logged_in=user_logged_in, authserver_authorise_uri=authserver_authorise_uri, - user_details=user_details) + user_details=user_details, + num_collections=num_collections) app.config["SESSION_REDIS"] = redis.from_url(app.config["REDIS_URL"]) diff --git a/wqflask/wqflask/oauth2/collections.py b/wqflask/wqflask/oauth2/collections.py new file mode 100644 index 00000000..e31b4ad2 --- /dev/null +++ b/wqflask/wqflask/oauth2/collections.py @@ -0,0 +1,16 @@ +"""Functions for collections.""" +from .session import session_info +from .checks import user_logged_in +from .client import oauth2_get, no_token_get + +def num_collections() -> int: + """Compute the number of collections available for tte current sussion.""" + anon_id = session_info()["anon_id"] + all_collections = no_token_get( + f"oauth2/user/collections/{anon_id}/list").either( + lambda _err: [], lambda colls: colls) + if user_logged_in(): + all_collections = all_collections + oauth2_get( + "oauth2/user/collections/list").either( + lambda _err: [], lambda colls: colls) + return len(all_collections) diff --git a/wqflask/wqflask/templates/base.html b/wqflask/wqflask/templates/base.html index 38819240..126e1b82 100644 --- a/wqflask/wqflask/templates/base.html +++ b/wqflask/wqflask/templates/base.html @@ -141,7 +141,7 @@ {% if g.user_session %}