def login(): """The function logs in a new user""" try: print("====================================") data, q_error = request.json, {'Error': 'User not found', 'e': 403} if validation(data, ['username', 'password']) and \ query_username(q_error, True): if check_password_hash(objects['user'].user_password, valid_data['password']): token = objects['user'].generate_auth_token() login_user(objects['user']) user = { "username": objects['user'].user_username, "name": objects['user'].user_name, "email": objects['user'].user_email } print(user, "+++++++++++++++++++++++++++") return jsonify({ 'user': user, 'token': token.decode('ascii'), 'message': 'login was successful' }), 200 else: create_error({'Error': 'Incorrect password'}, 403) return format_error['error'] except Exception as ex: excepts = { 'BadRequest': { 'Error': 'Please ensure all fieds are ' + 'correctly specified', 'e': 400 } } handle_exceptions(type(ex).__name__, excepts) return format_error['error']
def delete_account(token): """The function deletes a user's account""" try: user_id, data = Users.decode_token(token), request.json if validation(data, ['password']): user = Users.query.filter_by(id=user_id).first() # check if the password provided matches the known if check_password_hash(user.user_password, valid_data['password']): user.delete() return jsonify({'Error': 'Account deleted'}), 200 else: create_error({'Error': 'Incorrect password'}, 403) return format_error['error'] except Exception as ex: excepts = { 'TypeError': { 'Error': 'Please provide a password ' + 'key and value', 'e': 400 }, 'BadRequest': { 'Error': 'Password is missing', 'e': 400 } } handle_exceptions(type(ex).__name__, excepts) return format_error['error']
def do_recipe_update(data_list, user_id, category_id, recipe_id): data, recipe_category_id = data_list user_recipe = Recipes.query.filter_by(rec_cat=category_id, rec_id=recipe_id).first() if user_recipe is not None: user_recipe.rec_name = valid_data['recipe_name'] user_recipe.rec_cat = category_id user_recipe.rec_ingredients = valid_data['ingredients'], user_recipe.rec_description = valid_data['description'] user_recipe.rec_cat = recipe_category_id user_recipe.update() return jsonify({ 'recipe_id': user_recipe.rec_id, 'recipe_name': user_recipe.rec_name, 'category_name': objects['category'].cat_name, 'category_id': user_recipe.rec_cat, 'description': user_recipe.rec_description, 'recipe_ingredients': user_recipe.rec_ingredients.split(','), 'message': 'Recipe updated' }), 201 create_error({'Error': 'Recipe not found'}, 404) return format_error['error']
def do_recipes_search(user_id): found_recipes = find_recipes(user_id) current_page, number_of_pages, next_page, previous_page, page = \ found_recipes[0].page, found_recipes[0].pages, \ found_recipes[0].next_num, found_recipes[0].prev_num, found_recipes[1] if page <= number_of_pages: results = [] for recipe in found_recipes[0].items: result = { 'category_id': recipe.cat_id, 'category_name': recipe.cat_name, 'id': recipe.rec_id, 'recipe_name': recipe.rec_name, 'description': recipe.rec_description, 'ingredients': recipe.rec_ingredients.split(','), 'recipe_date': recipe.rec_date } results.append(result) return jsonify({ 'recipes': results, 'count': str(len(results)), 'current_page': current_page, 'number_of_pages': number_of_pages, 'next_page': next_page, 'previous_page': previous_page, 'message': 'Recipes found' }), 200 else: create_error({'Error': 'Page not found'}, 404) return format_error['error']
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']
def password_reset_verification(user_id): user = Users.query.filter_by(id=user_id).first() if check_password_hash(user.user_password, valid_data['password']): if valid_data['new_password'] == valid_data['confirm_password']: objects['user'] = user return True else: create_error({'Error': 'Passwords do not match'}, 400) else: create_error({'Error': 'Incorrect password'}, 403)
def check_category_id(user_id, category_id, e, state): user_category = Category.query.filter_by( cat_id=category_id, user_id=user_id).first() if user_category is None and not state: return True elif user_category and state: objects['category'] = user_category return objects error['Error'] = e['Error'] create_error(error, e['e'])
def check_category_name(user_id, e, state): check_category = Category.query.filter_by( user_id=int(user_id), cat_name=valid_data['category_name'] ).first() if check_category is None and not state: return True elif check_category and state: objects['category'] = check_category return objects error['Error'] = e['Error'] create_error(error, e['e'])
def get_one_recipe(user_recipes): """The method returns one recipe from a parsed recipes object""" if user_recipes is not None: results = create_recipes_list(user_recipes) for recipe in results: recipe['category_name'] = objects['category'].cat_name if len(results): return jsonify({ 'recipes': results, 'message': 'recipe found', 'count': len(results) }), 200 else: create_error({'error': 'no recipes found'}, 404) create_error({'Error': 'no recipes found'}, 404) return format_error['error']
def get_paginated_recipes(user_recipes): """The method returns paginated recipes from a parsed recipes object""" if user_recipes.items: current_page, number_of_pages, next_page, previous_page = \ user_recipes.page, user_recipes.pages, user_recipes.next_num, \ user_recipes.prev_num results = create_recipes_list(user_recipes.items) for recipe in results: recipe['category_name'] = objects['category'].cat_name return jsonify({ 'recipes': results, 'count': str(len(results)), 'current_page': current_page, 'number_of_pages': number_of_pages, 'category_name': objects['category'].cat_name, 'next_page': next_page, 'previous_page': previous_page, 'message': 'recipes found' }), 200 create_error({'Error': 'no recipes found'}, 404) return format_error['error']