def get(self, current_user, category_id=None, title=None): """ Resource that gets a list of recipes """ # Find category that recipe goes with category = None if category_id: category = Category.get_by_id(category_id) else: category = Category.get_by_title(title) if not category: abort(404, {"message": "Category does not exist."}) # Get the category's recipes recipes = Recipe.query.filter_by(category_id=category.id) args = recipe_collection_parser.parse_args() # fancy url argument query filtering! if args['title'] is not None: recipes = Recipe.query.filter( Recipe.title.ilike('%' + args['title'] + '%')).filter( Recipe.category_id == category.id) if not recipes: abort(404, {"message": "No recipes to display."}) return recipes
def post(self, current_user, user_id=None, username=None): """ Resource that creates a new category """ args = category_parser.parse_args() abort_if_exists(g.user.id, category_name=args['title']) # user owns the category args['user_id'] = g.user.id category = Category.create(**args) return category, 201
def put(self, current_user, user_id=None, category_id=0, **kwargs): """ Resource that updates a category by id""" category = Category.get_by_id(category_id) args = category_parser.parse_args() abort_if_exists(g.user.id, category_name=args['title']) if not category: abort(404, { "message": "Category does not exist" }) category.update(**category_parser.parse_args()) return category
def abort_if_exists(user_id, category_name=None, category_id=None, recipe_name=None): category = None recipe = None if category_name: categories = Category.get_user_all(user_id) categorylist = [] for category in categories: categorylist.append(category.title) if category_name in categorylist: abort(409, {"message": "Category already exists"}) if recipe_name: category = Category.get_by_id(category_id) if category.user_id == user_id: if Recipe.recipe_exists(category_id, recipe_name): abort(409, {"message": "Recipe already exists"}) else: abort(403, {"message": "Category does not belong to this user"})
def delete(self, current_user, user_id=None, category_id=0, **kwargs): """ Resource that deletes a category by id""" category = Category.get_by_id(category_id) if not category: abort(404) category.delete() result = { 'status': 'Deleted', 'message': 'Category deleted successfully', 'id': category_id } return result
def post(self, current_user, category_id=None, title=None): """ Resource that creates a new recipe """ args = recipe_parser.parse_args() if args['category_id'] != category_id: abort(404, {"message": "Provide valid category id."}) category = Category.get_by_id(category_id) if not category: abort(404, {"message": "Category does not exist."}) # abort if recipe exists abort_if_exists(g.user.id, category_id=category_id, recipe_name=args['title']) recipe = Recipe.create(**args) return recipe, 201
def measure_category(**kwargs): if kwargs.get('category_id', None): category = Category.get_by_id(kwargs['category_id']) if not category: abort(404, { "status": "Bad request", "state": "Category does not exist" }) if g.user.id != category.user_id: abort( 403, { "status": "Access denied", "state": "Entity not accessible to current user. Provide valid name or id." })
def measure_recipe(**kwargs): if kwargs.get('recipe_id', None): recipe = Recipe.get_by_id(kwargs['recipe_id']) if not recipe: abort(404, { "status": "Bad request", "state": "Recipe does not exist" }) category = Category.get_by_id(recipe.category_id) if g.user.id != category.user_id: abort( 403, { "status": "Access denied", "state": "Entity not accessible to current user. Provide valid name or id." })
def get(self, current_user, user_id=None, category_id=0, **kwargs): """ Resource that gets a category by id""" category = Category.get_by_id(category_id) if not category: abort(404, { "message": "Category does not exist" }) return category