def delete_saved_search(saved_search_id): """Delete saved search by ID. Endpoint deleting saved search by it's ID. Request should contain saved search ID. If saved search is successfully deleted, it's ID will be returned. If there isn't any saved search with provided ID, 404 status code and "No saved search with id <id> found for current user" message will be returned. Args: saved_search_id (int): ID of saved search to delete Returns: Flask Response object containing ID of deleted saved search or error message if saved search is not found. """ user = login.get_current_user(use_external_user=False) saved_search = user.saved_searches.filter( SavedSearch.id == saved_search_id ).first() if not saved_search: return make_error_response( "No saved search with id {} found for current user".format( saved_search_id, ), 404, force_json=True, ) db.session.delete(saved_search) db.session.commit() return json_success_response({"deleted": saved_search_id})
def get_saved_searches_by_id(saved_search_id): """Get a saved search by ID. Endpoint returning saved search JSON representation by it's ID. Request should contain saved search ID. If there isn't any saved search with provided ID, 404 status code and "No saved search with id <id> found" message will be returned. Args: saved_search_id (int): ID of saved search to look for. Returns: Flask Response object containing JSON representation of saved search or error message if saved search was not found. """ saved_search = SavedSearch.query.filter( SavedSearch.id == saved_search_id ).first() if saved_search: response_data = { SavedSearch.__name__: saved_search } return json_success_response(response_data) return make_error_response( "No saved search with id {} found".format( saved_search_id, ), 404, force_json=True, )
def create_saved_search(): """Create a saved search. Endpoint creating saved search with provided parameters. Request payload should contain saved search `name`, `search_type` and `object_type`. Also it could contain saved search `filters`. If there will any error during saved search creation, 400 status code and corresponding error will be returned. Returns: Flask Response object containing JSON representation of created saved search or error message if error occurred. """ user = login.get_current_user(use_external_user=False) data = request.get_json() try: search = SavedSearch( data.get("name"), data.get("object_type"), user, data.get("search_type"), data.get("filters"), ) except ValidationError as error: return make_error_response(error.message, 400, force_json=True) user.saved_searches.append(search) db.session.commit() return json_success_response(search)
def delete_saved_search(saved_search_id): user = login.get_current_user(use_external_user=False) saved_search = user.saved_searches.filter( SavedSearch.id == saved_search_id).first() if not saved_search: return make_error_response( "No saved search with id {} found for current user".format( saved_search_id, ), 404, force_json=True, ) db.session.delete(saved_search) db.session.commit() return json_success_response({"deleted": saved_search_id})
def create_saved_search(): user = login.get_current_user(use_external_user=False) data = request.get_json() try: search = SavedSearch( data.get("query"), data.get("name"), data.get("object_type"), user, ) except ValidationError as error: return make_error_response(error.message, 400, force_json=True) user.saved_searches.append(search) db.session.commit() return json_success_response(search)