From 78ff756b0d4416f8b1fc4c0008d39bd14b5194f3 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 30 Oct 2013 14:52:09 -0500 Subject: Progress on user collections --- wqflask/wqflask/collect.py | 57 ++++++++++++++++++++++---- wqflask/wqflask/model.py | 7 ++++ wqflask/wqflask/templates/collections/add.html | 55 ++++++++++++++++++++----- wqflask/wqflask/user_manager.py | 17 ++++++++ 4 files changed, 117 insertions(+), 19 deletions(-) (limited to 'wqflask') diff --git a/wqflask/wqflask/collect.py b/wqflask/wqflask/collect.py index f61ea3dc..39a63a1f 100644 --- a/wqflask/wqflask/collect.py +++ b/wqflask/wqflask/collect.py @@ -46,16 +46,48 @@ from wqflask import user_manager @app.route("/collections/add") def collections_add(): - return render_template("collections/add.html", traits=request.args['traits']) + user_collections = g.user_session.user_ob.user_collections + print("user_collections are:", user_collections) + return render_template("collections/add.html", + traits=request.args['traits'], + user_collections = user_collections, + ) @app.route("/collections/new") def collections_new(): - uc = model.UserCollection() - uc.name = request.args['new_collection'] - print("user_session:", g.user_session.__dict__) - uc.user = g.user_session.record['user_id'] - unprocessed_traits = request.args['traits'] + print("request.args in collections_new are:", request.args) + if "create_new" in request.args: + return create_new() + elif "add_to_existing" in request.args: + return add_to_existing() + elif "continue" in request.args: + return unnamed() + else: + CauseAnError + + +def unnamed(): + return "unnamed" + +def add_to_existing(): + params = request.args + print("---> params are:", params.keys()) + print(" type(params):", type(params)) + uc = model.UserCollection.query.get(params['existing_collection']) + members = set(json.loads(uc.members)) + + traits = process_traits(params['traits']) + + uc.members = json.dumps(list(members | traits)) + + uc.changed_timestamp = datetime.datetime.utcnow() + + db_session.commit() + + return "added to existing, now set is:" + str(uc.members) + +def process_traits(unprocessed_traits): print("unprocessed_traits are:", unprocessed_traits) unprocessed_traits = unprocessed_traits.split(",") traits = set() @@ -66,6 +98,17 @@ def collections_new(): print("hmac is:", hmac) assert hmac==user_manager.actual_hmac_creation(data), "Data tampering?" traits.add(str(data)) + return traits + +def create_new(): + params = request.args + uc = model.UserCollection() + uc.name = params['new_collection'] + 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) @@ -73,6 +116,4 @@ def collections_new(): db_session.add(uc) db_session.commit() - - return "Created: " + uc.name diff --git a/wqflask/wqflask/model.py b/wqflask/wqflask/model.py index c89dc80a..c1ad0a78 100644 --- a/wqflask/wqflask/model.py +++ b/wqflask/wqflask/model.py @@ -50,6 +50,10 @@ class User(Base): lazy='dynamic' # Necessary for filter in login_count ) + user_collections = relationship("UserCollection", + order_by="asc(UserCollection.name)", + ) + @property def login_count(self): return self.logins.filter_by(successful=True).count() @@ -98,3 +102,6 @@ class UserCollection(Base): created_timestamp = Column(DateTime(), default=lambda: datetime.datetime.utcnow()) changed_timestamp = Column(DateTime(), default=lambda: datetime.datetime.utcnow()) members = Column(Text) # We're going to store them as a json list + + # This index ensures a user doesn't have more than one collection with the same name + __table_args__ = (Index('usercollection_index', "user", "name"), ) diff --git a/wqflask/wqflask/templates/collections/add.html b/wqflask/wqflask/templates/collections/add.html index dfffff3b..8b6a17a3 100644 --- a/wqflask/wqflask/templates/collections/add.html +++ b/wqflask/wqflask/templates/collections/add.html @@ -1,15 +1,48 @@
You have three choices: Contuine without naming the collection, creating a new named collection, + or adding the traits to an existing collection.
+