def test_userprofile_is_deleted_from_db(self): """Test if the user profile has been successfully deleted from the database""" user = self._create_sample_user("test") userprofile = UserProfileModel.find_by_user_id(user.id) userprofile.delete_from_db() found_userprofile = UserProfileModel.find_by_user_id(user.id) self.assertIsNone(found_userprofile)
def delete(cls, training_id: int): """Delete method""" current_user_id = get_jwt_identity() user_profile = UserProfileModel.find_by_user_id(current_user_id) training = TrainingModel.find_by_id(training_id) if not training: return {"message": "Training not found."}, 404 if training.user_id != current_user_id: return ( { "message": "You don't have permission to perform this action." }, 403, ) user_profile.trainings_number -= 1 user_profile.kilometers_run -= training.distance try: training.delete_from_db() user_profile.save_to_db() except: return ( { "message": "An error has occurred deleting the training." }, 500, ) return {"message": "Training deleted"}, 200
def setUp(self): """Set up a test app, test client and test database""" self.app = self._set_up_test_app(create_app) self.client = self._set_up_client(self.app) self._set_up_test_db(db) self.user = self._create_sample_user( age=25, gender="Female", height=165, weight=58 ) self.user_profile = UserProfileModel.find_by_user_id(self.user.id)
def put(cls, training_id: int): """Put method""" current_user_id = get_jwt_identity() user_profile = UserProfileModel.find_by_user_id(current_user_id) training = TrainingModel.find_by_id(training_id) if not training: return {"message": "Training not found."}, 404 if training.user_id != current_user_id: return ( { "message": "You don't have permission to perform this action." }, 403, ) user_profile.kilometers_run -= training.distance training_data = training_schema.load(request.get_json()) if (TrainingModel.find_by_name_and_user_id( training_data.name, current_user_id) != training): return ( { "message": f"You have already created a training " f"called {training_data.name}. " f"Choose another name." }, 400, ) # bad request training.name = training_data.name training.distance = training_data.distance user_profile.kilometers_run += training.distance training.time_in_seconds = training_data.time_in_seconds training.calculate_average_tempo() training.calculate_calories_burnt() try: training.save_to_db() user_profile.save_to_db() except: return ( { "message": "An error has occurred updating the training." }, 500, ) return training_schema.dump(training), 200
def delete(cls, user_id: int): """Delete method""" current_user_id = get_jwt_identity() user = UserModel.find_by_id(user_id) if not user: return {"message": "User not found."}, 404 if current_user_id != user_id: return ( { "message": "You don't have permission to perform this action." }, 403, ) user_profile = UserProfileModel.find_by_user_id(user_id) user_profile.delete_from_db() user.delete_from_db() return {"message": "User deleted."}, 200
def delete(cls, user_id): """Delete the user""" claims = get_jwt_claims() if not claims["is_admin"]: return ( { "message": "You don't have permission to perform this action." }, 403, ) user = UserModel.find_by_id(user_id) if not user: return {"message": "User not found."}, 404 user_profile = UserProfileModel.find_by_user_id(user_id) user_profile.delete_from_db() user.delete_from_db() return {"message": "User deleted."}, 200
def post(cls): """Post method""" current_user_id = get_jwt_identity() user_profile = UserProfileModel.find_by_user_id(current_user_id) json_data = daily_needs_schema.load(request.get_json()) user_profile.daily_cal = json_data["daily_cal"] try: user_profile.save_to_db() except: return {"message": "An error has occurred updating the user."}, 500 return ( { "message": "Daily caloric needs have been updated", "daily_cal": json_data["daily_cal"], }, 201, )
def post(cls): """Post method""" training_json = request.get_json() current_user_id = get_jwt_identity() user_profile = UserProfileModel.find_by_user_id(current_user_id) training = TrainingModel.find_by_name_and_user_id( training_json["name"], current_user_id) if training: return ( { "message": f"You have already created a training " f"called {training_json['name']}. " f"Choose another name." }, 400, ) # bad request training = training_schema.load(training_json) training.user_id = current_user_id training.calculate_average_tempo() training.calculate_calories_burnt() user_profile.trainings_number += 1 user_profile.kilometers_run += training.distance try: training.save_to_db() user_profile.save_to_db() except: return ( { "message": "An error has occurred inserting the training." }, 500, ) # internal server error return training_schema.dump(training), 201
def _get_users_weight(self) -> int: """Access user's weight by user profile""" user_profile = UserProfileModel.find_by_user_id(self.user_id) return user_profile.weight
def __given_test_user_is_created(self): self.user1 = self._create_sample_user(username="******") self.user_profile1 = UserProfileModel.find_by_user_id(self.user1.id) self.access_token = self._get_access_token(self.client)
def __given_other_user_is_created(self): self.user2 = self._create_sample_user(username="******") self.user_profile2 = UserProfileModel.find_by_user_id(self.user2.id)