diff options
author | BonfaceKilz | 2021-11-08 15:41:00 +0300 |
---|---|---|
committer | BonfaceKilz | 2021-11-11 09:32:26 +0300 |
commit | 42a0bd960095e96b536a51249f2355e82b59fd30 (patch) | |
tree | 6619d7564721f2c4f493e2ccfcbee80af5a664c0 /wqflask | |
parent | f8bdd6cabd97a66bd3e3f8eeff910a0a9a79cf55 (diff) | |
download | genenetwork2-42a0bd960095e96b536a51249f2355e82b59fd30.tar.gz |
group_manager.py: Add methods for creating and deleting groups
Diffstat (limited to 'wqflask')
-rw-r--r-- | wqflask/wqflask/group_manager.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/wqflask/wqflask/group_manager.py b/wqflask/wqflask/group_manager.py index 4edafc66..3434eab9 100644 --- a/wqflask/wqflask/group_manager.py +++ b/wqflask/wqflask/group_manager.py @@ -1,10 +1,16 @@ +import json import redis from flask import current_app from flask import Blueprint from flask import g from flask import render_template +from flask import request +from flask import redirect +from flask import url_for from gn3.authentication import get_groups_by_user_uid +from gn3.authentication import get_user_info_by_key +from gn3.authentication import create_group from wqflask.decorators import login_required group_management = Blueprint("group_management", __name__) @@ -29,3 +35,56 @@ def view_groups(): @login_required def view_create_group_page(): return render_template("admin/create_group.html") + + +@group_management.route("/groups/create", methods=("POST",)) +@login_required +def create_new_group(): + conn = redis.from_url(current_app.config["REDIS_URL"], + decode_responses=True) + if group_name := request.form.get("group_name"): + members_uid, admins_uid = set(), set() + admins_uid.add(user_uid := ( + g.user_session.record.get( + b"user_id", + b"").decode("utf-8") or + g.user_session.record.get("user_id", ""))) + if admin_string := request.form.get("admin_emails_to_add"): + for email in admin_string.split(","): + user_info = get_user_info_by_key(key="email_address", + value=email, + conn=conn) + if user_uid := user_info.get("user_id"): + admins_uid.add(user_uid) + if member_string := request.form.get("member_emails_to_add"): + for email in member_string.split(","): + user_info = get_user_info_by_key(key="email_address", + value=email, + conn=conn) + if user_uid := user_info.get("user_id"): + members_uid.add(user_uid) + + # Create the new group: + create_group(conn=conn, + group_name=group_name, + member_user_uids=list(members_uid), + admin_user_uids=list(admins_uid)) + return redirect(url_for('group_management.view_groups')) + return redirect(url_for('group_management.create_groups')) + + +@group_management.route("/groups/delete", methods=("POST",)) +@login_required +def delete_groups(): + conn = redis.from_url(current_app.config["REDIS_URL"], + decode_responses=True) + user_uid = (g.user_session.record.get(b"user_id", b"").decode("utf-8") or + g.user_session.record.get("user_id", "")) + current_app.logger.info(request.form.get("selected_group_ids")) + for group_uid in request.form.get("selected_group_ids", "").split(":"): + if group_info := conn.hget("groups", group_uid): + group_info = json.loads(group_info) + # A user who is an admin can delete things + if user_uid in group_info.get("admins"): + conn.hdel("groups", group_uid) + return redirect(url_for('group_management.view_groups')) |