about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSam2013-10-30 14:52:09 -0500
committerSam2013-10-30 14:52:09 -0500
commit78ff756b0d4416f8b1fc4c0008d39bd14b5194f3 (patch)
treeb2b3624414e0f39bb602245fe217b78a30601414
parent5c5671f3c3353f6adc1ce5d9cab8f9f4de5a4e05 (diff)
downloadgenenetwork2-78ff756b0d4416f8b1fc4c0008d39bd14b5194f3.tar.gz
Progress on user collections
-rw-r--r--wqflask/wqflask/collect.py57
-rw-r--r--wqflask/wqflask/model.py7
-rw-r--r--wqflask/wqflask/templates/collections/add.html55
-rw-r--r--wqflask/wqflask/user_manager.py17
4 files changed, 117 insertions, 19 deletions
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 @@
 <div id="myModal">
     <div class="modal-header">
-        <h3 id="myModalLabel">Add to collection</h3>
+        <h3>Add to collection</h3>
+        <p>You have three choices: Contuine without naming the collection, creating a new named collection,
+        or adding the traits to an existing collection.</p>
+    </div>
+    <div class="modal-body">
+        <form action="/collections/new" data-validate="parsley" id="add_form">
+            <fieldset>
+                <legend>Continue without naming collection</legend>
+                <span class="help-block">Choose this if you don't plan on using the collection again.</span>
+                <span class="help-block"><em></em>If you are unsure this is probably the option you want.</em></span>
+                <button type="submit" name="continue" class="btn">Continue</button>
+            </fieldset>
+            <hr />
+
+
+            <input type="hidden" name="traits" value="{{ traits }}" />
+            <fieldset>
+                <legend>Or create a new named collection</legend>
+                <label>New collection name</label>
+                <input type="text" name="new_collection" placeholder="Name of new collection..."
+                    data-trigger="change" data-minlength="5" data-maxlength="50">
+                <span class="help-block">Type the name of the new collection.</span>
+                <button type="submit" name="create_new" class="btn">Create and add traits</button>
+            </fieldset>
+
+            <hr />
+            <fieldset>
+                <legend>Or add to an existing collection</legend>
+                <label>Existing collection name</label>
+
+                <select name="existing_collection" class="form-control">
+                {% for col in user_collections %}
+                    <option value="{{ col.id }}">{{ col.name }}</option>
+                {% endfor %}
+                </select>
+                <br />
+
+                <button type="submit" name="add_to_existing" class="btn">Add to existing collection</button>
+            </fieldset>
+        </form>
     </div>
-    <form action="/collections/new">
-        <input type="hidden" name="traits" value="{{ traits }}" />
-        <fieldset>
-            <legend>Create a new collection</legend>
-            <label>New collection name</label>
-            <input type="text" name="new_collection" placeholder="Name of new collection...">
-            <span class="help-block">Type the name of the new collection.</span>
-            <button type="submit" class="btn">Create and add traits</button>
-        </fieldset>
-</form>
 </div>
+
+<script>
+    $('#add_form').parsley();
+</script>
diff --git a/wqflask/wqflask/user_manager.py b/wqflask/wqflask/user_manager.py
index f519aed9..913ff231 100644
--- a/wqflask/wqflask/user_manager.py
+++ b/wqflask/wqflask/user_manager.py
@@ -77,6 +77,23 @@ class UserSession(object):
             print("record is:", self.record)
             self.logged_in = True
 
+    @property
+    def user_id(self):
+        """Shortcut to the user_id"""
+        return self.record['user_id']
+
+    @property
+    def user_ob(self):
+        """Actual sqlalchemy record"""
+        # Only look it up once if needed, then store it
+        try:
+            # Already did this before
+            return self.db_object
+        except AttributeError:
+            # Doesn't exist so we'll create it
+            self.db_object = model.User.query.get(self.user_id)
+            return self.db_object
+
 
     def delete_session(self):
         # And more importantly delete the redis record