def replace_user_filters(user_id): """ :Route: PUT /<user_id>/filter?filter=now&filter=popular :Description: Replaces all filters for a single user with id `user_id`. New filters passed by query parameter are validated. :param user_id: The unique ID of a specific user :type user_id: int :param filter: An optional query component/parameter that takes a list of values. Passed in values are validated as filters before being added. :type filter: str or None :return: Success/error message :Requires: Admin permissions """ new_filters = request.args.getlist('filter') # Check that user exists user = user_utils.get_user(user_id) if not user: return "No such user with id " + str(user_id) + " found!" valid_filters = [f for f in new_filters if user_utils.is_valid_filter(f)] users_collection.update({'account.id': str(user_id)}, {'$set': { 'app.filters': valid_filters }}) return "Replaced filters for user with id " + str(user_id)
def add_user_filters(user_id): """ :Route: POST /<user_id>/filter?filter=now&filter=popular :Description: Add new filters for a single user with id `user_id`. New filters passed by query parameter are validated. Only new filters are added, aka no duplicates are inserted. :param user_id: The unique ID of a specific user :type user_id: int :param filter: An optional query component/parameter that takes a list of values. Passed in values are validated as filters before being added. :type filter: str or None :return: Success/error message :Requires: Admin permissions """ new_filters = request.args.getlist('filter') # Check that user exists user = user_utils.get_user(user_id) if not user: return "No such user with id " + str(user_id) + " found!" # Get user filters updated = False added_filters = [] old_filters = user['app']['filters'] # Ignore filters already in filters list for new_f in [f for f in new_filters if f not in old_filters]: # Check that filter is valid and then add to filters list if user_utils.is_valid_filter(new_f): added_filters.append(new_f) updated = True if updated: users_collection.update( {'account.id': str(user_id)}, {'$push': { 'app.filters': { '$each': added_filters } }}) return "Added specified filters for user with id " + str(user_id) return "No filters specified to add to user with id " + str(user_id)
def add_user_past_events(user_id): """ :Route: POST /<user_id>/past?past_event=event_id&past_event=event_id :Description: Add new past events for a single user with id `user_id`. Only unique events are added, aka no duplicates are inserted. :param user_id: The unique ID of a specific user :type user_id: int :param past_event: An optional query component/parameter that takes a list of values. Passed in values are int event IDs which uniquely identify an event. :type past_event: int or None :return: Success/error message :Requires: Admin permissions """ new_past_events = request.args.getlist('past_event') # Check that user exists user = user_utils.get_user(user_id) if not user: return "No such user with id " + str(user_id) + " found!" # Get user past events updated = False added_events = [] old_past_events = user['app']['past_events'] # Ignore past events already in past events list for new_f in [f for f in new_past_events if f not in old_past_events]: added_events.append(new_f) updated = True if updated: users_collection.update( {'account.id': str(user_id)}, {'$push': { 'app.past_events': { '$each': added_events } }}) return "Added specified past events for user with id " + str(user_id) return "No past events specified to add to user with id " + str(user_id)
def remove_user_past_events(user_id): """ :Route: DELETE /<user_id>/past?past_event=event_id&past_event=event_id :Description: Remove past events for a single user with id `user_id`. If no past events are specified, all of the user's past events are removed. :param user_id: The unique ID of a specific user :type user_id: int :param past_event: An optional query component/parameter that takes a list of values. Passed in values are int event IDs which uniquely identify an event. :type past_event: int or None :return: Success/error message :Requires: Admin permissions """ remove_past_events = request.args.getlist('past_event') # Check that user exists user = user_utils.get_user(user_id) if not user: return "No such user with id " + str(user_id) + " found!" # If no past events specified, remove all past events from the user if not remove_past_events: users_collection.update({'account.id': str(user_id)}, {'$set': { 'app.past_events': [] }}) return "Removed all past events for user with id " + str(user_id) # Otherwise remove only the past events specified users_collection.update( {'account.id': str(user_id)}, {'$pull': { 'app.past_events': { '$in': remove_past_events } }}) return "Removed specified past events for user with id " + str(user_id)
def remove_user_filters(user_id): """ :Route: DELETE /<user_id>/filter?filter=now&filter=popular :Description: Remove filters for a single user with id `user_id`. Filters to remove are passed by query parameter. If no filters are specified, all of the user's filters are removed. :param user_id: The unique ID of a specific user :type user_id: int :param filter: An optional query component/parameter that takes a list of values. :type filter: str or None :return: Success/error message :Requires: Admin permissions """ remove_filters = request.args.getlist('filter') # Check that user exists user = user_utils.get_user(user_id) if not user: return "No such user with id " + str(user_id) + " found!" # If no filters specified, remove all filters from the user if not remove_filters: users_collection.update({'account.id': str(user_id)}, {'$set': { 'app.filters': [] }}) return "Removed all filters for user with id " + str(user_id) # Otherwise remove only the filters specified users_collection.update( {'account.id': str(user_id)}, {'$pull': { 'app.filters': { '$in': remove_filters } }}) return "Removed specified filters for user with id " + str(user_id)