diff options
Diffstat (limited to 'wqflask/wqflask/collect.py')
-rw-r--r-- | wqflask/wqflask/collect.py | 109 |
1 files changed, 22 insertions, 87 deletions
diff --git a/wqflask/wqflask/collect.py b/wqflask/wqflask/collect.py index 2bd4b721..e1aceac8 100644 --- a/wqflask/wqflask/collect.py +++ b/wqflask/wqflask/collect.py @@ -88,7 +88,6 @@ class AnonCollection(object): def add_traits(self, params): #assert collection_name == "Default", "Unexpected collection name for anonymous user" self.traits = list(process_traits(params['traits'])) - #len_before = len(Redis.smembers(self.key)) existing_collections = Redis.get(self.key) logger.debug("existing_collections:", existing_collections) if existing_collections != None and existing_collections != "None": @@ -138,54 +137,12 @@ class AnonCollection(object): collections_list[collection_position]['num_members'] = len(collections_list[collection_position]['members']) collections_list[collection_position]['changed_timestamp'] = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p') len_now = collections_list[collection_position]['num_members'] - #print("before in redis:", json.loads(Redis.get(self.key))) Redis.set(self.key, json.dumps(collections_list)) - #print("currently in redis:", json.loads(Redis.get(self.key))) # 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_now) - -class UserCollection(object): - """User is logged in""" - - def add_traits(self, params, collection_name): - logger.debug("---> params are:", params.keys()) - logger.debug(" 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'].split(":")[0]) - members = list(uc.members_as_set()) #set(json.loads(uc.members)) - len_before = len(members) - - traits = process_traits(params['traits']) - - members_now = members - for trait in traits: - if trait in members: - continue - else: - members_now.append(trait) - - #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() - - logger.debug("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 process_traits(unprocessed_traits): #print("unprocessed_traits are:", unprocessed_traits) if isinstance(unprocessed_traits, basestring): @@ -216,9 +173,8 @@ def collections_add(): traits=request.args['traits'] if g.user_session.logged_in: - logger.debug("user_session",g.user_session) - user_collections = g.user_session.user_ob.user_collections - logger.debug("user_collections are:", user_collections) + user_collections = g.user_session.user_collections + #logger.debug("user_collections are:", user_collections) return render_template("collections/add.html", traits = traits, collections = user_collections, @@ -262,14 +218,9 @@ def create_new(collection_name): traits = process_traits(unprocessed_traits) if g.user_session.logged_in: - uc = model.UserCollection() - uc.name = collection_name - logger.debug("user_session:", g.user_session.__dict__) - uc.user = g.user_session.user_id - uc.members = json.dumps(list(traits)) - db_session.add(uc) - db_session.commit() - return redirect(url_for('view_collection', uc_id=uc.id)) + uc_id = g.user_session.add_collection(collection_name, traits) + + return redirect(url_for('view_collection', uc_id=uc_id)) else: ac = AnonCollection(collection_name) ac.changed_timestamp = datetime.datetime.utcnow().strftime('%b %d %Y %I:%M%p') @@ -279,17 +230,17 @@ def create_new(collection_name): @app.route("/collections/list") def list_collections(): params = request.args - logger.debug("PARAMS:", params) + #logger.debug("PARAMS:", params) if g.user_session.logged_in: - user_collections = list(g.user_session.user_ob.user_collections) - logger.debug("user_collections are:", user_collections) + user_collections = list(g.user_session.user_collections) + #logger.debug("user_collections are:", user_collections) return render_template("collections/list.html", params = params, collections = user_collections, ) else: anon_collections = user_manager.AnonUser().get_collections() - logger.debug("anon_collections are:", anon_collections) + #logger.debug("anon_collections are:", anon_collections) return render_template("collections/list.html", params = params, collections = anon_collections) @@ -298,20 +249,14 @@ def list_collections(): @app.route("/collections/remove", methods=('POST',)) def remove_traits(): params = request.form - logger.debug("params are:", params) if "uc_id" in params: uc_id = params['uc_id'] - uc = model.UserCollection.query.get(uc_id) traits_to_remove = params.getlist('traits[]') traits_to_remove = process_traits(traits_to_remove) logger.debug("\n\n after processing, traits_to_remove:", traits_to_remove) - all_traits = uc.members_as_set() - members_now = all_traits - traits_to_remove - logger.debug(" members_now:", members_now) - uc.members = json.dumps(list(members_now)) - uc.changed_timestamp = datetime.datetime.utcnow() - db_session.commit() + + members_now = g.user_session.remove_traits_from_collection(uc_id, traits_to_remove) else: collection_name = params['collection_name'] members_now = AnonCollection(collection_name).remove_traits(params) @@ -325,22 +270,13 @@ def remove_traits(): @app.route("/collections/delete", methods=('POST',)) def delete_collection(): params = request.form - logger.debug("params:", params) if g.user_session.logged_in: uc_id = params['uc_id'] if len(uc_id.split(":")) > 1: for this_uc_id in uc_id.split(":"): - uc = model.UserCollection.query.get(this_uc_id) - collection_name = uc.name - db_session.delete(uc) - db_session.commit() + collection_name = g.user_session.delete_collection(this_uc_id) else: - 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() + collection_name = g.user_session.delete_collection(uc_id) else: if "collection_name" in params: collection_name = params['collection_name'] @@ -348,7 +284,10 @@ def delete_collection(): for this_collection in params['uc_id'].split(":"): user_manager.AnonUser().delete_collection(this_collection) - flash("We've deleted the collection: {}.".format(collection_name), "alert-info") + if len(uc_id.split(":")) > 1: + flash("We've deleted the selected collections.", "alert-info") + else: + flash("We've deleted the collection: {}.".format(collection_name), "alert-info") return redirect(url_for('list_collections')) @@ -356,12 +295,11 @@ def delete_collection(): @app.route("/collections/view") def view_collection(): params = request.args - logger.debug("PARAMS in view collection:", params) - if "uc_id" in params: + if g.user_session.logged_in and "uc_id" in params: uc_id = params['uc_id'] - uc = model.UserCollection.query.get(uc_id) - traits = json.loads(uc.members) + uc = (collection for collection in g.user_session.user_collections if collection["id"] == uc_id).next() + traits = uc["members"] else: user_collections = json.loads(Redis.get(user_manager.AnonUser().key)) this_collection = {} @@ -369,10 +307,8 @@ def view_collection(): if collection['id'] == params['collection_id']: this_collection = collection break - #this_collection = user_collections[params['collection_id']] - traits = this_collection['members'] - logger.debug("in view_collection traits are:", traits) + traits = this_collection['members'] trait_obs = [] json_version = [] @@ -398,9 +334,8 @@ def view_collection(): collection_info = dict(trait_obs=trait_obs, collection_name=this_collection['name']) if "json" in params: - logger.debug("json_version:", json_version) return json.dumps(json_version) else: return render_template("collections/view.html", **collection_info - ) + )
\ No newline at end of file |