aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzsloan2016-07-18 11:16:21 -0500
committerGitHub2016-07-18 11:16:21 -0500
commita24f99966b6e0b2d245af6d8085907c68ed7a672 (patch)
treee679ecb02557c6fba0b3ca638f6361fcebbe618d
parenta1000128f0c759d13ff79360922cedab387ca3f8 (diff)
parentf2b638f5fa8644e596349f09ddd79fc0c9e5ec40 (diff)
downloadgenenetwork2-a24f99966b6e0b2d245af6d8085907c68ed7a672.tar.gz
Merge pull request #171 from zsloan/cookie_sessions
Cookie sessions
-rw-r--r--wqflask/wqflask/collect.py186
-rw-r--r--wqflask/wqflask/templates/collections/add.html3
-rw-r--r--wqflask/wqflask/templates/collections/view.html2
-rw-r--r--wqflask/wqflask/user_manager.py25
4 files changed, 150 insertions, 66 deletions
diff --git a/wqflask/wqflask/collect.py b/wqflask/wqflask/collect.py
index 4ea8407c..733a4df0 100644
--- a/wqflask/wqflask/collect.py
+++ b/wqflask/wqflask/collect.py
@@ -54,22 +54,67 @@ 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)
-
- def add_traits(self, params, collection_name):
- assert collection_name == "Default", "Unexpected collection name for anonymous user"
- 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)
- print("currently in redis:", Redis.smembers(self.key))
- len_now = len(Redis.smembers(self.key))
- report_change(len_before, len_now)
+ def __init__(self, collection_name):
+ anon_user = user_manager.AnonUser()
+ 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')
+ 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:
+ collections_dict = json.loads(Redis.get(self.key))
+ traits = collections_dict[str(self.id)]["num_members"]
+ except:
+ return 0
+
+ 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)
+ 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():
+ 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[]')
@@ -83,10 +128,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"""
@@ -164,76 +205,93 @@ 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,
- user_collections = user_collections,
+ traits = traits,
+ collections = user_collections,
)
else:
- return render_template("collections/add_anonymous.html",
- traits=traits
- )
+ anon_collections = list(user_manager.AnonUser().get_collections().keys())
+ return render_template("collections/add.html",
+ traits = traits,
+ collections = anon_collections,
+ )
+ # return render_template("collections/add_anonymous.html",
+ # traits=traits
+ # )
@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']
+
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)
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:
+ #print("PARAMS ADD TO COLLECTION:", params)
+ return AnonCollection().add_traits(params)
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))
+ traits.add (str(data))
return 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)
-
- 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))
+
+ 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()
+ return redirect(url_for('view_collection', uc_id=uc.id))
+ 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.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():
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 +343,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 +354,16 @@ 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:
+ 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)
trait_obs = []
@@ -312,8 +371,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)
@@ -332,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/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 @@
<hr />
-->
-
<input type="hidden" name="traits" value="{{ traits }}" />
<fieldset>
<legend>Create a new named collection</legend>
@@ -33,7 +32,7 @@
<label>Existing collection name</label>
<select name="existing_collection" class="form-control">
- {% for col in user_collections %}
+ {% for col in collections %}
<option value="{{ col.id }}">{{ col.name }}</option>
{% endfor %}
</select>
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 @@
<h2>{{ uc.name }}</h2>
<h3>{{ 'This collection has {}.'.format(numify(trait_obs|count, "record", "records")) }}</h3>
{% else %}
- <h2>Your Collection</h2>
+ <h2> {{ collection_name }}</h2>
<h3>{{ 'This collection has {}.'.format(numify(trait_obs|count, "record", "records")) }}</h3>
{% endif %}
</div>
diff --git a/wqflask/wqflask/user_manager.py b/wqflask/wqflask/user_manager.py
index 10fac06d..c7b3bdb4 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,37 @@ 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)
+ print("THE KEY IS:", self.key)
@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(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_members())
+
+ 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.get(self.key)
+ if collections == "None":
+ return {}
+ else:
+ return json.loads(collections)
+ #print("COLLECTIONS:", collections)
+ #return collections
def verify_cookie(cookie):