def add_habit(): data = request.get_json() habit_name = data['habit_name'] user_id = data['user_id'] user = system.get_user(user_id) if validate_habit_name(habit_name) == 0: if user != -1: system.add_habit(user_id, habit_name) pickle_update(system) return {}, status.HTTP_200_OK else: error = {'error': 'unauthorized to make this request'} return error, status.HTTP_400_BAD_REQUEST else: error = {'error': validate_habit_name(habit_name)} return error, status.HTTP_400_BAD_REQUEST
def add_user(): data = request.get_json() username = data["username"] password = data["password"] email = data["email"] if validate_signup(username, password, email) == 0: if system.username_available(username) == 0: user_id = system.add_user(username, password, email) pickle_update(system) user = system.get_user(user_id) return user.toJSON(), status.HTTP_200_OK else: error = {'error': 'username taken'} return error, status.HTTP_400_BAD_REQUEST else: error = {'error': validate_signup(username, password, email)} return error, status.HTTP_400_BAD_REQUEST
def update_user(): data = request.get_json() action = data['action'] user_id = data['user_id'] # change password if action == "changePassword": oldPassword = data['oldPassword'] newPassword = data['newPassword'] if validate_password(newPassword) == 0: result = system.change_password(user_id, oldPassword, newPassword) if result == 0: pickle_update(system) return { 'error': 'successfully changed password' }, status.HTTP_200_OK else: error = {'error': 'old password is incorrect'} return error, status.HTTP_400_BAD_REQUEST else: error = {'error': validate_password(newPassword)} return error, status.HTTP_400_BAD_REQUEST # change email elif action == "changeEmail": newEmail = data['newEmail'] if validate_email(newEmail) == 0: result = system.change_email(user_id, newEmail) if result == 0: pickle_update(system) return { 'error': 'successfully changed email' }, status.HTTP_200_OK else: error = {'error': 'email must be different to current one'} return error, status.HTTP_400_BAD_REQUEST else: error = {'error': validate_email(newEmail)} return error, status.HTTP_400_BAD_REQUEST else: return {}, status.HTTP_400_BAD_REQUEST
def remove_habit(): user_id = request.args.get('user_id') user = system.get_user(user_id) habit_name = request.args.get('habit_name') if validate_habit_name(habit_name) == 0: if user != -1: if system.get_habit(user_id, habit_name) != -1: system.remove_habit(user_id, habit_name) pickle_update(system) return {}, status.HTTP_200_OK else: error = {'error': 'habit name does not exist'} return error, status.HTTP_400_BAD_REQUEST else: error = {'error': 'unauthorized to make this request'} return error, status.HTTP_400_BAD_REQUEST else: error = {'error': validate_habit_name(habit_name)} return error, status.HTTP_400_BAD_REQUEST return {}, status.HTTP_200_OK
def update_habit_status(): #actions: checkoff, uncheck , update_todays_status data = request.get_json() action = data['action'] user_id = data['user_id'] habit_name = data['habit_name'] checkoff = system.checkoff_habit(user_id, habit_name) if action == 'check': if checkoff == -1: return { 'error': 'user_id or habit name is invalid' }, status.HTTP_400_BAD_REQUEST elif checkoff == -2: system.uncheck_habit(user_id, habit_name) pickle_update(system) habit = system.get_habit(user_id, habit_name) print(habit.toJSON()) return {'habit_status': 'unchecked'}, status.HTTP_200_OK else: pickle_update(system) return {'habit_status': 'checked'}, status.HTTP_200_OK elif action == 'todays_status_to_false': change_todays_status = system.uncheck_habit(user_id, habit_name) if change_todays_status == -1: return { 'error': 'user_id or habit name is invalid' }, status.HTTP_400_BAD_REQUEST else: pickle_update(system) return { 'habit_status': 'todays status now false' }, status.HTTP_200_OK