From a9f63aadeeedb7c28bbcd16396c8c6e7d5cbbfd0 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 31 Dec 2013 08:52:23 +0000 Subject: Finished code to remove a trait or multiple traits from a collection Wrote code to make buttons a little cleaner and more obvious when dealing with collections (they now enable themselves when traits that can be added or removed to/from a collection are selected) --- wqflask/wqflask/collect.py | 58 ++++++++++++++--- wqflask/wqflask/model.py | 6 ++ .../static/new/javascript/search_results.coffee | 61 +++++++++++++++++ .../static/new/javascript/search_results.js | 76 +++++++++++++++++++++- wqflask/wqflask/templates/collections/add.html | 2 +- wqflask/wqflask/templates/collections/view.html | 12 +++- 6 files changed, 203 insertions(+), 12 deletions(-) (limited to 'wqflask') diff --git a/wqflask/wqflask/collect.py b/wqflask/wqflask/collect.py index 2bf5b602..29b73130 100644 --- a/wqflask/wqflask/collect.py +++ b/wqflask/wqflask/collect.py @@ -67,8 +67,8 @@ def collections_new(): 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") + elif "Default" in params: + return add_traits(params, "Default") else: CauseAnError @@ -78,14 +78,14 @@ def collections_new(): 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") + 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") + return create_new("Default") else: uc = model.UserCollection.query.get(params['existing_collection']) - members = set(json.loads(uc.members)) + members = uc.members_as_set() #set(json.loads(uc.members)) len_before = len(members) traits = process_traits(params['traits']) @@ -111,7 +111,8 @@ def add_traits(params, collection_name): def process_traits(unprocessed_traits): print("unprocessed_traits are:", unprocessed_traits) - unprocessed_traits = unprocessed_traits.split(",") + if isinstance(unprocessed_traits, basestring): + unprocessed_traits = unprocessed_traits.split(",") traits = set() for trait in unprocessed_traits: data, _separator, hmac = trait.rpartition(':') @@ -144,13 +145,54 @@ def create_new(collection_name): @app.route("/collections/list") def list_collections(): params = request.args - user_collections = g.user_session.user_ob.user_collections + user_collections = list(g.user_session.user_ob.user_collections) + print("user_collections are:", user_collections) return render_template("collections/list.html", params = params, user_collections = user_collections, ) +@app.route("/collections/remove", methods=('POST',)) +def 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)) + + + +@app.route("/collections/delete", methods=('POST',)) +def delete_collection(): + params = request.form + uc_id = params['uc_id'] + uc = model.UserCollection.query.get(uc_id) + # Todo: For now having the id is good enough since it's so unique + # 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')) + + @app.route("/collections/view") def view_collection(): params = request.args diff --git a/wqflask/wqflask/model.py b/wqflask/wqflask/model.py index 192aedd0..e588e78a 100644 --- a/wqflask/wqflask/model.py +++ b/wqflask/wqflask/model.py @@ -10,6 +10,8 @@ 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 @@ -153,3 +155,7 @@ class UserCollection(Base): def num_members(self): print("members are:", json.loads(self.members)) return len(json.loads(self.members)) + + + def members_as_set(self): + return set(json.loads(self.members)) diff --git a/wqflask/wqflask/static/new/javascript/search_results.coffee b/wqflask/wqflask/static/new/javascript/search_results.coffee index 34989a77..84cf7f0a 100644 --- a/wqflask/wqflask/static/new/javascript/search_results.coffee +++ b/wqflask/wqflask/static/new/javascript/search_results.coffee @@ -1,4 +1,8 @@ $ -> + # These are also used by collections view + # So the name search_results in the filename is misleading + + checked_traits = null select_all = -> console.log("selected_all") @@ -17,6 +21,59 @@ $ -> console.log("checked is:", traits) $.colorbox({href:"/collections/add?traits=#{traits}"}) + removed_traits = -> + # After we've removed the traits from the database we get rid of them in the table + console.log('in removed_traits with checked_traits:', checked_traits) + checked_traits.closest("tr").fadeOut() + + + change_buttons = -> + buttons = ["#add", "#remove"] + num_checked = $('.trait_checkbox:checked').length + console.log("num_checked is:", num_checked) + if (num_checked == 0) + for button in buttons + $(button).prop("disabled", true) + else + for button in buttons + $(button).prop("disabled", false) + + + if (num_checked > 1) + console.log("in loop") + for item in buttons + console.log(" processing item:", item) + text = $(item).html() + if text.indexOf("Records") == -1 + text = text.replace("Record", "Records") + $(item).html(text) + else + console.log("in loop") + for item in buttons + console.log(" processing item:", item) + text = $(item).html() + text = text.replace("Records", "Record") + $(item).html(text) + + + # remove is only used by collections view + remove = -> + checked_traits = $("#trait_table input:checked") + traits = checked_traits.map(-> + return $(this).val()).get() + console.log("checked length is:", traits.length) + console.log("checked is:", traits) + uc_id = $("#uc_id").val() + console.log("uc.id is:", uc_id) + # Todo: Consider adding a failure message + $.ajax( + type: "POST" + url: "/collections/remove" + data: + uc_id: uc_id + traits: traits + success: removed_traits + ) @@ -24,3 +81,7 @@ $ -> $("#deselect_all").click(deselect_all) $("#invert").click(invert) $("#add").click(add) + $("#remove").click(remove) + + $('.trait_checkbox').click(change_buttons) + $('.btn').click(change_buttons) diff --git a/wqflask/wqflask/static/new/javascript/search_results.js b/wqflask/wqflask/static/new/javascript/search_results.js index 3efaca8b..477c9b94 100644 --- a/wqflask/wqflask/static/new/javascript/search_results.js +++ b/wqflask/wqflask/static/new/javascript/search_results.js @@ -2,7 +2,8 @@ (function() { $(function() { - var add, deselect_all, invert, select_all; + var add, change_buttons, checked_traits, deselect_all, invert, remove, removed_traits, select_all; + checked_traits = null; select_all = function() { console.log("selected_all"); return $(".trait_checkbox").prop('checked', true); @@ -24,10 +25,81 @@ href: "/collections/add?traits=" + traits }); }; + removed_traits = function() { + console.log('in removed_traits with checked_traits:', checked_traits); + return checked_traits.closest("tr").fadeOut(); + }; + change_buttons = function() { + var button, buttons, item, num_checked, text, _i, _j, _k, _l, _len, _len1, _len2, _len3, _results, _results1; + buttons = ["#add", "#remove"]; + num_checked = $('.trait_checkbox:checked').length; + console.log("num_checked is:", num_checked); + if (num_checked === 0) { + for (_i = 0, _len = buttons.length; _i < _len; _i++) { + button = buttons[_i]; + $(button).prop("disabled", true); + } + } else { + for (_j = 0, _len1 = buttons.length; _j < _len1; _j++) { + button = buttons[_j]; + $(button).prop("disabled", false); + } + } + if (num_checked > 1) { + console.log("in loop"); + _results = []; + for (_k = 0, _len2 = buttons.length; _k < _len2; _k++) { + item = buttons[_k]; + console.log(" processing item:", item); + text = $(item).html(); + if (text.indexOf("Records") === -1) { + text = text.replace("Record", "Records"); + _results.push($(item).html(text)); + } else { + _results.push(void 0); + } + } + return _results; + } else { + console.log("in loop"); + _results1 = []; + for (_l = 0, _len3 = buttons.length; _l < _len3; _l++) { + item = buttons[_l]; + console.log(" processing item:", item); + text = $(item).html(); + text = text.replace("Records", "Record"); + _results1.push($(item).html(text)); + } + return _results1; + } + }; + remove = function() { + var traits, uc_id; + checked_traits = $("#trait_table input:checked"); + traits = checked_traits.map(function() { + return $(this).val(); + }).get(); + console.log("checked length is:", traits.length); + console.log("checked is:", traits); + uc_id = $("#uc_id").val(); + console.log("uc.id is:", uc_id); + return $.ajax({ + type: "POST", + url: "/collections/remove", + data: { + uc_id: uc_id, + traits: traits + }, + success: removed_traits + }); + }; $("#select_all").click(select_all); $("#deselect_all").click(deselect_all); $("#invert").click(invert); - return $("#add").click(add); + $("#add").click(add); + $("#remove").click(remove); + $('.trait_checkbox').click(change_buttons); + return $('.btn').click(change_buttons); }); }).call(this); diff --git a/wqflask/wqflask/templates/collections/add.html b/wqflask/wqflask/templates/collections/add.html index c87203b1..faee4f78 100644 --- a/wqflask/wqflask/templates/collections/add.html +++ b/wqflask/wqflask/templates/collections/add.html @@ -10,7 +10,7 @@ Choose this if you're in a hurry or don't plan on using the collection again. If you are unsure this is probably the option you want. - +