def add_interests(user_id, interests): logger.warning('add_interests user_id:%s interests:%s' % (user_id, str(interests))) user = User.get_by_id(user_id) for interest in interests: if not interest: continue if interest not in user.get('interests', []): User.push_user_field(user_id, 'interests', interest) network = user.get('network') interest = interest.strip('#').lower() interest_data = Interest.get_by_name_network(interest, network) int_col = get_interest_collection() if not interest_data: #Add a new one interest_data = { 'name' : interest, 'num_followers' : 1, 'followers' : [user_id], 'network' : network, 'created' : datetime.datetime.now(), 'creator' : user_id } user_id = int_col.insert(interest_data) else: #Update existing # push={} # updates = {} followers = interest_data.get('followers') if user_id not in followers: updates = {"$push" : {'followers' : user_id}, "$set" : { "num_followers" : len(followers) + 1 }} int_col.update({"name" : interest}, updates)
def remove_interests(user_id, interests): logger.warning('remove_interests user_id:%s interests:%s' % (user_id, str(interests))) for interest in interests: user = User.get_by_id(user_id) network = user.get('network') existing = set(user.get('interests', [])) if interest in existing: remaining = list(existing - set([interest])) User.set_user_field(user_id, 'interests', remaining) interest_data = Interest.get_by_name_network(interest, network) int_col = get_interest_collection() # user = User.get_by_id(user_id) # network = user.get('network') if user_id in interest_data.get('followers'): int_col.update({"name" : interest, 'network' : network}, {"$inc" : { "num_followers" : -1 }, "$pull" : { "followers" : user_id } })
def check_network_interests(network): """ Checks that the current network has the pre populated interests added, if not it adds them """ logger.warning("check_network_interests network:%s" % network) if not network: logger.warning("check_network_interests network:%s network value none so exiting" % network) return interests = list(Interest.get_all_from_network(network)) logger.warning("check_network_interests network:%s %s interests found" % (network, str(len(interests)))) if len(interests) == 0: new_interests = [] for name in Interest.PRE_POPULATE: new_interests.append({ 'name' : name, 'num_followers' : 0, 'followers' : [], 'network' : network, 'created' : datetime.datetime.now(), 'creator' : 'prepop' }) int_col = get_interest_collection() user_id = int_col.insert(new_interests)