Esempio n. 1
0
 def delete_recipe(token, category_id, recipe_id):
     """The function delete a recipe"""
     try:
         # get the user id from the token
         user_id, error = Users.decode_token(token), \
             {'Error': 'Category not found', 'e': 404}
         if check_category_id(user_id, category_id, error, True):
             user_recipe = Recipes.query.filter_by(
                 rec_cat=category_id, rec_id=recipe_id).first()
             if user_recipe is not None:
                 user_recipe.delete()
                 return jsonify({'message': 'recipe deleted'}), 200
             else:
                 create_error({'Error': 'Recipe not found'}, 404)
         return format_error['error']
     # capture value error
     except Exception as ex:
         excepts = {
             'ValueError': {
                 'Error':
                 'Invalid entry, please ' +
                 'provide the category id and recipe ' + 'id as integers',
                 'e':
                 400
             },
             'BadRequest': {
                 'Error': 'Please parse the recipe id ' + 'and category id',
                 'e': 400
             }
         }
         handle_exceptions(type(ex).__name__, excepts)
         return format_error['error']
Esempio n. 2
0
 def add_recipe(token, category_id):
     """The function adds recipes to the database"""
     try:
         user_id, data, error = Users.decode_token(token), request.json, \
             {'Error': 'category not found', 'e': 404}
         if check_category_id(user_id, category_id, error, True):
             return do_create_recipe(data, category_id)
         return format_error['error']
     except Exception as ex:
         excepts = {
             'ValueError': {
                 'Error':
                 'Invalid entry, please provide' +
                 ' the category id as integer while ' +
                 'recipe name and ingredients as string',
                 'e':
                 400
             },
             'IntegrityError': {
                 'Error': 'Recipe name already exists',
                 'e': '409'
             },
             'BadRequest': {
                 'Error': 'Please parse category id, ' +
                 'recipe name and ingredients',
                 'e': 400
             }
         }
         handle_exceptions(type(ex).__name__, excepts)
         return format_error['error']
Esempio n. 3
0
 def update_recipe(token, category_id, recipe_id):
     """The function updates a recipe"""
     try:
         user_id, data, error = Users.decode_token(token), request.json, \
             {'Error': 'category not found', 'e': 404}
         recipe_category_id = data.pop('recipe_category_id')
         if int(recipe_category_id) and validation(
             data, ['recipe_name', 'ingredients', 'description']) \
                 and check_category_id(user_id, category_id, error, True):
             return do_recipe_update([data, recipe_category_id], user_id,
                                     category_id, recipe_id)
         return format_error['error']
     except Exception as ex:
         excepts = {
             'KeyError': {
                 'Error': str(ex).strip('\'') + ' key missing',
                 'e': 400
             },
             'IntegrityError': {
                 'Error': 'Recipe name already ' + 'exists',
                 'e': 409
             },
             'ValueError': {
                 'Error': 'Invalid entry',
                 'e': 400
             },
             'BadRequest': {
                 'Error': 'Please parse category id, ' +
                 'recipe name and ingredients',
                 'e': 400
             }
         }
         handle_exceptions(type(ex).__name__, excepts)
         return format_error['error']
Esempio n. 4
0
 def view_category_recipes(token, category_id):
     """The function return recipes in a category"""
     try:
         user_id, error = Users.decode_token(token), \
             {'Error': 'category not found', 'e': 404}
         if check_category_id(user_id, category_id, error, True):
             return get_recipes(user_id, category_id, True)
         return format_error['error']
     # capture value error
     except Exception as ex:
         excepts = {
             'ValueError': {
                 'Error':
                 'Invalid entry, please ' + ' provide the category id as ' +
                 'integer',
                 'e':
                 400
             },
             'BadRequest': {
                 'Error': 'Please parse the category ' + 'id',
                 'e': 400
             }
         }
         handle_exceptions(type(ex).__name__, excepts)
         return format_error['error']
Esempio n. 5
0
def update_category(user_id, category_id, error):
    if check_category_id(user_id, category_id, error, True):
        objects['category'].cat_name = valid_data["category_name"]
        objects['category'].cat_description = valid_data[
            "category_description"]
        objects['category'].update()
        return jsonify(return_category('category updated')), 201
    return format_error['error']
Esempio n. 6
0
 def view_a_category(token, category_id):
     """The function returns one category"""
     try:
         user_id, error = Users.decode_token(token), \
             {'Error': 'category not found', 'e': 404}
         # get the category object with category
         if check_category_id(user_id, category_id, error, True):
             return jsonify(return_category('category found')), 200
         return format_error['error']
     except Exception as ex:
         return jsonify({'Error': str(ex)}), 400
Esempio n. 7
0
 def delete_category(token, category_id):
     """The function deletes a category"""
     try:
         # get user id from token
         user_id, error = Users.decode_token(token), \
             {'Error': 'category not found', 'e': 404}
         if check_category_id(user_id, category_id, error, True):
             objects['category'].delete()
             return jsonify({'message': 'category deleted'}), 200
         return format_error['error']
     except Exception as ex:
         excepts = {
             'ValueError': {
                 'Error': 'Invalid entry for category id',
                 'e': 400
             },
             'BadRequest': {
                 'Error': 'Please parse category id',
                 'e': 400
             }
         }
         handle_exceptions(type(ex).__name__, excepts)
         return format_error['error']
Esempio n. 8
0
 def view_one_recipe(token, category_id, recipe_id):
     """The function returns one recipe"""
     try:
         user_id, error = Users.decode_token(token), \
             {'Error': 'category not found', 'e': 404}
         if check_category_id(user_id, category_id, error, True):
             return get_recipes(user_id, category_id, False, recipe_id)
         return format_error['error']
     except Exception as ex:
         excepts = {
             'ValueError': {
                 'Error':
                 'Invalid entry, please ' +
                 'provide the category id and recipe ' + 'id as integers',
                 'e':
                 400
             },
             'BadRequest': {
                 'Error': 'Please parse the recipe id ' + 'and category id',
                 'e': 400
             }
         }
         handle_exceptions(type(ex).__name__, excepts)
         return format_error['error']