예제 #1
0
def delete_recordings(dataset_id):
    """Delete recordings from a class in a dataset.

    **Example request**:

    .. sourcecode:: json

        {
            "class_name": "Happy",
            "recordings": ["770cc467-8dde-4d22-bc4c-a42f91e"]
        }

    :reqheader Content-Type: *application/json*
    :<json string class_name: *Required.* Name of the class.
    :<json array recordings: *Required.* Array of recoding MBIDs (``string``) that need be deleted from a class.

    :resheader Content-Type: *application/json*
    """
    ds = get_check_dataset(dataset_id, write=True)
    class_dict = request.get_json()
    try:
        dataset_validator.validate_recordings_add_delete(class_dict)
    except dataset_validator.ValidationException as e:
        raise api_exceptions.APIBadRequest(str(e))

    unique_mbids = list(set(class_dict["recordings"]))
    class_dict["recordings"] = unique_mbids

    try:
        db.dataset.delete_recordings(ds["id"], class_dict["class_name"], class_dict["recordings"])
    except db.exceptions.NoDataFoundException as e:
        # NoDataFoundException is raised if the class name doesn't exist in this dataset.
        # We treat this as a bad request, because it's based on data in the request body,
        # and not the url
        raise api_exceptions.APIBadRequest(str(e))

    return jsonify(
        success=True,
        message="Recordings deleted."
    )
예제 #2
0
def delete_recordings(dataset_id):
    """Delete recordings from a class in a dataset.

    **Example request**:

    .. sourcecode:: json

        {
            "class_name": "Happy",
            "recordings": ["770cc467-8dde-4d22-bc4c-a42f91e"]
        }

    :reqheader Content-Type: *application/json*
    :<json string class_name: *Required.* Name of the class.
    :<json array recordings: *Required.* Array of recoding MBIDs (``string``) that need be deleted from a class.

    :resheader Content-Type: *application/json*
    """
    ds = get_check_dataset(dataset_id, write=True)
    class_dict = request.get_json()
    try:
        dataset_validator.validate_recordings_add_delete(class_dict)
    except dataset_validator.ValidationException as e:
        raise api_exceptions.APIBadRequest(e.error)

    unique_mbids = list(set(class_dict["recordings"]))
    class_dict["recordings"] = unique_mbids

    try:
        db.dataset.delete_recordings(ds["id"], class_dict["class_name"],
                                     class_dict["recordings"])
    except db.exceptions.NoDataFoundException as e:
        # NoDataFoundException is raised if the class name doesn't exist in this dataset.
        # We treat this as a bad request, because it's based on data in the request body,
        # and not the url
        raise api_exceptions.APIBadRequest(str(e))

    return jsonify(success=True, message="Recordings deleted.")
    def test_recordings_add_delete(self):
        """ Validator for requests to add or delete recordings from a class """

        # not a dictionary
        with self.assertRaises(dataset_validator.ValidationException) as e:
            out = e
            dataset_validator.validate_recordings_add_delete("not_dictionary")
        self.assertEqual(out.exception.message,
                         "Request must be a dictionary.")

        # missing a required element
        with self.assertRaises(dataset_validator.ValidationException) as e:
            out = e
            dataset_validator.validate_recordings_add_delete(
                {"class_name": "Test"})
        self.assertEqual(
            out.exception.message,
            "Field `recordings` is missing from recordings dictionary.")

        with self.assertRaises(dataset_validator.ValidationException) as e:
            out = e
            dataset_validator.validate_recordings_add_delete({
                "class_name": "Test",
                "recordings": [],
                "other": None
            })
        self.assertEqual(out.exception.message,
                         "Unexpected field `other` in recordings dictionary.")

        # recordings not a list
        with self.assertRaises(dataset_validator.ValidationException) as e:
            out = e
            dataset_validator.validate_recordings_add_delete({
                "class_name":
                "Test",
                "recordings":
                "notlist"
            })
        self.assertEqual(out.exception.message,
                         'Field `recordings` in class "Test" is not a list.')

        # recording item not a uuid
        with self.assertRaises(dataset_validator.ValidationException) as e:
            out = e
            dataset_validator.validate_recordings_add_delete({
                "class_name": "Test",
                "recordings": [1]
            })
        self.assertEqual(out.exception.message,
                         '"1" is not a valid recording MBID in class "Test".')

        # all ok
        dataset_validator.validate_recordings_add_delete({
            "class_name":
            "Test",
            "recordings": ["cc355a8a-1cf0-4eda-a693-fd38dc1dd4e2"]
        })
    def test_recordings_add_delete(self):
        """ Validator for requests to add or delete recordings from a class """

        # not a dictionary
        with self.assertRaises(dataset_validator.ValidationException) as out:
            dataset_validator.validate_recordings_add_delete("not_dictionary")
        self.assertEqual(six.ensure_text(out.exception.error),
                         "Request must be a dictionary.")

        # missing a required element
        with self.assertRaises(dataset_validator.ValidationException) as out:
            dataset_validator.validate_recordings_add_delete(
                {"class_name": "Test"})
        self.assertEqual(
            six.ensure_text(out.exception.error),
            "Field `recordings` is missing from recordings dictionary.")

        with self.assertRaises(dataset_validator.ValidationException) as out:
            dataset_validator.validate_recordings_add_delete({
                "class_name": "Test",
                "recordings": [],
                "other": None
            })
        self.assertEqual(six.ensure_text(out.exception.error),
                         "Unexpected field `other` in recordings dictionary.")

        # recordings not a list
        with self.assertRaises(dataset_validator.ValidationException) as out:
            dataset_validator.validate_recordings_add_delete({
                "class_name":
                "Test",
                "recordings":
                "notlist"
            })
        self.assertEqual(six.ensure_text(out.exception.error),
                         'Field `recordings` in class "Test" is not a list.')

        # recording item not a uuid
        with self.assertRaises(dataset_validator.ValidationException) as out:
            dataset_validator.validate_recordings_add_delete({
                "class_name": "Test",
                "recordings": [1]
            })
        self.assertEqual(six.ensure_text(out.exception.error),
                         '"1" is not a valid recording MBID in class "Test".')

        # utf-8 characters in the uuid field
        with self.assertRaises(dataset_validator.ValidationException) as out:
            dataset_validator.validate_recordings_add_delete({
                "class_name":
                "Test",
                "recordings": [u"bé686320-8057-4ca2-b484-e01434a3a2b1"]
            })
        self.assertEqual(
            six.ensure_text(out.exception.error),
            u'"bé686320-8057-4ca2-b484-e01434a3a2b1" is not a valid recording MBID in class "Test".'
        )

        # all ok
        dataset_validator.validate_recordings_add_delete({
            "class_name":
            "Test",
            "recordings": ["cc355a8a-1cf0-4eda-a693-fd38dc1dd4e2"]
        })
    def test_recordings_add_delete(self):
        """ Validator for requests to add or delete recordings from a class """

        # not a dictionary
        with self.assertRaises(dataset_validator.ValidationException) as out:
            dataset_validator.validate_recordings_add_delete("not_dictionary")
        self.assertEqual(str(out.exception), "Request must be a dictionary.")

        # missing a required element
        with self.assertRaises(dataset_validator.ValidationException) as out:
            dataset_validator.validate_recordings_add_delete({"class_name": "Test"})
        self.assertEqual(str(out.exception), "Field `recordings` is missing from recordings dictionary.")

        with self.assertRaises(dataset_validator.ValidationException) as out:
            dataset_validator.validate_recordings_add_delete({"class_name": "Test", "recordings": [], "other": None})
        self.assertEqual(str(out.exception), "Unexpected field `other` in recordings dictionary.")

        # recordings not a list
        with self.assertRaises(dataset_validator.ValidationException) as out:
            dataset_validator.validate_recordings_add_delete({"class_name": "Test", "recordings": "notlist"})
        self.assertEqual(str(out.exception), 'Field `recordings` in class "Test" is not a list.')

        # recording item not a uuid
        with self.assertRaises(dataset_validator.ValidationException) as out:
            dataset_validator.validate_recordings_add_delete({"class_name": "Test", "recordings": [1]})
        self.assertEqual(str(out.exception), '"1" is not a valid recording MBID in class "Test".')

        # all ok
        dataset_validator.validate_recordings_add_delete({"class_name": "Test", "recordings": ["cc355a8a-1cf0-4eda-a693-fd38dc1dd4e2"]})