From d8c2e2a38ad0f7ee504591e93c9988dad95fe452 Mon Sep 17 00:00:00 2001 From: zsloan Date: Wed, 6 Jul 2016 19:04:25 +0000 Subject: Committing current process on enabling multiple collections for users who aren't logged in, but doesn't work yet --- wqflask/wqflask/collect.py | 84 +++++++++++++++----------- wqflask/wqflask/templates/collections/add.html | 3 +- wqflask/wqflask/user_manager.py | 21 +++++++ 3 files changed, 72 insertions(+), 36 deletions(-) (limited to 'wqflask') diff --git a/wqflask/wqflask/collect.py b/wqflask/wqflask/collect.py index 4ea8407c..5ad6b1f4 100644 --- a/wqflask/wqflask/collect.py +++ b/wqflask/wqflask/collect.py @@ -57,16 +57,25 @@ class AnonCollection(object): def __init__(self): self.anon_user = user_manager.AnonUser() self.key = "anon_collection:v5:{}".format(self.anon_user.anon_id) - + self.name = None + + @property + def num_members(self): + try: + return len(Redis.smembers(self.key)) + except: + return 0 + def add_traits(self, params, collection_name): - assert collection_name == "Default", "Unexpected collection name for anonymous user" + #assert collection_name == "Default", "Unexpected collection name for anonymous user" + self.name = collection_name print("params[traits]:", params['traits']) traits = process_traits(params['traits']) print("traits is:", traits) print("self.key is:", self.key) len_before = len(Redis.smembers(self.key)) Redis.sadd(self.key, *list(traits)) - Redis.expire(self.key, 60 * 60 * 24 * 3) + Redis.expire(self.key, 60 * 60 * 24 * 5) print("currently in redis:", Redis.smembers(self.key)) len_now = len(Redis.smembers(self.key)) report_change(len_before, len_now) @@ -165,12 +174,17 @@ def collections_add(): print("user_collections are:", user_collections) return render_template("collections/add.html", traits=traits, - user_collections = user_collections, + collections = user_collections, ) else: - return render_template("collections/add_anonymous.html", - traits=traits - ) + anon_collections = user_manager.AnonUser().get_collections() + return render_template("collections/add.html", + traits=traits, + collections = anon_collections, + ) + # return render_template("collections/add_anonymous.html", + # traits=traits + # ) @app.route("/collections/new") @@ -178,14 +192,14 @@ def collections_new(): params = request.args print("request.args in collections_new are:", params) + collection_name = params['new_collection'] + if "anonymous_add" in params: - AnonCollection().add_traits(params, "Default") + AnonCollection(name=collection_name).add_traits(params, "Default") return redirect(url_for('view_collection')) - elif "sign_in" in params: + if "sign_in" in params: return redirect(url_for('login')) - collection_name = params['new_collection'] - if "create_new" in params: print("in create_new") return create_new(collection_name) @@ -214,26 +228,29 @@ def process_traits(unprocessed_traits): def create_new(collection_name): params = request.args - uc = model.UserCollection() - uc.name = collection_name - print("user_session:", g.user_session.__dict__) - uc.user = g.user_session.user_id + unprocessed_traits = params['traits'] - traits = process_traits(unprocessed_traits) + + if 'uc_id' in params: + uc = model.UserCollection() + uc.name = collection_name + print("user_session:", g.user_session.__dict__) + uc.user = g.user_session.user_id + uc.members = json.dumps(list(traits)) + db_session.add(uc) + db_session.commit() + else: + ac = AnonCollection().add_traits(params, collection_name) + print("traits are:", ac.get_traits()) + user_manager.AnonUser().add_collection(ac) - uc.members = json.dumps(list(traits)) - print("traits are:", traits) - - db_session.add(uc) - db_session.commit() - - print("Created: " + uc.name) return redirect(url_for('view_collection', uc_id=uc.id)) @app.route("/collections/list") def list_collections(): params = request.args + print("PARAMS:", params) try: user_collections = list(g.user_session.user_ob.user_collections) print("user_collections are:", user_collections) @@ -285,7 +302,6 @@ def delete_collection(): # But might want to check ownership in the future collection_name = uc.name db_session.delete(uc) - db_session.commit() flash("We've deletet the collection: {}.".format(collection_name), "alert-info") return redirect(url_for('list_collections')) @@ -297,14 +313,14 @@ def view_collection(): params = request.args print("PARAMS in view collection:", params) - #if "uc_id" in params: - uc_id = params['uc_id'] - uc = model.UserCollection.query.get(uc_id) - traits = json.loads(uc.members) - print("traits are:", traits) - #else: - # traits = AnonCollection().get_traits() - + if "uc_id" in params: + uc_id = params['uc_id'] + uc = model.UserCollection.query.get(uc_id) + traits = json.loads(uc.members) + print("traits are:", traits) + else: + traits = AnonCollection().get_traits() + print("in view_collection traits are:", traits) trait_obs = [] @@ -312,8 +328,8 @@ def view_collection(): for atrait in traits: print("atrait is:", atrait) - name, dataset_name = atrait.split(':') - + name, dataset_name = atrait.split(':') + trait_ob = trait.GeneralTrait(name=name, dataset_name=dataset_name) trait_ob.retrieve_info(get_qtl_info=True) trait_obs.append(trait_ob) diff --git a/wqflask/wqflask/templates/collections/add.html b/wqflask/wqflask/templates/collections/add.html index 07fcba22..f4a69423 100644 --- a/wqflask/wqflask/templates/collections/add.html +++ b/wqflask/wqflask/templates/collections/add.html @@ -16,7 +16,6 @@
--> -
Create a new named collection @@ -33,7 +32,7 @@ diff --git a/wqflask/wqflask/user_manager.py b/wqflask/wqflask/user_manager.py index 10fac06d..7d368e9f 100644 --- a/wqflask/wqflask/user_manager.py +++ b/wqflask/wqflask/user_manager.py @@ -18,6 +18,7 @@ import uuid import hashlib import hmac import base64 +import datetime import urlparse @@ -69,13 +70,33 @@ class AnonUser(object): if self.cookie: logger.debug("already is cookie") self.anon_id = verify_cookie(self.cookie) + else: logger.debug("creating new cookie") self.anon_id, self.cookie = create_signed_cookie() + self.key = "anon_collection:v5:{}".format(self.anon_id) @after.after_this_request def set_cookie(response): response.set_cookie(self.cookie_name, self.cookie) + + def add_collection(self, new_collection): + collection_dict = dict(id = uuid.uuid4(), + name = new_collection.name, + created_timestamp = datetime.datetime.utcnow(), + last_changed_timestamp = datetime.datetime.utcnow(), + num_members = new_collection.num_members, + members = new_collection.get_traits()) + + Redis.sadd(self.key, json.dumps(collection_dict)) + Redis.expire(self.key, 60 * 60 * 24 * 5) + len_now = len(Redis.smembers(self.key)) + print("LENGTH NOW:", len_now) + + def get_collections(self): + collections = Redis.smembers(self.key) + print("COLLECTIONS:", collections) + return collections def verify_cookie(cookie): -- cgit v1.2.3 From f740c18da76a831be799b72ad77e337d1c94478a Mon Sep 17 00:00:00 2001 From: zsloan Date: Mon, 11 Jul 2016 20:37:40 +0000 Subject: Can now create a collection when not logged in (get to the view_collection page), though still need to fix the code that lets you change a collection or list collections --- wqflask/wqflask/collect.py | 107 +++++++++++++++++++++++++++------------- wqflask/wqflask/user_manager.py | 13 +++-- 2 files changed, 80 insertions(+), 40 deletions(-) (limited to 'wqflask') diff --git a/wqflask/wqflask/collect.py b/wqflask/wqflask/collect.py index 5ad6b1f4..d5ed89cd 100644 --- a/wqflask/wqflask/collect.py +++ b/wqflask/wqflask/collect.py @@ -54,31 +54,66 @@ def get_collection(): class AnonCollection(object): """User is not logged in""" - def __init__(self): - self.anon_user = user_manager.AnonUser() - self.key = "anon_collection:v5:{}".format(self.anon_user.anon_id) - self.name = None + def __init__(self, collection_name): + anon_user = user_manager.AnonUser() + self.key = "anon_collection:v1:{}".format(anon_user.anon_id) + self.name = collection_name + self.id = uuid.uuid4() + self.created_timestamp = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p') + self.last_changed_timestamp = self.created_timestamp #ZS: will be updated when changes are made + Redis.set(self.key, None) #ZS: For some reason I get the error "Operation against a key holding the wrong kind of value" if I don't do this + + def get_members(self): + collections_dict = json.loads(Redis.get(self.key)) + traits = collections_dict[str(self.id)].members + #print("traits:", traits) + return traits + @property def num_members(self): try: - return len(Redis.smembers(self.key)) + collections_dict = json.loads(Redis.get(self.key)) + traits = collections_dict[str(self.id)]["num_members"] except: return 0 - def add_traits(self, params, collection_name): + def add_traits(self, params): #assert collection_name == "Default", "Unexpected collection name for anonymous user" - self.name = collection_name - print("params[traits]:", params['traits']) - traits = process_traits(params['traits']) - print("traits is:", traits) + #print("params[traits]:", params['traits']) + self.traits = list(process_traits(params['traits'])) + print("traits is:", self.traits) print("self.key is:", self.key) - len_before = len(Redis.smembers(self.key)) - Redis.sadd(self.key, *list(traits)) - Redis.expire(self.key, 60 * 60 * 24 * 5) - print("currently in redis:", Redis.smembers(self.key)) - len_now = len(Redis.smembers(self.key)) - report_change(len_before, len_now) + #len_before = len(Redis.smembers(self.key)) + existing_collections = Redis.get(self.key) + print("EXISTING COLLECTIONS:", existing_collections) + if existing_collections != "None": + collections_dict = json.loads(existing_collections) + #print("EXISTING COLLECTIONS:", collections_dict) + if self.id in collections_dict.keys(): + collections_dict[str(self.id)]['members'].append(self.traits) + collections_dict[str(self.id)]['last_changed_timestamp'] = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p') + else: + collections_dict[str(self.id)] = {"name" : self.name, + "created_timestamp" : datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p'), + "last_changed_timestamp" : datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p'), + "num_members" : self.num_members, + "members" : self.traits} + else: + new_collection_dict = {"name" : self.name, + "created_timestamp" : datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p'), + "last_changed_timestamp" : datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p'), + "num_members" : self.num_members, + "members" : self.traits} + collections_dict = {str(self.id) : new_collection_dict} + + Redis.set(self.key, json.dumps(collections_dict)) + print("COLLECTIONS_DICT:", Redis.get(self.key)) + #Redis.sadd(self.key, *list(traits)) + #Redis.expire(self.key, 60 * 60 * 24 * 5) + #print("currently in redis:", Redis.smembers(self.key)) + #len_now = len(Redis.smembers(self.key)) + #report_change(len_before, len_now) def remove_traits(self, params): traits_to_remove = params.getlist('traits[]') @@ -92,10 +127,6 @@ class AnonCollection(object): # we can use it to check the results return str(len_now) - def get_traits(self): - traits = Redis.smembers(self.key) - print("traits:", traits) - return traits class UserCollection(object): """User is logged in""" @@ -173,13 +204,13 @@ def collections_add(): user_collections = g.user_session.user_ob.user_collections print("user_collections are:", user_collections) return render_template("collections/add.html", - traits=traits, + traits = traits, collections = user_collections, ) else: anon_collections = user_manager.AnonUser().get_collections() return render_template("collections/add.html", - traits=traits, + traits = traits, collections = anon_collections, ) # return render_template("collections/add_anonymous.html", @@ -205,23 +236,26 @@ def collections_new(): return create_new(collection_name) elif "add_to_existing" in params: print("in add to existing") - return UserCollection().add_traits(params, collection_name) + if g.user_session.logged_in: + return UserCollection().add_traits(params, collection_name) + else: + return AnonCollection().add_traits(params, collection_name) else: print("ELSE") CauseAnError def process_traits(unprocessed_traits): - print("unprocessed_traits are:", unprocessed_traits) + #print("unprocessed_traits are:", unprocessed_traits) if isinstance(unprocessed_traits, basestring): unprocessed_traits = unprocessed_traits.split(",") traits = set() for trait in unprocessed_traits: - print("trait is:", trait) + #print("trait is:", trait) data, _separator, hmac = trait.rpartition(':') data = data.strip() - print("data is:", data) - print("hmac is:", hmac) + #print("data is:", data) + #print("hmac is:", hmac) assert hmac==user_manager.actual_hmac_creation(data), "Data tampering?" traits.add(str(data)) return traits @@ -240,12 +274,17 @@ def create_new(collection_name): uc.members = json.dumps(list(traits)) db_session.add(uc) db_session.commit() + return redirect(url_for('view_collection', uc_id=uc.id)) else: - ac = AnonCollection().add_traits(params, collection_name) - print("traits are:", ac.get_traits()) - user_manager.AnonUser().add_collection(ac) - - return redirect(url_for('view_collection', uc_id=uc.id)) + current_collections = user_manager.AnonUser().get_collections() + ac = AnonCollection(collection_name) + if ac.created_timestamp == None: + datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p') + ac.last_changed_timestamp = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p') + ac.add_traits(params) + #print("traits are:", ac.members) + #user_manager.AnonUser().add_collection(ac) + return redirect(url_for('view_collection', collection_key=ac.key, collection_id=ac.id)) @app.route("/collections/list") def list_collections(): @@ -319,7 +358,9 @@ def view_collection(): traits = json.loads(uc.members) print("traits are:", traits) else: - traits = AnonCollection().get_traits() + user_collections = json.loads(Redis.get(params['collection_key'])) + this_collection = user_collections[params['collection_id']] + traits = this_collection['members'] print("in view_collection traits are:", traits) diff --git a/wqflask/wqflask/user_manager.py b/wqflask/wqflask/user_manager.py index 7d368e9f..f0655ff4 100644 --- a/wqflask/wqflask/user_manager.py +++ b/wqflask/wqflask/user_manager.py @@ -81,20 +81,19 @@ class AnonUser(object): response.set_cookie(self.cookie_name, self.cookie) def add_collection(self, new_collection): - collection_dict = dict(id = uuid.uuid4(), - name = new_collection.name, - created_timestamp = datetime.datetime.utcnow(), - last_changed_timestamp = datetime.datetime.utcnow(), + collection_dict = dict(name = new_collection.name, + created_timestamp = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p'), + last_changed_timestamp = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p'), num_members = new_collection.num_members, - members = new_collection.get_traits()) + members = new_collection.get_members()) - Redis.sadd(self.key, json.dumps(collection_dict)) + Redis.set(self.key, json.dumps(collection_dict)) Redis.expire(self.key, 60 * 60 * 24 * 5) len_now = len(Redis.smembers(self.key)) print("LENGTH NOW:", len_now) def get_collections(self): - collections = Redis.smembers(self.key) + collections = Redis.get(self.key) print("COLLECTIONS:", collections) return collections -- cgit v1.2.3 From f2b638f5fa8644e596349f09ddd79fc0c9e5ec40 Mon Sep 17 00:00:00 2001 From: zsloan Date: Mon, 18 Jul 2016 15:57:49 +0000 Subject: Committing latest changes to anon collection stuff, but still need to fix some issues --- wqflask/wqflask/collect.py | 23 +++++++++++++---------- wqflask/wqflask/templates/collections/view.html | 2 +- wqflask/wqflask/user_manager.py | 9 +++++++-- 3 files changed, 21 insertions(+), 13 deletions(-) (limited to 'wqflask') diff --git a/wqflask/wqflask/collect.py b/wqflask/wqflask/collect.py index d5ed89cd..733a4df0 100644 --- a/wqflask/wqflask/collect.py +++ b/wqflask/wqflask/collect.py @@ -56,7 +56,7 @@ class AnonCollection(object): """User is not logged in""" def __init__(self, collection_name): anon_user = user_manager.AnonUser() - self.key = "anon_collection:v1:{}".format(anon_user.anon_id) + self.key = anon_user.key self.name = collection_name self.id = uuid.uuid4() self.created_timestamp = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p') @@ -82,12 +82,13 @@ class AnonCollection(object): #assert collection_name == "Default", "Unexpected collection name for anonymous user" #print("params[traits]:", params['traits']) self.traits = list(process_traits(params['traits'])) - print("traits is:", self.traits) - print("self.key is:", self.key) + #print("traits is:", self.traits) + #print("self.key is:", self.key) #len_before = len(Redis.smembers(self.key)) existing_collections = Redis.get(self.key) print("EXISTING COLLECTIONS:", existing_collections) if existing_collections != "None": + print("EXISTING COLLECTION NOT NONE") collections_dict = json.loads(existing_collections) #print("EXISTING COLLECTIONS:", collections_dict) if self.id in collections_dict.keys(): @@ -108,7 +109,7 @@ class AnonCollection(object): collections_dict = {str(self.id) : new_collection_dict} Redis.set(self.key, json.dumps(collections_dict)) - print("COLLECTIONS_DICT:", Redis.get(self.key)) + #print("COLLECTIONS_DICT:", Redis.get(self.key)) #Redis.sadd(self.key, *list(traits)) #Redis.expire(self.key, 60 * 60 * 24 * 5) #print("currently in redis:", Redis.smembers(self.key)) @@ -208,7 +209,7 @@ def collections_add(): collections = user_collections, ) else: - anon_collections = user_manager.AnonUser().get_collections() + anon_collections = list(user_manager.AnonUser().get_collections().keys()) return render_template("collections/add.html", traits = traits, collections = anon_collections, @@ -221,7 +222,7 @@ def collections_add(): @app.route("/collections/new") def collections_new(): params = request.args - print("request.args in collections_new are:", params) + #print("request.args in collections_new are:", params) collection_name = params['new_collection'] @@ -239,7 +240,8 @@ def collections_new(): if g.user_session.logged_in: return UserCollection().add_traits(params, collection_name) else: - return AnonCollection().add_traits(params, collection_name) + #print("PARAMS ADD TO COLLECTION:", params) + return AnonCollection().add_traits(params) else: print("ELSE") CauseAnError @@ -257,7 +259,7 @@ def process_traits(unprocessed_traits): #print("data is:", data) #print("hmac is:", hmac) assert hmac==user_manager.actual_hmac_creation(data), "Data tampering?" - traits.add(str(data)) + traits.add (str(data)) return traits def create_new(collection_name): @@ -389,9 +391,10 @@ def view_collection(): if "uc_id" in params: collection_info = dict(trait_obs=trait_obs, - uc = uc) + uc = uc) else: - collection_info = dict(trait_obs=trait_obs) + collection_info = dict(trait_obs=trait_obs, + collection_name=this_collection['name']) if "json" in params: print("json_version:", json_version) return json.dumps(json_version) diff --git a/wqflask/wqflask/templates/collections/view.html b/wqflask/wqflask/templates/collections/view.html index 288207e7..4b1752cd 100644 --- a/wqflask/wqflask/templates/collections/view.html +++ b/wqflask/wqflask/templates/collections/view.html @@ -14,7 +14,7 @@

{{ uc.name }}

{{ 'This collection has {}.'.format(numify(trait_obs|count, "record", "records")) }}

{% else %} -

Your Collection

+

{{ collection_name }}

{{ 'This collection has {}.'.format(numify(trait_obs|count, "record", "records")) }}

{% endif %} diff --git a/wqflask/wqflask/user_manager.py b/wqflask/wqflask/user_manager.py index f0655ff4..c7b3bdb4 100644 --- a/wqflask/wqflask/user_manager.py +++ b/wqflask/wqflask/user_manager.py @@ -75,6 +75,7 @@ class AnonUser(object): logger.debug("creating new cookie") self.anon_id, self.cookie = create_signed_cookie() self.key = "anon_collection:v5:{}".format(self.anon_id) + print("THE KEY IS:", self.key) @after.after_this_request def set_cookie(response): @@ -94,8 +95,12 @@ class AnonUser(object): def get_collections(self): collections = Redis.get(self.key) - print("COLLECTIONS:", collections) - return collections + if collections == "None": + return {} + else: + return json.loads(collections) + #print("COLLECTIONS:", collections) + #return collections def verify_cookie(cookie): -- cgit v1.2.3