From 3d773719255109f3bdcc4adc6314bdafae18ad14 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Fri, 12 May 2023 12:53:24 +0300 Subject: auth: Fetch anonymous collections from separate endpoint. --- wqflask/wqflask/collect.py | 27 +++++++++++-------- wqflask/wqflask/oauth2/client.py | 1 + wqflask/wqflask/templates/collections/list.html | 35 +++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/wqflask/wqflask/collect.py b/wqflask/wqflask/collect.py index 2149dd20..5ff114b2 100644 --- a/wqflask/wqflask/collect.py +++ b/wqflask/wqflask/collect.py @@ -10,6 +10,7 @@ from flask import url_for from flask import request from flask import redirect from flask import flash +from flask import current_app from wqflask import app from utility import hmac @@ -22,8 +23,10 @@ from base.trait import retrieve_trait_info from base.trait import jsonable from base.data_set import create_dataset -from wqflask.oauth2.client import oauth2_get +from wqflask.oauth2 import session from wqflask.oauth2.checks import user_logged_in +from wqflask.oauth2.request_utils import process_error +from wqflask.oauth2.client import oauth2_get, no_token_get Redis = get_redis_conn() @@ -142,20 +145,22 @@ def create_new(collection_name): @app.route("/collections/list") def list_collections(): params = request.args + anon_id = session.session_info()["anon_id"] + anon_collections = no_token_get( + f"oauth2/user/{anon_id}/collections/list").either( + lambda err: {"anon_collections_error": process_error(err)}, + lambda colls: {"anon_collections": colls}) + user_collections = {"collections": []} if user_logged_in(): - return oauth2_get("oauth2/user/collections").either( - lambda err: __error__(process_error(err)), - lambda colls: render_template( - "collections/list.html", params=params, collections=colls)) - - # TODO: Provide GN3 endpoint to fetch collections for anonymous users - # Maybe /oauth2/user//collections/list - user_collections = list(g.user_session.user_collections) + user_collections = oauth2_get("oauth2/user/collections/list").either( + lambda err: {"user_collections_error": process_error(err)}, + lambda colls: {"collections": colls}) + return render_template("collections/list.html", params=params, - collections=user_collections, - ) + **user_collections, + **anon_collections) @app.route("/collections/remove", methods=('POST',)) diff --git a/wqflask/wqflask/oauth2/client.py b/wqflask/wqflask/oauth2/client.py index 249d158d..b070e0bc 100644 --- a/wqflask/wqflask/oauth2/client.py +++ b/wqflask/wqflask/oauth2/client.py @@ -1,4 +1,5 @@ """Common oauth2 client utilities.""" +import json import requests from typing import Any, Optional from urllib.parse import urljoin diff --git a/wqflask/wqflask/templates/collections/list.html b/wqflask/wqflask/templates/collections/list.html index 1a125dbb..7e5c5330 100644 --- a/wqflask/wqflask/templates/collections/list.html +++ b/wqflask/wqflask/templates/collections/list.html @@ -1,4 +1,5 @@ {% extends "base.html" %} +{%from "oauth2/display_error.html" import display_error%} {% block title %}Your Collections{% endblock %} {% block css %} @@ -29,6 +30,40 @@
+ {%if anon_collections_error is defined%} + {{display_error("Anonymous Collections", anon_collections_error)}} + {%endif%} + {%if anon_collections | length > 0%} + + + + + + + + + + + + + + + {% for uc in anon_collections %} + {% if uc.num_members > 0 %} + + + + + + + + {% endif %} + {% endfor %} + +
Anonymous Collections
IndexNameCreatedLast Changed# Records
{{ loop.index }} + {{ uc.name }}{{ uc.created_timestamp }}{{ uc.changed_timestamp }}{{ uc.num_members }}
+ {%endif%} {% if collections|length > 0 %} -- cgit v1.2.3