diff options
Diffstat (limited to 'wqflask')
-rw-r--r-- | wqflask/base/trait_collection.py | 79 | ||||
-rw-r--r-- | wqflask/wqflask/collect.py | 116 |
2 files changed, 93 insertions, 102 deletions
diff --git a/wqflask/base/trait_collection.py b/wqflask/base/trait_collection.py index 49b0eaea..d388a3af 100644 --- a/wqflask/base/trait_collection.py +++ b/wqflask/base/trait_collection.py @@ -1,40 +1,39 @@ -#The TraitCollection class with encompass the UserCollection (collections
-#created by registered and logged in users) and AnonCollection (collections created by users who
-#aren't logged in) class. The former will represent and act on a database table while the latter
-#will store its trait information in Redis
-
-
-from __future__ import print_function, division, absolute_import
-
-import uuid
-import datetime
-
-import simplejson as json
-
-from flask import request
-from flask.ext.sqlalchemy import SQLAlchemy
-
-from wqflask import app
-
-import sqlalchemy
-
-from sqlalchemy import (Column, Integer, String, Table, ForeignKey, Unicode, Boolean, DateTime,
- Text, Index)
-from sqlalchemy.orm import relationship, backref
-
-#from redis import StrictRedis
-import redis
-Redis = redis.StrictRedis()
-
-from wqflask.database import Base, init_db
-
class TraitCollection(object):
-
-
-class AnonCollection(TraitCollection):
+ def __init__(self, is_anon=False):
+ self.is_anon = is_anon
+
+
+ @app.route("/collections/remove", methods=('POST',))
+ def remove_traits():
+ if is_anon:
+ AnonCollection.remove_traits()
+ else:
+ UserCollection.remove_traits()
+
+ params = request.form
+ print("params are:", params)
+ uc_id = params['uc_id']
+ uc = model.UserCollection.query.get(uc_id)
+ traits_to_remove = params.getlist('traits[]')
+ print("traits_to_remove are:", traits_to_remove)
+ traits_to_remove = process_traits(traits_to_remove)
+ print("\n\n after processing, traits_to_remove:", traits_to_remove)
+ all_traits = uc.members_as_set()
+ print(" all_traits:", all_traits)
+ members_now = all_traits - traits_to_remove
+ print(" members_now:", members_now)
+ print("Went from {} to {} members in set.".format(len(all_traits), len(members_now)))
+ uc.members = json.dumps(list(members_now))
+ uc.changed_timestamp = datetime.datetime.utcnow()
+ db_session.commit()
+
+ # We need to return something so we'll return this...maybe in the future
+ # we can use it to check the results
+ return str(len(members_now))
+
def __init__(self, anon_id)
- self.anon_id = anon_id
+ self.anon_key = anon_key
self.collection_members = Redis.smembers(self.anon_id)
print("self.collection_members is:", self.collection_members)
self.num_members = len(self.collection_members)
@@ -52,15 +51,3 @@ class AnonCollection(TraitCollection): # We need to return something so we'll return this...maybe in the future
# we can use it to check the results
return str(len(members_now))
-
- @property
- def num_members(self):
- print("members are:", json.loads(self.members))
- return len(json.loads(self.members))
-
- #@property
- #def display_num_members(self):
- # return display_collapsible(self.num_members)
-
- def members_as_set(self):
- return set(json.loads(self.members))
\ No newline at end of file diff --git a/wqflask/wqflask/collect.py b/wqflask/wqflask/collect.py index daa3b2af..34def295 100644 --- a/wqflask/wqflask/collect.py +++ b/wqflask/wqflask/collect.py @@ -44,6 +44,65 @@ from wqflask import user_manager from base import trait +class AnonCollection(object): + """User is not logged in""" + def __init__(self): + self.anon_user = user_manager.AnonUser() + self.key = "anon_collection:v1:{}".format(self.anon_user.anon_id) + + + def add_traits(params, collection_name): + assert collection_name == "Default", "Unexpected collection name for anonymous user" + traits = process_traits(params['traits']) + len_before = len(Redis.smembers) + Redis.sadd(self.key, traits) + Redis.expire(self.key, 60 * 60 * 24 * 3) + len_now = len(Redis.smembers) + report_change(len_before, len_now) + + + +class UserCollection(object): + """User is logged in""" + + def add_traits(params, collection_name): + print("---> params are:", params.keys()) + print(" type(params):", type(params)) + if collection_name=="Default": + uc = g.user_session.user_ob.get_collection_by_name("Default") + # Doesn't exist so we'll create it + if not uc: + return create_new("Default") + else: + uc = model.UserCollection.query.get(params['existing_collection']) + members = uc.members_as_set() #set(json.loads(uc.members)) + len_before = len(members) + + traits = process_traits(params['traits']) + + members_now = list(members | traits) + len_now = len(members_now) + uc.members = json.dumps(members_now) + + uc.changed_timestamp = datetime.datetime.utcnow() + + db_session.commit() + + print("added to existing, now set is:" + str(uc.members)) + report_change(len_before, len_now) + + # Probably have to change that + return redirect(url_for('view_collection', uc_id=uc.id)) + +def report_change(len_before, len_now): + new_length = len_now - len_before + if new_length: + flash("We've added {} to your collection.".format( + numify(new_length, 'new trait', 'new traits'))) + else: + flash("No new traits were added.") + + @app.route("/collections/add") @@ -84,62 +143,7 @@ def collections_new(): CauseAnError -def add_anon_traits(params): - # Todo: assert user isn't logged in - anon_id = user_manager.AnonUser().anon_id - print("anon_id is:", anon_id) - - key = "anon_collection:v1:" + anon_id - - len_before = len(Redis.smembers(key)) - traits = process_traits(params['traits']) - - print("redis key is:", key) - Redis.expire(key, 60*60*72) - for trait in traits: - Redis.sadd(key, trait) - #print("set members:", Redis.smembers(key)) - - len_now = len(Redis.smembers(key)) - new_length = len_now - len_before - print("new_length:", new_length) - #print("anon traits:", traits) - - return redirect(url_for('view_collection', key=key)) - -def add_traits(params, collection_name): - print("---> params are:", params.keys()) - print(" type(params):", type(params)) - if collection_name=="Default": - uc = g.user_session.user_ob.get_collection_by_name("Default") - # Doesn't exist so we'll create it - if not uc: - return create_new("Default") - else: - uc = model.UserCollection.query.get(params['existing_collection']) - members = uc.members_as_set() #set(json.loads(uc.members)) - len_before = len(members) - - traits = process_traits(params['traits']) - - members_now = list(members | traits) - len_now = len(members_now) - uc.members = json.dumps(members_now) - - uc.changed_timestamp = datetime.datetime.utcnow() - - db_session.commit() - print("added to existing, now set is:" + str(uc.members)) - - new_length = len_now - len_before - if new_length: - flash("We've added {} to your collection.".format( - numify(new_length, 'new trait', 'new traits'))) - else: - flash("No new traits were added.") - - return redirect(url_for('view_collection', uc_id=uc.id)) def process_traits(unprocessed_traits): print("unprocessed_traits are:", unprocessed_traits) @@ -188,6 +192,7 @@ def list_collections(): @app.route("/collections/remove", methods=('POST',)) def remove_traits(): + params = request.form print("params are:", params) uc_id = params['uc_id'] @@ -210,7 +215,6 @@ def remove_traits(): return str(len(members_now)) - @app.route("/collections/delete", methods=('POST',)) def delete_collection(): params = request.form |