aboutsummaryrefslogtreecommitdiff
path: root/wqflask/utility/redis_tools.py
diff options
context:
space:
mode:
Diffstat (limited to 'wqflask/utility/redis_tools.py')
-rw-r--r--wqflask/utility/redis_tools.py90
1 files changed, 90 insertions, 0 deletions
diff --git a/wqflask/utility/redis_tools.py b/wqflask/utility/redis_tools.py
index 0754e16f..ca42f7b7 100644
--- a/wqflask/utility/redis_tools.py
+++ b/wqflask/utility/redis_tools.py
@@ -78,3 +78,93 @@ def check_verification_code(code):
else:
return None
flash("Invalid code: Password reset code does not exist or might have expired!", "error")
+
+def get_user_groups(user_id):
+ #ZS: Get the groups where a user is an admin or a member and return lists corresponding to those two sets of groups
+ admin_group_ids = [] #ZS: Group IDs where user is an admin
+ user_group_ids = [] #ZS: Group IDs where user is a regular user
+ groups_list = Redis.hgetall("groups")
+ for key in groups_list:
+ group_ob = json.loads(groups_list[key])
+ group_admins = set(group_ob['admins'])
+ group_users = set(group_ob['users'])
+ if user_id in group_admins:
+ admin_group_ids.append(group_ob['id'])
+ elif user_id in group_users:
+ user_group_ids.append(group_ob['id'])
+ else:
+ continue
+
+ return admin_group_ids, user_group_ids
+
+def get_group_info(group_id):
+ group_json = Redis.hget("groups", group_id)
+ group_info = None
+ if group_json:
+ group_info = json.loads(group_json)
+
+ return group_info
+
+def create_group(admin_member_ids, user_member_ids = [], group_name = ""):
+ group_id = str(uuid.uuid4())
+ new_group = {
+ "id" : group_id,
+ "admins": admin_member_ids,
+ "users" : user_member_ids,
+ "name" : group_name,
+ "created_timestamp": datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p'),
+ "changed_timestamp": datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p')
+ }
+
+ Redis.hset("groups", group_id, new_group)
+
+ return new_group
+
+def delete_group(user_id, group_id):
+ #ZS: If user is an admin of a group, remove it from the groups hash
+ group_info = get_group_info(group_id)
+ if user_id in group_info["admins"]:
+ Redis.hdel("groups", group_id)
+ return get_user_groups(user_id)
+ else:
+ None
+
+def add_users_to_group(user_id, group_id, user_emails = [], admins = False): #ZS "admins" is just to indicate whether the users should be added to the groups admins or regular users set
+ group_info = get_group_info(group_id)
+ if user_id in group_info["admins"]: #ZS: Just to make sure that the user is an admin for the group, even though they shouldn't be able to reach this point unless they are
+ if admins:
+ group_users = set(group_info["admins"])
+ else:
+ group_users = set(group_info["users"])
+
+ for email in user_emails:
+ user_id = get_user_id("email_address", email)
+ group_users.add(user_id)
+
+ if admins:
+ group_info["admins"] = list(group_users)
+ else:
+ group_info["users"] = list(group_users)
+
+ group_info["changed_timestamp"] = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p')
+ Redis.hset("groups", group_id, json.dumps(group_info))
+ return group_info
+ else:
+ return None
+
+def remove_users_from_group(user_id, users_to_remove_ids, group_id, user_type = "users"): #ZS: User type is because I assume admins can remove other admins
+ group_info = get_group_info(group_id)
+ if user_id in group_info["admins"]:
+ group_users = set(group_info[user_type])
+ group_users -= set(users_to_remove_ids)
+ group_info[user_type] = list(group_users)
+ group_info["changed_timestamp"] = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p')
+ Redis.hset("groups", group_id, json.dumps(group_info))
+
+def change_group_name(user_id, group_id, new_name):
+ group_info = get_group_info(group_id)
+ if user_id in group_info["admins"]:
+ group_info["name"] = new_name
+ return group_info
+ else:
+ return None \ No newline at end of file