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(-) 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(-) 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(-) 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 From ddd4f3dcb0109ccf57ec629c7960cfbc1d2e31d9 Mon Sep 17 00:00:00 2001 From: zsloan Date: Mon, 18 Jul 2016 16:40:30 +0000 Subject: Now correctly displays existing collections in 'add to collection' dropdown menu --- wqflask/wqflask/collect.py | 14 ++++++++------ wqflask/wqflask/user_manager.py | 3 --- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/wqflask/wqflask/collect.py b/wqflask/wqflask/collect.py index 733a4df0..6edbfe92 100644 --- a/wqflask/wqflask/collect.py +++ b/wqflask/wqflask/collect.py @@ -62,12 +62,12 @@ class AnonCollection(object): 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 + if Redis.get(self.key) == "None": + 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 @@ -86,9 +86,7 @@ class AnonCollection(object): #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(): @@ -209,10 +207,14 @@ def collections_add(): collections = user_collections, ) else: - anon_collections = list(user_manager.AnonUser().get_collections().keys()) + anon_collections = user_manager.AnonUser().get_collections() + collection_names = [] + for key in anon_collections.keys(): + this_collection = {'id':key, 'name':anon_collections[key]['name']} + collection_names.append(this_collection) return render_template("collections/add.html", traits = traits, - collections = anon_collections, + collections = collection_names, ) # return render_template("collections/add_anonymous.html", # traits=traits diff --git a/wqflask/wqflask/user_manager.py b/wqflask/wqflask/user_manager.py index c7b3bdb4..e91c7327 100644 --- a/wqflask/wqflask/user_manager.py +++ b/wqflask/wqflask/user_manager.py @@ -99,9 +99,6 @@ class AnonUser(object): return {} else: return json.loads(collections) - #print("COLLECTIONS:", collections) - #return collections - def verify_cookie(cookie): the_uuid, separator, the_signature = cookie.partition(':') -- cgit v1.2.3 From 7a24f2a307229f70ddcee0e4d99df55ded89940c Mon Sep 17 00:00:00 2001 From: zsloan Date: Tue, 19 Jul 2016 19:26:51 +0000 Subject: Now correctly displays number of collections in top menu bar for non-logged in users --- wqflask/wqflask/templates/base.html | 2 ++ wqflask/wqflask/user_manager.py | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/wqflask/wqflask/templates/base.html b/wqflask/wqflask/templates/base.html index 759c4a8d..6af07edc 100644 --- a/wqflask/wqflask/templates/base.html +++ b/wqflask/wqflask/templates/base.html @@ -43,6 +43,8 @@ Collections {% if g.user_session.user_ob %} {{ g.user_session.user_ob.display_num_collections() }} + {% else %} + {{ g.cookie_session.display_num_collections() }} {% endif %} diff --git a/wqflask/wqflask/user_manager.py b/wqflask/wqflask/user_manager.py index e91c7327..31754ff5 100644 --- a/wqflask/wqflask/user_manager.py +++ b/wqflask/wqflask/user_manager.py @@ -99,6 +99,25 @@ class AnonUser(object): return {} else: return json.loads(collections) + + def display_num_collections(self): + """ + Returns the number of collections or a blank string if there are zero. + + Because this is so unimportant...we wrap the whole thing in a try/expect...last thing we + want is a webpage not to be displayed because of an error here + + Importand TODO: use redis to cache this, don't want to be constantly computing it + """ + try: + num = len(self.get_collections().keys()) + if num > 0: + return num + else: + return "" + except Exception as why: + print("Couldn't display_num_collections:", why) + return "" def verify_cookie(cookie): the_uuid, separator, the_signature = cookie.partition(':') @@ -186,7 +205,8 @@ class UserSession(object): @app.before_request def before_request(): g.user_session = UserSession() - + g.cookie_session = AnonUser() + class UsersManager(object): def __init__(self): self.users = model.User.query.all() -- cgit v1.2.3 From 1a4a115c36370ece8bce0f3ab32a5bb4076d0ab7 Mon Sep 17 00:00:00 2001 From: zsloan Date: Wed, 20 Jul 2016 21:28:32 +0000 Subject: Changed anonymous collections to list of dictionaries instead of a dictionary of dictionaries Can now add collections, view list of collections, and view collections from the collection list with anonymous collections --- wqflask/wqflask/collect.py | 134 ++++++++++++------------ wqflask/wqflask/templates/collections/list.html | 17 ++- wqflask/wqflask/user_manager.py | 21 ++-- 3 files changed, 95 insertions(+), 77 deletions(-) diff --git a/wqflask/wqflask/collect.py b/wqflask/wqflask/collect.py index 6edbfe92..004240b4 100644 --- a/wqflask/wqflask/collect.py +++ b/wqflask/wqflask/collect.py @@ -58,65 +58,75 @@ class AnonCollection(object): anon_user = user_manager.AnonUser() self.key = anon_user.key self.name = collection_name - self.id = uuid.uuid4() + self.id = str(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 + self.changed_timestamp = self.created_timestamp #ZS: will be updated when changes are made if Redis.get(self.key) == "None": - 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 - + 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 + traits = [] + collections_list = json.loads(Redis.get(self.key)) + for collection in collections_list: + if collection['id'] == self.id: + traits = collection['members'] return traits @property def num_members(self): - try: - collections_dict = json.loads(Redis.get(self.key)) - traits = collections_dict[str(self.id)]["num_members"] - except: - return 0 + num_members = 0 + collections_list = json.loads(Redis.get(self.key)) + for collection in collections_list: + if collection['id'] == self.id: + num_members = collection['num_members'] + return num_members def add_traits(self, params): #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) #len_before = len(Redis.smembers(self.key)) existing_collections = Redis.get(self.key) - 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') + if existing_collections != None: + collections_list = json.loads(existing_collections) + collection_position = 0 #ZS: Position of collection in collection_list, if it exists + collection_exists = False + for i, collection in enumerate(collections_list): + if collection['id'] == self.id: + collection_position = i + collection_exists = True + break + if collection_exists: + collections_list[collection_position]['members'].extend(self.traits) + collections_list[collection_position]['num_members'] = len(collections_list[collection_position]['members']) + collections_list[collection_position]['changed_timestamp'] = self.last_changed_timestamp 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, + collection_dict = {"id" : self.id, + "name" : self.name, + "created_timestamp" : self.created_timestamp, + "changed_timestamp" : self.changed_timestamp, + "num_members" : len(self.traits), "members" : self.traits} - collections_dict = {str(self.id) : new_collection_dict} + collections_list.append(collection_dict) + else: + collections_list = [] + collection_dict = {"id" : self.id, + "name" : self.name, + "created_timestamp" : self.created_timestamp, + "changed_timestamp" : self.changed_timestamp, + "num_members" : len(self.traits), + "members" : self.traits} + collections_list.append(collection_dict) - Redis.set(self.key, json.dumps(collections_dict)) - #print("COLLECTIONS_DICT:", Redis.get(self.key)) + Redis.set(self.key, json.dumps(collections_list)) #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[]') - print("traits_to_remove:", process_traits(traits_to_remove)) + print("traits_to_remove:", process_txraits(traits_to_remove)) len_before = len(Redis.smembers(self.key)) Redis.srem(self.key, traits_to_remove) print("currently in redis:", Redis.smembers(self.key)) @@ -209,16 +219,13 @@ def collections_add(): else: anon_collections = user_manager.AnonUser().get_collections() collection_names = [] - for key in anon_collections.keys(): - this_collection = {'id':key, 'name':anon_collections[key]['name']} - collection_names.append(this_collection) + for collection in anon_collections: + print("COLLECTION:", collection) + collection_names.append({'id':collection['id'], 'name':collection['name']}) return render_template("collections/add.html", - traits = traits, - collections = collection_names, - ) - # return render_template("collections/add_anonymous.html", - # traits=traits - # ) + traits = traits, + collections = collection_names, + ) @app.route("/collections/new") @@ -258,8 +265,6 @@ def process_traits(unprocessed_traits): #print("trait is:", trait) data, _separator, hmac = trait.rpartition(':') data = data.strip() - #print("data is:", data) - #print("hmac is:", hmac) assert hmac==user_manager.actual_hmac_creation(data), "Data tampering?" traits.add (str(data)) return traits @@ -282,27 +287,27 @@ def create_new(collection_name): else: 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.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)) + return redirect(url_for('view_collection', collection_id=ac.id)) @app.route("/collections/list") def list_collections(): params = request.args print("PARAMS:", params) - try: + if 'uc_id' in params: user_collections = list(g.user_session.user_ob.user_collections) print("user_collections are:", user_collections) return render_template("collections/list.html", params = params, - user_collections = user_collections, + collections = user_collections, ) - except: - return redirect(url_for('view_collection')) + else: + anon_collections = user_manager.AnonUser().get_collections() + print("anon_collections are:", anon_collections) + return render_template("collections/list.html", + params = params, + collections = anon_collections) @app.route("/collections/remove", methods=('POST',)) @@ -362,8 +367,13 @@ def view_collection(): traits = json.loads(uc.members) print("traits are:", traits) else: - user_collections = json.loads(Redis.get(params['collection_key'])) - this_collection = user_collections[params['collection_id']] + user_collections = json.loads(Redis.get(user_manager.AnonUser().key)) + this_collection = {} + for collection in user_collections: + if collection['id'] == params['collection_id']: + this_collection = collection + break + #this_collection = user_collections[params['collection_id']] traits = this_collection['members'] print("in view_collection traits are:", traits) @@ -380,14 +390,6 @@ def view_collection(): trait_obs.append(trait_ob) json_version.append(trait_ob.jsonable()) - #json_version.append(dict(name=trait_ob.name, - # description=trait_ob.description_display, - # location=trait_ob.location_repr, - # mean=trait_ob.mean, - # lrs_score=trait_ob.LRS_score_repr, - # lrs_location=trait_ob.LRS_location_repr)) - # dis=trait_ob.description)) - #json_version.append(trait_ob.__dict__th) print("trait_obs:", trait_obs) diff --git a/wqflask/wqflask/templates/collections/list.html b/wqflask/wqflask/templates/collections/list.html index 0e7612aa..a26e77ef 100644 --- a/wqflask/wqflask/templates/collections/list.html +++ b/wqflask/wqflask/templates/collections/list.html @@ -8,13 +8,22 @@ {% endblock %} {% block content %} + {% if g.user_session.user_ob %} {{ header("Your Collections", - 'You have {}.'.format(numify(user_collections|count, "collection", "collections"))) }} + 'You have {}.'.format(numify(collections|count, "collection", "collections"))) }} + {% else %} + {{ header("Your Collections", + 'You have {}.'.format(numify(collections|count, "collection", "collections"))) }} + {% endif %}
@@ -30,10 +39,14 @@ - {% for uc in user_collections %} + {% for uc in collections %} {{ loop.index }} + {% if g.user_session.user_ob %} {{ uc.name }} + {% else %} + {{ uc.name }} + {% endif %} {{ timeago(uc.created_timestamp.isoformat() + "Z") }} {{ timeago(uc.changed_timestamp.isoformat() + "Z") }} {{ uc.num_members }} diff --git a/wqflask/wqflask/user_manager.py b/wqflask/wqflask/user_manager.py index 31754ff5..7cc0f415 100644 --- a/wqflask/wqflask/user_manager.py +++ b/wqflask/wqflask/user_manager.py @@ -18,7 +18,6 @@ import uuid import hashlib import hmac import base64 -import datetime import urlparse @@ -63,7 +62,7 @@ def timestamp(): class AnonUser(object): - cookie_name = 'anon_user_v1' + cookie_name = 'anon_user_v3' def __init__(self): self.cookie = request.cookies.get(self.cookie_name) @@ -74,7 +73,7 @@ class AnonUser(object): else: logger.debug("creating new cookie") self.anon_id, self.cookie = create_signed_cookie() - self.key = "anon_collection:v5:{}".format(self.anon_id) + self.key = "anon_collection:v1:{}".format(self.anon_id) print("THE KEY IS:", self.key) @after.after_this_request @@ -84,7 +83,7 @@ class AnonUser(object): def add_collection(self, new_collection): 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'), + changed_timestamp = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p'), num_members = new_collection.num_members, members = new_collection.get_members()) @@ -94,11 +93,15 @@ class AnonUser(object): print("LENGTH NOW:", len_now) def get_collections(self): - collections = Redis.get(self.key) - if collections == "None": - return {} + json_collections = Redis.get(self.key) + if json_collections == None: + return [] else: - return json.loads(collections) + collections = json.loads(json_collections) + for collection in collections: + collection['created_timestamp'] = datetime.datetime.strptime(collection['created_timestamp'], '%b %d %Y %I:%M%p') + collection['changed_timestamp'] = datetime.datetime.strptime(collection['changed_timestamp'], '%b %d %Y %I:%M%p') + return collections def display_num_collections(self): """ @@ -110,7 +113,7 @@ class AnonUser(object): Importand TODO: use redis to cache this, don't want to be constantly computing it """ try: - num = len(self.get_collections().keys()) + num = len(self.get_collections()) if num > 0: return num else: -- cgit v1.2.3 From 44e302f3d84b10655b6900ceab6736d24e281f9e Mon Sep 17 00:00:00 2001 From: zsloan Date: Wed, 27 Jul 2016 16:41:42 +0000 Subject: Fixed problem with phenotype traits causing error Can now remove traits from collections and add to existing collections Fixed a few other minor collection bugs --- wqflask/base/data_set.py | 3 +- wqflask/wqflask/collect.py | 71 ++++++++++++++++------ .../static/new/javascript/search_results.js | 35 +++++++---- wqflask/wqflask/templates/collections/view.html | 4 +- wqflask/wqflask/user_manager.py | 14 ++++- 5 files changed, 92 insertions(+), 35 deletions(-) diff --git a/wqflask/base/data_set.py b/wqflask/base/data_set.py index d0ec3f3c..7aed2576 100644 --- a/wqflask/base/data_set.py +++ b/wqflask/base/data_set.py @@ -106,7 +106,8 @@ Publish or ProbeSet. E.g. new_type = "Geno" else: new_type = "ProbeSet" - self.datasets[short_dataset_name] = new_type + self.datasets[short_dataset_name] = new_type + print("DATASETS:", self.datasets) logger.info("datasets",self.datasets) def __call__(self, name): diff --git a/wqflask/wqflask/collect.py b/wqflask/wqflask/collect.py index 004240b4..f1c6ddd5 100644 --- a/wqflask/wqflask/collect.py +++ b/wqflask/wqflask/collect.py @@ -58,12 +58,26 @@ class AnonCollection(object): anon_user = user_manager.AnonUser() self.key = anon_user.key self.name = collection_name - self.id = str(uuid.uuid4()) + self.id = None self.created_timestamp = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p') self.changed_timestamp = self.created_timestamp #ZS: will be updated when changes are made - if Redis.get(self.key) == "None": + #ZS: Find id and set it if the collection doesn't already exist + if Redis.get(self.key) == "None" or Redis.get(self.key) == None: 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 + else: + collections_list = json.loads(Redis.get(self.key)) + collection_position = 0 #ZS: Position of collection in collection_list, if it exists + collection_exists = False + for i, collection in enumerate(collections_list): + if collection['name'] == self.name: + collection_position = i + collection_exists = True + self.id = collection['id'] + break + if self.id == None: + self.id = str(uuid.uuid4()) + def get_members(self): traits = [] @@ -87,7 +101,8 @@ class AnonCollection(object): self.traits = list(process_traits(params['traits'])) #len_before = len(Redis.smembers(self.key)) existing_collections = Redis.get(self.key) - if existing_collections != None: + print("existing_collections:", existing_collections) + if existing_collections != None and existing_collections != "None": collections_list = json.loads(existing_collections) collection_position = 0 #ZS: Position of collection in collection_list, if it exists collection_exists = False @@ -99,7 +114,7 @@ class AnonCollection(object): if collection_exists: collections_list[collection_position]['members'].extend(self.traits) collections_list[collection_position]['num_members'] = len(collections_list[collection_position]['members']) - collections_list[collection_position]['changed_timestamp'] = self.last_changed_timestamp + collections_list[collection_position]['changed_timestamp'] = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p') else: collection_dict = {"id" : self.id, "name" : self.name, @@ -125,12 +140,22 @@ class AnonCollection(object): #report_change(len_before, len_now) def remove_traits(self, params): - traits_to_remove = params.getlist('traits[]') - print("traits_to_remove:", process_txraits(traits_to_remove)) - len_before = len(Redis.smembers(self.key)) - Redis.srem(self.key, traits_to_remove) - print("currently in redis:", Redis.smembers(self.key)) - len_now = len(Redis.smembers(self.key)) + traits_to_remove = [(":").join(trait.split(":")[:2]) for trait in params.getlist('traits[]')] + existing_collections = Redis.get(self.key) + collection_position = 0 + collections_list = json.loads(existing_collections) + for i, collection in enumerate(collections_list): + if collection['id'] == self.id: + collection_position = i + collection_exists = True + break + collections_list[collection_position]['members'] = [trait for trait in collections_list[collection_position]['members'] if trait not in traits_to_remove] + collections_list[collection_position]['num_members'] = len(collections_list[collection_position]['members']) + collections_list[collection_position]['changed_timestamp'] = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p') + len_now = collections_list[collection_position]['num_members'] + #print("before in redis:", json.loads(Redis.get(self.key))) + Redis.set(self.key, json.dumps(collections_list)) + #print("currently in redis:", json.loads(Redis.get(self.key))) # We need to return something so we'll return this...maybe in the future # we can use it to check the results @@ -249,8 +274,9 @@ def collections_new(): if g.user_session.logged_in: return UserCollection().add_traits(params, collection_name) else: - #print("PARAMS ADD TO COLLECTION:", params) - return AnonCollection().add_traits(params) + ac = AnonCollection(collection_name) + ac.add_traits(params) + return redirect(url_for('view_collection', collection_id=ac.id)) else: print("ELSE") CauseAnError @@ -289,6 +315,7 @@ def create_new(collection_name): ac = AnonCollection(collection_name) ac.changed_timestamp = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p') ac.add_traits(params) + print("Collection ID:", ac.id) return redirect(url_for('view_collection', collection_id=ac.id)) @app.route("/collections/list") @@ -332,7 +359,8 @@ def remove_traits(): uc.changed_timestamp = datetime.datetime.utcnow() db_session.commit() else: - members_now = AnonCollection().remove_traits(params) + collection_name = params['collection_name'] + members_now = AnonCollection(collection_name).remove_traits(params) # We need to return something so we'll return this...maybe in the future @@ -344,12 +372,17 @@ def remove_traits(): def delete_collection(): params = request.form print("params:", params) - uc_id = params['uc_id'] - uc = model.UserCollection.query.get(uc_id) - # Todo: For now having the id is good enough since it's so unique - # But might want to check ownership in the future - collection_name = uc.name - db_session.delete(uc) + if "uc_id" in params: + uc_id = params['uc_id'] + uc = model.UserCollection.query.get(uc_id) + # Todo: For now having the id is good enough since it's so unique + # But might want to check ownership in the future + collection_name = uc.name + db_session.delete(uc) + else: + collection_name = params['collection_name'] + user_manager.AnonUser().delete_collection(collection_name) + flash("We've deletet the collection: {}.".format(collection_name), "alert-info") return redirect(url_for('list_collections')) diff --git a/wqflask/wqflask/static/new/javascript/search_results.js b/wqflask/wqflask/static/new/javascript/search_results.js index 4218fdbb..746564fd 100644 --- a/wqflask/wqflask/static/new/javascript/search_results.js +++ b/wqflask/wqflask/static/new/javascript/search_results.js @@ -130,17 +130,30 @@ $(function() { }).get(); console.log("checked length is:", traits.length); console.log("checked is:", traits); - uc_id = $("#uc_id").val(); - console.log("uc.id is:", uc_id); - return $.ajax({ - type: "POST", - url: "/collections/remove", - data: { - uc_id: uc_id, - traits: traits - }, - success: removed_traits - }); + if ( $("#uc_id").length ) { + uc_id = $("#uc_id").val(); + return $.ajax({ + type: "POST", + url: "/collections/remove", + data: { + uc_id: uc_id, + traits: traits + }, + success: removed_traits + }); + } + else { + collection_name = $("#collection_name").val(); + return $.ajax({ + type: "POST", + url: "/collections/remove", + data: { + collection_name: collection_name, + traits: traits + }, + success: removed_traits + }); + } }; $("#select_all").click(select_all); $("#deselect_all").click(deselect_all); diff --git a/wqflask/wqflask/templates/collections/view.html b/wqflask/wqflask/templates/collections/view.html index 4b1752cd..68d2886f 100644 --- a/wqflask/wqflask/templates/collections/view.html +++ b/wqflask/wqflask/templates/collections/view.html @@ -21,16 +21,16 @@
- {% if uc %}
{% if uc %} + {% else %} + {% endif %}
- {% endif %}
{% if uc %} diff --git a/wqflask/wqflask/user_manager.py b/wqflask/wqflask/user_manager.py index 7cc0f415..f1a798d4 100644 --- a/wqflask/wqflask/user_manager.py +++ b/wqflask/wqflask/user_manager.py @@ -62,7 +62,7 @@ def timestamp(): class AnonUser(object): - cookie_name = 'anon_user_v3' + cookie_name = 'anon_user_v7' def __init__(self): self.cookie = request.cookies.get(self.cookie_name) @@ -92,9 +92,19 @@ class AnonUser(object): len_now = len(Redis.smembers(self.key)) print("LENGTH NOW:", len_now) + def delete_collection(self, collection_name): + existing_collections = self.get_collections() + for i, collection in enumerate(existing_collections): + collection['created_timestamp'] = collection['created_timestamp'].strftime('%b %d %Y %I:%M%p') + collection['changed_timestamp'] = collection['changed_timestamp'].strftime('%b %d %Y %I:%M%p') + if collection['name'] == collection_name: + existing_collections.pop(i) + Redis.set(self.key, json.dumps(existing_collections)) + def get_collections(self): json_collections = Redis.get(self.key) - if json_collections == None: + print("json_collections:", json_collections) + if json_collections == None or json_collections == "None": return [] else: collections = json.loads(json_collections) -- cgit v1.2.3 From 801d2099f4d1e5fdf001e17d9897777e10a6afb5 Mon Sep 17 00:00:00 2001 From: zsloan Date: Fri, 29 Jul 2016 17:19:38 +0000 Subject: Fixed a couple issues where you couldn't remove or add traits to user collections --- wqflask/wqflask/collect.py | 20 +++++--------------- wqflask/wqflask/user_manager.py | 23 ++++++++++++++--------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/wqflask/wqflask/collect.py b/wqflask/wqflask/collect.py index f1c6ddd5..fedf6dfc 100644 --- a/wqflask/wqflask/collect.py +++ b/wqflask/wqflask/collect.py @@ -15,35 +15,26 @@ import urlparse import simplejson as json -#from sqlalchemy import orm - -#from redis import StrictRedis import redis Redis = redis.StrictRedis() - from flask import (Flask, g, render_template, url_for, request, make_response, redirect, flash, jsonify) from wqflask import app - from pprint import pformat as pf - from wqflask.database import db_session from wqflask import model +from wqflask import user_manager from utility import Bunch, Struct from utility.formatting import numify -from wqflask import user_manager - - from base import trait - def get_collection(): if g.user_session.logged_in: return UserCollection() @@ -78,7 +69,6 @@ class AnonCollection(object): if self.id == None: self.id = str(uuid.uuid4()) - def get_members(self): traits = [] collections_list = json.loads(Redis.get(self.key)) @@ -301,7 +291,7 @@ def create_new(collection_name): unprocessed_traits = params['traits'] traits = process_traits(unprocessed_traits) - if 'uc_id' in params: + if g.user_session.logged_in: uc = model.UserCollection() uc.name = collection_name print("user_session:", g.user_session.__dict__) @@ -322,7 +312,7 @@ def create_new(collection_name): def list_collections(): params = request.args print("PARAMS:", params) - if 'uc_id' in params: + if g.user_session.logged_in: user_collections = list(g.user_session.user_ob.user_collections) print("user_collections are:", user_collections) return render_template("collections/list.html", @@ -372,7 +362,7 @@ def remove_traits(): def delete_collection(): params = request.form print("params:", params) - if "uc_id" in params: + if g.user_session.logged_in: uc_id = params['uc_id'] uc = model.UserCollection.query.get(uc_id) # Todo: For now having the id is good enough since it's so unique @@ -383,7 +373,7 @@ def delete_collection(): collection_name = params['collection_name'] user_manager.AnonUser().delete_collection(collection_name) - flash("We've deletet the collection: {}.".format(collection_name), "alert-info") + flash("We've deleted the collection: {}.".format(collection_name), "alert-info") return redirect(url_for('list_collections')) diff --git a/wqflask/wqflask/user_manager.py b/wqflask/wqflask/user_manager.py index f1a798d4..4ff3e79c 100644 --- a/wqflask/wqflask/user_manager.py +++ b/wqflask/wqflask/user_manager.py @@ -62,7 +62,7 @@ def timestamp(): class AnonUser(object): - cookie_name = 'anon_user_v7' + cookie_name = 'anon_user_v8' def __init__(self): self.cookie = request.cookies.get(self.cookie_name) @@ -74,7 +74,6 @@ class AnonUser(object): logger.debug("creating new cookie") self.anon_id, self.cookie = create_signed_cookie() self.key = "anon_collection:v1:{}".format(self.anon_id) - print("THE KEY IS:", self.key) @after.after_this_request def set_cookie(response): @@ -89,21 +88,27 @@ class AnonUser(object): 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 delete_collection(self, collection_name): existing_collections = self.get_collections() + updated_collections = [] for i, collection in enumerate(existing_collections): - collection['created_timestamp'] = collection['created_timestamp'].strftime('%b %d %Y %I:%M%p') - collection['changed_timestamp'] = collection['changed_timestamp'].strftime('%b %d %Y %I:%M%p') if collection['name'] == collection_name: - existing_collections.pop(i) - Redis.set(self.key, json.dumps(existing_collections)) + continue + else: + this_collection = {} + this_collection['id'] = collection['id'] + this_collection['name'] = collection['name'] + this_collection['created_timestamp'] = collection['created_timestamp'].strftime('%b %d %Y %I:%M%p') + this_collection['changed_timestamp'] = collection['changed_timestamp'].strftime('%b %d %Y %I:%M%p') + this_collection['num_members'] = collection['num_members'] + this_collection['members'] = collection['members'] + updated_collections.append(this_collection) + + Redis.set(self.key, json.dumps(updated_collections)) def get_collections(self): json_collections = Redis.get(self.key) - print("json_collections:", json_collections) if json_collections == None or json_collections == "None": return [] else: -- cgit v1.2.3