From 957ec58b605af421947335197ed09eddf72479a0 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 30 Dec 2013 03:45:27 +0000 Subject: Collections changes --- wqflask/wqflask/collect.py | 36 +++++++++++++++---------- wqflask/wqflask/model.py | 8 ++++++ wqflask/wqflask/templates/collections/add.html | 10 +++---- wqflask/wqflask/templates/collections/view.html | 1 + 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/wqflask/wqflask/collect.py b/wqflask/wqflask/collect.py index 6ac4abcf..2bf5b602 100644 --- a/wqflask/wqflask/collect.py +++ b/wqflask/wqflask/collect.py @@ -58,25 +58,33 @@ def collections_add(): @app.route("/collections/new") def collections_new(): - 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() + params = request.args + print("request.args in collections_new are:", params) + + collection_name = params['new_collection'] + + if "create_new" in params: + return create_new(collection_name) + elif "add_to_existing" in params: + return add_traits(params, collection_name) + elif "default" in params: + return add_traits(params, "default") + else: CauseAnError -def unnamed(): - return "unnamed" -def add_to_existing(): - params = request.args +def add_traits(params, collection_name): print("---> params are:", params.keys()) print(" type(params):", type(params)) - uc = model.UserCollection.query.get(params['existing_collection']) + 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 = set(json.loads(uc.members)) len_before = len(members) @@ -114,10 +122,10 @@ def process_traits(unprocessed_traits): traits.add(str(data)) return traits -def create_new(): +def create_new(collection_name): params = request.args uc = model.UserCollection() - uc.name = params['new_collection'] + uc.name = collection_name print("user_session:", g.user_session.__dict__) uc.user = g.user_session.user_id unprocessed_traits = params['traits'] diff --git a/wqflask/wqflask/model.py b/wqflask/wqflask/model.py index b508f18e..192aedd0 100644 --- a/wqflask/wqflask/model.py +++ b/wqflask/wqflask/model.py @@ -57,8 +57,16 @@ class User(Base): user_collections = relationship("UserCollection", order_by="asc(UserCollection.name)", + lazy='dynamic', ) + def get_collection_by_name(self, collection_name): + try: + collect = self.user_collections.filter_by(name=collection_name).one() + except sqlalchemy.orm.exc.NoResultFound: + collect = None + return collect + @property def name_and_org(self): """Nice shortcut for printing out who the user is""" diff --git a/wqflask/wqflask/templates/collections/add.html b/wqflask/wqflask/templates/collections/add.html index 8b6a17a3..c87203b1 100644 --- a/wqflask/wqflask/templates/collections/add.html +++ b/wqflask/wqflask/templates/collections/add.html @@ -1,16 +1,16 @@