about summary refs log tree commit diff
path: root/wqflask/utility
diff options
context:
space:
mode:
authorzsloan2020-10-19 13:43:32 -0500
committerGitHub2020-10-19 13:43:32 -0500
commit8eaae7296e2d66726975ddba0de8aecae256e63b (patch)
tree5505b932195a454c1ada10677f0e76faba6b588f /wqflask/utility
parent5fdb3b83566516782542d04b92a5be97f41c2330 (diff)
parent3f242af74af814d9344e1e80c5f94914c6d9b621 (diff)
downloadgenenetwork2-8eaae7296e2d66726975ddba0de8aecae256e63b.tar.gz
Merge branch 'testing' into scroller_testing
Diffstat (limited to 'wqflask/utility')
-rw-r--r--wqflask/utility/authentication_tools.py41
-rw-r--r--wqflask/utility/hmac.py9
-rw-r--r--wqflask/utility/redis_tools.py120
3 files changed, 114 insertions, 56 deletions
diff --git a/wqflask/utility/authentication_tools.py b/wqflask/utility/authentication_tools.py
index 3553b92b..239b08e3 100644
--- a/wqflask/utility/authentication_tools.py
+++ b/wqflask/utility/authentication_tools.py
@@ -1,4 +1,6 @@
 from __future__ import absolute_import, print_function, division
+import logging
+from flask import Flask, g, redirect, url_for
 
 import json
 import requests
@@ -9,33 +11,31 @@ from utility import hmac
 from utility.redis_tools import get_redis_conn, get_resource_info, get_resource_id, add_resource
 Redis = get_redis_conn()
 
-from flask import Flask, g, redirect, url_for
 
-import logging
-logger = logging.getLogger(__name__ )
+logger = logging.getLogger(__name__)
+
 
 def check_resource_availability(dataset, trait_id=None):
 
-    #At least for now assume temporary entered traits are accessible
-    if type(dataset) == str:
-        return webqtlConfig.DEFAULT_PRIVILEGES
-    if dataset.type == "Temp":
+    # At least for now assume temporary entered traits are accessible
+    if type(dataset) == str or dataset.type == "Temp":
         return webqtlConfig.DEFAULT_PRIVILEGES
 
     resource_id = get_resource_id(dataset, trait_id)
 
-    if resource_id: #ZS: This should never be false, but it's technically possible if a non-Temp dataset somehow had a type other than Publish/ProbeSet/Geno
+    if resource_id:  # ZS: This should never be false, but it's technically possible if a non-Temp dataset somehow had a type other than Publish/ProbeSet/Geno
         resource_info = get_resource_info(resource_id)
-        if not resource_info: #ZS: If resource isn't already in redis, add it with default privileges
+        if not resource_info:  # ZS: If resource isn't already in redis, add it with default privileges
             resource_info = add_new_resource(dataset, trait_id)
 
-    #ZS: Check if super-user - we should probably come up with some way to integrate this into the proxy
+    # ZS: Check if super-user - we should probably come up with some way to integrate this into the proxy
     if g.user_session.user_id in Redis.smembers("super_users"):
-       return webqtlConfig.SUPER_PRIVILEGES
+        return webqtlConfig.SUPER_PRIVILEGES
 
     response = None
 
-    the_url = "http://localhost:8080/available?resource={}&user={}".format(resource_id, g.user_session.user_id)
+    the_url = "http://localhost:8080/available?resource={}&user={}".format(
+        resource_id, g.user_session.user_id)
     try:
         response = json.loads(requests.get(the_url).content)
     except:
@@ -43,18 +43,19 @@ def check_resource_availability(dataset, trait_id=None):
 
     return response
 
+
 def add_new_resource(dataset, trait_id=None):
     resource_ob = {
-        'owner_id'    : "none", # webqtlConfig.DEFAULT_OWNER_ID,
+        'owner_id': "none",  # webqtlConfig.DEFAULT_OWNER_ID,
         'default_mask': webqtlConfig.DEFAULT_PRIVILEGES,
-        'group_masks' : {}
+        'group_masks': {}
     }
 
     if dataset.type == "Publish":
         resource_ob['name'] = get_group_code(dataset) + "_" + str(trait_id)
         resource_ob['data'] = {
             'dataset': dataset.id,
-            'trait'  : trait_id
+            'trait': trait_id
         }
         resource_ob['type'] = 'dataset-publish'
     elif dataset.type == "Geno":
@@ -74,15 +75,19 @@ def add_new_resource(dataset, trait_id=None):
 
     return resource_info
 
+
 def get_group_code(dataset):
-    results = g.db.execute("SELECT InbredSetCode from InbredSet where Name='{}'".format(dataset.group.name)).fetchone()
+    results = g.db.execute("SELECT InbredSetCode from InbredSet where Name='{}'".format(
+        dataset.group.name)).fetchone()
     if results[0]:
         return results[0]
     else:
         return ""
 
+
 def check_admin(resource_id=None):
-    the_url = "http://localhost:8080/available?resource={}&user={}".format(resource_id, g.user_session.user_id)
+    the_url = "http://localhost:8080/available?resource={}&user={}".format(
+        resource_id, g.user_session.user_id)
     try:
         response = json.loads(requests.get(the_url).content)['admin']
     except:
@@ -96,6 +101,7 @@ def check_admin(resource_id=None):
     else:
         return "not-admin"
 
+
 def check_owner(dataset=None, trait_id=None, resource_id=None):
     if resource_id:
         resource_info = get_resource_info(resource_id)
@@ -110,6 +116,7 @@ def check_owner(dataset=None, trait_id=None, resource_id=None):
 
     return False
 
+
 def check_owner_or_admin(dataset=None, trait_id=None, resource_id=None):
     if not resource_id:
         if dataset.type == "Temp":
diff --git a/wqflask/utility/hmac.py b/wqflask/utility/hmac.py
index b08be97e..fd75803e 100644
--- a/wqflask/utility/hmac.py
+++ b/wqflask/utility/hmac.py
@@ -7,11 +7,11 @@ from flask import url_for
 
 from wqflask import app
 
+
 def hmac_creation(stringy):
     """Helper function to create the actual hmac"""
 
     secret = app.config['SECRET_HMAC_CODE']
-
     hmaced = hmac.new(secret, stringy, hashlib.sha1)
     hm = hmaced.hexdigest()
     # ZS: Leaving the below comment here to ask Pjotr about
@@ -20,10 +20,12 @@ def hmac_creation(stringy):
     hm = hm[:20]
     return hm
 
+
 def data_hmac(stringy):
-    """Takes arbitray data string and appends :hmac so we know data hasn't been tampered with"""
+    """Takes arbitrary data string and appends :hmac so we know data hasn't been tampered with"""
     return stringy + ":" + hmac_creation(stringy)
 
+
 def url_for_hmac(endpoint, **values):
     """Like url_for but adds an hmac at the end to insure the url hasn't been tampered with"""
 
@@ -36,5 +38,6 @@ def url_for_hmac(endpoint, **values):
         combiner = "?"
     return url + combiner + "hm=" + hm
 
+
 app.jinja_env.globals.update(url_for_hmac=url_for_hmac,
-                             data_hmac=data_hmac)
\ No newline at end of file
+                             data_hmac=data_hmac)
diff --git a/wqflask/utility/redis_tools.py b/wqflask/utility/redis_tools.py
index 81ba04ea..ef02268e 100644
--- a/wqflask/utility/redis_tools.py
+++ b/wqflask/utility/redis_tools.py
@@ -4,23 +4,21 @@ import uuid
 import simplejson as json
 import datetime
 
-import redis # used for collections
-
-import logging
-
-from flask import (render_template, flash)
-
-from utility import hmac
+import redis  # used for collections
 
+from utility.hmac import hmac_creation
 from utility.logger import getLogger
 logger = getLogger(__name__)
 
+
 def get_redis_conn():
     Redis = redis.StrictRedis(port=6379)
     return Redis
 
+
 Redis = get_redis_conn()
 
+
 def is_redis_available():
     try:
         Redis.ping()
@@ -28,6 +26,7 @@ def is_redis_available():
         return False
     return True
 
+
 def get_user_id(column_name, column_value):
     user_list = Redis.hgetall("users")
     key_list = []
@@ -38,6 +37,7 @@ def get_user_id(column_name, column_value):
 
     return None
 
+
 def get_user_by_unique_column(column_name, column_value):
     item_details = None
 
@@ -52,9 +52,11 @@ def get_user_by_unique_column(column_name, column_value):
 
     return item_details
 
+
 def get_users_like_unique_column(column_name, column_value):
-    """
-    Like previous function, but this only checks if the input is a subset of a field and can return multiple results
+    """Like previous function, but this only checks if the input is a
+    subset of a field and can return multiple results
+
     """
     matched_users = []
 
@@ -74,7 +76,6 @@ def get_users_like_unique_column(column_name, column_value):
 
     return matched_users
 
-# def search_users_by_unique_column(column_name, column_value):
 
 def set_user_attribute(user_id, column_name, column_value):
     user_info = json.loads(Redis.hget("users", user_id))
@@ -82,6 +83,7 @@ def set_user_attribute(user_id, column_name, column_value):
 
     Redis.hset("users", user_id, json.dumps(user_info))
 
+
 def get_user_collections(user_id):
     collections = None
     collections = Redis.hget("collections", user_id)
@@ -91,22 +93,27 @@ def get_user_collections(user_id):
     else:
         return []
 
+
 def save_user(user, user_id):
     Redis.hset("users", user_id, json.dumps(user))
 
+
 def save_collections(user_id, collections_ob):
     Redis.hset("collections", user_id, collections_ob)
 
+
 def save_verification_code(user_email, code):
     Redis.hset("verification_codes", code, user_email)
 
+
 def check_verification_code(code):
     email_address = None
     user_details = None
     email_address = Redis.hget("verification_codes", code)
 
     if email_address:
-        user_details = get_user_by_unique_column('email_address', email_address)
+        user_details = get_user_by_unique_column(
+            'email_address', email_address)
         if user_details:
             return user_details
         else:
@@ -114,10 +121,12 @@ def check_verification_code(code):
     else:
         return None
 
+
 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
+    # 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:
         try:
@@ -142,6 +151,7 @@ def get_user_groups(user_id):
 
     return admin_groups, user_groups
 
+
 def get_group_info(group_id):
     group_json = Redis.hget("groups", group_id)
     group_info = None
@@ -150,6 +160,7 @@ def get_group_info(group_id):
 
     return group_info
 
+
 def get_group_by_unique_column(column_name, column_value):
     """ Get group by column; not sure if there's a faster way to do this """
 
@@ -158,7 +169,8 @@ def get_group_by_unique_column(column_name, column_value):
     all_group_list = Redis.hgetall("groups")
     for key in all_group_list:
         group_info = json.loads(all_group_list[key])
-        if column_name == "admins" or column_name == "members": #ZS: Since these fields are lists, search in the list
+        # ZS: Since these fields are lists, search in the list
+        if column_name == "admins" or column_name == "members":
             if column_value in group_info[column_name]:
                 matched_groups.append(group_info)
         else:
@@ -167,9 +179,11 @@ def get_group_by_unique_column(column_name, column_value):
 
     return matched_groups
 
+
 def get_groups_like_unique_column(column_name, column_value):
-    """
-    Like previous function, but this only checks if the input is a subset of a field and can return multiple results
+    """Like previous function, but this only checks if the input is a
+    subset of a field and can return multiple results
+
     """
     matched_groups = []
 
@@ -178,7 +192,8 @@ def get_groups_like_unique_column(column_name, column_value):
         if column_name != "group_id":
             for key in group_list:
                 group_info = json.loads(group_list[key])
-                if column_name == "admins" or column_name == "members": #ZS: Since these fields are lists, search in the list
+                # ZS: Since these fields are lists, search in the list
+                if column_name == "admins" or column_name == "members":
                     if column_value in group_info[column_name]:
                         matched_groups.append(group_info)
                 else:
@@ -190,13 +205,15 @@ def get_groups_like_unique_column(column_name, column_value):
 
     return matched_groups
 
-def create_group(admin_user_ids, member_user_ids = [], group_name = "Default Group Name"):
+
+def create_group(admin_user_ids, member_user_ids=[],
+                 group_name="Default Group Name"):
     group_id = str(uuid.uuid4())
     new_group = {
-        "id"    : group_id,
+        "id": group_id,
         "admins": admin_user_ids,
-        "members" : member_user_ids,
-        "name"  : group_name,
+        "members": member_user_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')
     }
@@ -205,8 +222,9 @@ def create_group(admin_user_ids, member_user_ids = [], group_name = "Default Gro
 
     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
+    # 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)
@@ -214,9 +232,15 @@ def delete_group(user_id, group_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
+
+# ZS "admins" is just to indicate whether the users should be added to
+# the groups admins or regular users set
+def add_users_to_group(user_id, group_id, user_emails=[], admins=False):
     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
+    # 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 user_id in group_info["admins"]:
         if admins:
             group_users = set(group_info["admins"])
         else:
@@ -231,25 +255,36 @@ def add_users_to_group(user_id, group_id, user_emails = [], admins = False): #ZS
         else:
             group_info["members"] = list(group_users)
 
-        group_info["changed_timestamp"] = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p')
+        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 = "members"): #ZS: User type is because I assume admins can remove other admins
+
+# ZS: User type is because I assume admins can remove other admins
+def remove_users_from_group(user_id,
+                            users_to_remove_ids,
+                            group_id,
+                            user_type="members"):
     group_info = get_group_info(group_id)
 
     if user_id in group_info["admins"]:
         users_to_remove_set = set(users_to_remove_ids)
-        if user_type == "admins" and user_id in users_to_remove_set: #ZS: Make sure an admin can't remove themselves from a group, since I imagine we don't want groups to be able to become admin-less
+        # ZS: Make sure an admin can't remove themselves from a group,
+        # since I imagine we don't want groups to be able to become
+        # admin-less
+        if user_type == "admins" and user_id in users_to_remove_set:
             users_to_remove_set.remove(user_id)
         group_users = set(group_info[user_type])
         group_users -= users_to_remove_set
         group_info[user_type] = list(group_users)
-        group_info["changed_timestamp"] = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p')
+        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"]:
@@ -259,22 +294,28 @@ def change_group_name(user_id, group_id, new_name):
     else:
         return None
 
+
 def get_resources():
     resource_list = Redis.hgetall("resources")
     return resource_list
 
+
 def get_resource_id(dataset, trait_id=None):
     resource_id = False
     if dataset.type == "Publish":
         if trait_id:
-            resource_id = hmac.hmac_creation("{}:{}:{}".format('dataset-publish', dataset.id, trait_id))
+            resource_id = hmac_creation("{}:{}:{}".format(
+                'dataset-publish', dataset.id, trait_id))
     elif dataset.type == "ProbeSet":
-        resource_id = hmac.hmac_creation("{}:{}".format('dataset-probeset', dataset.id))
+        resource_id = hmac_creation(
+            "{}:{}".format('dataset-probeset', dataset.id))
     elif dataset.type == "Geno":
-        resource_id = hmac.hmac_creation("{}:{}".format('dataset-geno', dataset.id))
+        resource_id = hmac_creation(
+            "{}:{}".format('dataset-geno', dataset.id))
 
     return resource_id
 
+
 def get_resource_info(resource_id):
     resource_info = Redis.hget("resources", resource_id)
     if resource_info:
@@ -282,17 +323,23 @@ def get_resource_info(resource_id):
     else:
         return None
 
+
 def add_resource(resource_info, update=True):
     if 'trait' in resource_info['data']:
-        resource_id = hmac.hmac_creation('{}:{}:{}'.format(str(resource_info['type']), str(resource_info['data']['dataset']), str(resource_info['data']['trait'])))
+        resource_id = hmac_creation('{}:{}:{}'.format(
+            str(resource_info['type']), str(
+                resource_info['data']['dataset']),
+            str(resource_info['data']['trait'])))
     else:
-        resource_id = hmac.hmac_creation('{}:{}'.format(str(resource_info['type']), str(resource_info['data']['dataset'])))
+        resource_id = hmac_creation('{}:{}'.format(
+            str(resource_info['type']), str(resource_info['data']['dataset'])))
 
     if update or not Redis.hexists("resources", resource_id):
         Redis.hset("resources", resource_id, json.dumps(resource_info))
 
     return resource_info
 
+
 def add_access_mask(resource_id, group_id, access_mask):
     the_resource = get_resource_info(resource_id)
     the_resource['group_masks'][group_id] = access_mask
@@ -301,9 +348,10 @@ def add_access_mask(resource_id, group_id, access_mask):
 
     return the_resource
 
+
 def change_resource_owner(resource_id, new_owner_id):
-    the_resource= get_resource_info(resource_id)
+    the_resource = get_resource_info(resource_id)
     the_resource['owner_id'] = new_owner_id
 
     Redis.delete("resource")
-    Redis.hset("resources", resource_id, json.dumps(the_resource))
\ No newline at end of file
+    Redis.hset("resources", resource_id, json.dumps(the_resource))