예제 #1
0
    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
예제 #2
0
 def get(cls, training_id: int):
     """Get method"""
     current_user_id = get_jwt_identity()
     training = TrainingModel.find_by_id(training_id)
     if training and current_user_id == training.user_id:
         return training_schema.dump(training), 200
     return {"message": "Training not found."}, 404
예제 #3
0
    def test_delete_method_deletes_training_from_db(self):
        """Test if delete method deletes the training from the database"""
        self.client.delete(
            path=f"trainings/{self.training.id}",
            headers={
                "Content-Type": "application/json",
                "Authorization": f"Bearer {self.access_token}",
            },
        )

        training = TrainingModel.find_by_id(self.training.id)

        self.assertIsNone(training)
예제 #4
0
    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
예제 #5
0
    def test_put_method_updates_training_in_db(self):
        """Test if put method updates the training in the database"""
        data = {
            "name": self.training.name,
            "distance": 7,
            "time_in_seconds": 3700,
        }
        self.client.put(
            path=f"trainings/{self.training.id}",
            data=json.dumps(data),
            headers={
                "Content-Type": "application/json",
                "Authorization": f"Bearer {self.access_token}",
            },
        )

        training = TrainingModel.find_by_id(self.training.id)

        expected_avg_tempo = 6.8

        self.assertEqual(training.distance, data["distance"])
        self.assertEqual(training.time_in_seconds, data["time_in_seconds"])
        self.assertEqual(training.avg_tempo, expected_avg_tempo)
예제 #6
0
    def test_find_by_id_no_training(self):
        """Test if None is returned
        if the training with the given id doesn't exist"""
        found_training = TrainingModel.find_by_id(1)

        self.assertIsNone(found_training)
예제 #7
0
    def test_find_by_id(self):
        """Test if the training is found"""
        training = self._create_sample_training(self.user)
        found_training = TrainingModel.find_by_id(1)

        self.assertEqual(training, found_training)