From b1041332ef0437031839d4e6ad64f4c7b797d6f8 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Wed, 17 May 2023 10:51:15 +0300 Subject: auth: Import/Delete Anonymous Collections Integrate import/delete of anonymous collection when user is authenticated. --- wqflask/wqflask/collect.py | 23 ++++++++++++++- wqflask/wqflask/oauth2/toplevel.py | 2 +- wqflask/wqflask/templates/collections/list.html | 3 +- wqflask/wqflask/templates/index_page.html | 37 ++++++++++++++++++++++++- wqflask/wqflask/views.py | 21 +++++++++----- 5 files changed, 75 insertions(+), 11 deletions(-) (limited to 'wqflask') diff --git a/wqflask/wqflask/collect.py b/wqflask/wqflask/collect.py index b64270e0..ad0820c2 100644 --- a/wqflask/wqflask/collect.py +++ b/wqflask/wqflask/collect.py @@ -27,7 +27,8 @@ from wqflask.oauth2 import session from wqflask.oauth2.session import session_info 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, no_token_post +from wqflask.oauth2.client import ( + oauth2_get, oauth2_post, no_token_get, no_token_post) Redis = get_redis_conn() @@ -188,6 +189,26 @@ def list_collections(): **user_collections, **anon_collections) +@app.route("/collections/handle_anonymous", methods=["POST"]) +def handle_anonymous_collections(): + """Handle any anonymous collection on logging in.""" + choice = request.form.get("anon_choice") + if choice not in ("import", "delete"): + flash("Invalid choice!", "alert-danger") + return redirect("/") + def __impdel_error__(err): + error = process_error(err) + flash(f"{error['error']}: {error['error_description']}", + "alert-danger") + return redirect("/") + def __impdel_success__(msg): + flash(f"Success: {msg['message']}", "alert-success") + return redirect("/") + return oauth2_post( + f"oauth2/user/collections/anonymous/{choice}", + json={ + "anon_id": str(session_info()["anon_id"]) + }).either(__impdel_error__, __impdel_success__) @app.route("/collections/remove", methods=('POST',)) def remove_traits(): diff --git a/wqflask/wqflask/oauth2/toplevel.py b/wqflask/wqflask/oauth2/toplevel.py index ef9ce3db..04a08870 100644 --- a/wqflask/wqflask/oauth2/toplevel.py +++ b/wqflask/wqflask/oauth2/toplevel.py @@ -35,7 +35,7 @@ def authorisation_code(): "token": session.user_token(), "logged_in": True }) - return redirect(url_for("oauth2.user.user_profile")) + return redirect("/") code = request.args.get("code", "") if bool(code): diff --git a/wqflask/wqflask/templates/collections/list.html b/wqflask/wqflask/templates/collections/list.html index 7e5c5330..437b0cc3 100644 --- a/wqflask/wqflask/templates/collections/list.html +++ b/wqflask/wqflask/templates/collections/list.html @@ -65,7 +65,8 @@ {%endif%} {% if collections|length > 0 %} - +
+ diff --git a/wqflask/wqflask/templates/index_page.html b/wqflask/wqflask/templates/index_page.html index aad5d109..12503326 100755 --- a/wqflask/wqflask/templates/index_page.html +++ b/wqflask/wqflask/templates/index_page.html @@ -54,7 +54,42 @@
- {{ flash_me() }} + {{ flash_me() }} + + {%if anon_collections | length > 0%} +
+
+

Import Anonymous Collections

+
+
+

+ There {%if anon_collections | length > 1%}are{%else%}is{%endif%} + {{anon_collections | length}} anonymous + collection{%if anon_collections | length > 1%}s{%endif%} + associated with your current session. What do you wish to do? +

+

+ + If you choose to ignore this, the anonymous collections will be + eventually deleted and lost. + +

+
+
+ + + + +
+ + + +
+
+ {%endif%}
diff --git a/wqflask/wqflask/views.py b/wqflask/wqflask/views.py index f14ccdd0..164cf9ce 100644 --- a/wqflask/wqflask/views.py +++ b/wqflask/wqflask/views.py @@ -101,6 +101,8 @@ from wqflask.database import database_connection import jobs.jobs as jobs +from wqflask.oauth2.session import session_info +from wqflask.oauth2.checks import user_logged_in Redis = get_redis_conn() @@ -147,13 +149,18 @@ def no_access_page(): @app.route("/") def index_page(): - params = request.args - if 'import_collections' in params: - import_collections = params['import_collections'] - if import_collections == "true": - g.user_session.import_traits_to_user(params['anon_id']) - return render_template( - "index_page.html", version=GN_VERSION, gn_server_url=GN_SERVER_URL) + anon_id = session_info()["anon_id"] + def __render__(colls): + return render_template("index_page.html", version=GN_VERSION, + gn_server_url=GN_SERVER_URL, + anon_collections=( + colls if user_logged_in() else []), + anon_id=anon_id) + + return no_token_get( + f"oauth2/user/collections/{anon_id}/list").either( + lambda err: __render__([]), + __render__) @app.route("/tmp/") -- cgit v1.2.3
User Collections