Beispiel #1
0
 def test_new_file_exists(self, storage):
     file = storage.resource.get_file()
     uploader.explicit(
         file.name,
         type=file.resource.type,
         resource_type=file.resource.resource_type
     )
Beispiel #2
0
 def test_file_not_exists(self, storage):
     file_field = storage.resource.get_file_field()
     with pytest.raises(cloudinary.exceptions.Error):
         uploader.explicit(
             storage.old_source_name,
             type=file_field.type,
             resource_type=file_field.resource_type
         )
Beispiel #3
0
    def test_face_coordinates(self):
        """should allow sending face coordinates"""
        coordinates = [[120, 30, 109, 150], [121, 31, 110, 151]]
        result = uploader.upload("tests/logo.png", face_coordinates = coordinates, faces = True)
        self.assertEquals(coordinates, result["faces"])

        different_coordinates = [[122, 32, 111, 152]]
        uploader.explicit(result["public_id"], face_coordinates = different_coordinates, faces = True, type = "upload")
        info = api.resource(result["public_id"], faces = True)
        self.assertEquals(different_coordinates, info["faces"])
Beispiel #4
0
 def test_quality_override(self, mocker):
     """should pass quality_override """
     mocker.return_value = MOCK_RESPONSE
     test_values = ['auto:advanced', 'auto:best', '80:420', 'none']
     for quality in test_values:
         uploader.upload(TEST_IMAGE, tags=TEST_TAG, quality_override=quality)
         params = mocker.call_args[0][2]
         self.assertEqual(params['quality_override'], quality)
     # verify explicit works too
     uploader.explicit(TEST_IMAGE, quality_override='auto:best')
     params = mocker.call_args[0][2]
     self.assertEqual(params['quality_override'], 'auto:best')
Beispiel #5
0
 def test_quality_override(self, mocker):
     """should pass quality_override """
     mocker.return_value = MOCK_RESPONSE
     test_values = ['auto:advanced', 'auto:best', '80:420', 'none']
     for quality in test_values:
         uploader.upload(TEST_IMAGE, tags=TEST_TAG, quality_override=quality)
         params = mocker.call_args[0][2]
         self.assertEqual(params['quality_override'], quality)
     # verify explicit works too
     uploader.explicit(TEST_IMAGE, quality_override='auto:best')
     params = mocker.call_args[0][2]
     self.assertEqual(params['quality_override'], 'auto:best')
    def test_face_coordinates(self):
        """should allow sending face coordinates"""
        coordinates = [[120, 30, 109, 150], [121, 31, 110, 151]]
        result = uploader.upload(TEST_IMAGE, face_coordinates = coordinates, faces = True, tags=TEST_TAG)
        self.assertEqual(coordinates, result["faces"])

        different_coordinates = [[122, 32, 111, 152]]
        custom_coordinates = [1,2,3,4]
        uploader.explicit(result["public_id"], face_coordinates = different_coordinates, custom_coordinates = custom_coordinates, faces = True, type = "upload")
        info = api.resource(result["public_id"], faces = True, coordinates = True)
        self.assertEqual(different_coordinates, info["faces"])
        self.assertEqual({"faces": different_coordinates, "custom": [custom_coordinates]}, info["coordinates"])
    def test_accessibility_analysis(self, request_mock):
        """Should support accessibility analysis in upload and explicit"""
        request_mock.return_value = MOCK_RESPONSE

        uploader.upload(TEST_IMAGE, accessibility_analysis=True)

        params = get_params(request_mock.call_args[0])
        self.assertIn("accessibility_analysis", params)

        uploader.explicit(TEST_IMAGE, accessibility_analysis=True)

        params = get_params(request_mock.call_args[0])
        self.assertIn("accessibility_analysis", params)
    def test_cinemagraph_analysis(self, request_mock):
        """Should support cinemagraph analysis in upload and explicit"""
        request_mock.return_value = MOCK_RESPONSE

        uploader.upload(TEST_IMAGE, cinemagraph_analysis=True)

        params = request_mock.call_args[0][2]
        self.assertIn("cinemagraph_analysis", params)

        uploader.explicit(TEST_IMAGE, cinemagraph_analysis=True)

        params = request_mock.call_args[0][2]
        self.assertIn("cinemagraph_analysis", params)
Beispiel #9
0
    def test_face_coordinates(self):
        """should allow sending face coordinates"""
        coordinates = [[120, 30, 109, 150], [121, 31, 110, 151]]
        result_coordinates = [[120, 30, 109, 51], [121, 31, 110, 51]]
        result = uploader.upload(TEST_IMAGE, face_coordinates=coordinates, faces=True, tags=[UNIQUE_TAG])
        self.assertEqual(result_coordinates, result["faces"])

        different_coordinates = [[122, 32, 111, 152]]
        custom_coordinates = [1, 2, 3, 4]
        uploader.explicit(result["public_id"], face_coordinates=different_coordinates,
                          custom_coordinates=custom_coordinates, type="upload")
        info = api.resource(result["public_id"], faces=True, coordinates=True)
        self.assertEqual(different_coordinates, info["faces"])
        self.assertEqual({"faces": different_coordinates, "custom": [custom_coordinates]}, info["coordinates"])
    def test_eval_upload_parameter(self, request_mock):
        """Should support eval in upload and explicit"""
        request_mock.return_value = MOCK_RESPONSE

        uploader.upload(TEST_IMAGE, eval=EVAL_STR)

        params = get_params(request_mock.call_args[0])
        self.assertIn("eval", params)
        self.assertEqual(EVAL_STR, params["eval"])

        uploader.explicit(TEST_IMAGE, eval=EVAL_STR)

        params = get_params(request_mock.call_args[0])
        self.assertIn("eval", params)
        self.assertEqual(EVAL_STR, params["eval"])
Beispiel #11
0
    def test_face_coordinates(self):
        """should allow sending face coordinates"""
        coordinates = [[120, 30, 109, 150], [121, 31, 110, 151]]
        result = uploader.upload("tests/logo.png",
                                 face_coordinates=coordinates,
                                 faces=True)
        self.assertEquals(coordinates, result["faces"])

        different_coordinates = [[122, 32, 111, 152]]
        uploader.explicit(result["public_id"],
                          face_coordinates=different_coordinates,
                          faces=True,
                          type="upload")
        info = api.resource(result["public_id"], faces=True)
        self.assertEquals(different_coordinates, info["faces"])
Beispiel #12
0
    def test_responsive_breakpoints(self):
        result = uploader.upload(
            TEST_IMAGE,
            tags=[UNIQUE_TAG],
            responsive_breakpoints=dict(create_derived=False,
                                        transformation=dict(angle=90)))

        self.assertIsNotNone(result["responsive_breakpoints"])
        self.assertEqual(result["responsive_breakpoints"][0]["transformation"],
                         "a_90")

        result = uploader.explicit(
            result["public_id"],
            type="upload",
            tags=[UNIQUE_TAG],
            responsive_breakpoints=[dict(create_derived=False,
                                         transformation=dict(angle=90)),
                                    dict(create_derived=False,
                                         transformation=dict(angle=45))])

        self.assertIsNotNone(result["responsive_breakpoints"])
        self.assertEqual(result["responsive_breakpoints"][0]["transformation"],
                         "a_90")
        self.assertEqual(result["responsive_breakpoints"][1]["transformation"],
                         "a_45")
Beispiel #13
0
    def metadata(self):
        if self.resource.metadata is not None:
            return self.resource.metadata

        return uploader.explicit(self.public_id,
                                 type=self.resource.type,
                                 resource_type=self.resource.resource_type)
Beispiel #14
0
    def test_responsive_breakpoints(self):
        result = uploader.upload(
            TEST_IMAGE,
            tags=[UNIQUE_TAG],
            responsive_breakpoints=dict(create_derived=False,
                                        transformation=dict(angle=90)))

        self.assertIsNotNone(result["responsive_breakpoints"])
        self.assertEqual(result["responsive_breakpoints"][0]["transformation"],
                         "a_90")

        result = uploader.explicit(
            result["public_id"],
            type="upload",
            tags=[UNIQUE_TAG],
            responsive_breakpoints=[dict(create_derived=False,
                                         transformation=dict(angle=90)),
                                    dict(create_derived=False,
                                         transformation=dict(angle=45))])

        self.assertIsNotNone(result["responsive_breakpoints"])
        self.assertEqual(result["responsive_breakpoints"][0]["transformation"],
                         "a_90")
        self.assertEqual(result["responsive_breakpoints"][1]["transformation"],
                         "a_45")
Beispiel #15
0
 def test_explicit(self):
     """should support explicit """
     result = uploader.explicit("cloudinary", type="twitter_name", eager=[TEST_TRANS_SCALE2_PNG], tags=[UNIQUE_TAG])
     params = dict(TEST_TRANS_SCALE2_PNG, type="twitter_name", version=result["version"])
     url = utils.cloudinary_url("cloudinary", **params)[0]
     actual = result["eager"][0]["url"]
     self.assertEqual(parse_url(actual).path, parse_url(url).path)
Beispiel #16
0
    def file_exists(self) -> bool:
        file = self.get_file()
        if not file:
            return False

        file_field = self.get_file_field()

        try:
            uploader.explicit(
                self.name,
                type=file_field.type,
                resource_type=file_field.resource_type,
            )
        except cloudinary.exceptions.Error:
            return False
        return True
 def test_explicit(self):
     """should support explicit """
     result = uploader.explicit("cloudinary", type="twitter_name", eager=[dict(crop="scale", width="2.0")])
     url = utils.cloudinary_url(
         "cloudinary", type="twitter_name", crop="scale", width="2.0", format="png", version=result["version"]
     )[0]
     self.assertEqual(result["eager"][0]["url"], url)
 def test_explicit(self):
     """should support explicit """
     result = uploader.explicit("cloudinary", type="twitter_name", eager=[TEST_TRANS_SCALE2_PNG], tags=[UNIQUE_TAG])
     params = dict(TEST_TRANS_SCALE2_PNG, type="twitter_name", version=result["version"])
     url = utils.cloudinary_url("cloudinary", **params)[0]
     actual = result["eager"][0]["url"]
     self.assertEqual(parse_url(actual).path, parse_url(url).path)
Beispiel #19
0
 def test_explicit(self):
     """should support explicit """
     result = uploader.explicit("cloudinary", type="twitter_name",
                                eager=[dict(crop="scale", width="2.0")], tags=[UNIQUE_TAG])
     url = utils.cloudinary_url("cloudinary", type="twitter_name", crop="scale", width="2.0", format="png",
                                version=result["version"])[0]
     actual = result["eager"][0]["url"]
     self.assertEqual(parse_url(actual).path, parse_url(url).path)
 def test_explicit_with_metadata(self):
     """Explicit should support `metadata` parameter"""
     resource = uploader.upload(TEST_IMAGE, tags=[UNIQUE_TAG])
     result = uploader.explicit(resource['public_id'],
                                type="upload",
                                metadata=METADATA_FIELDS)
     self.assertEqual(METADATA_FIELD_VALUE,
                      result['metadata'][METADATA_FIELD_UNIQUE_EXTERNAL_ID])
Beispiel #21
0
 def test_explicit(self):
     """should support explicit """
     result = uploader.explicit("cloudinary", type="twitter_name",
                                eager=[dict(crop="scale", width="2.0")], tags=[UNIQUE_TAG])
     url = utils.cloudinary_url("cloudinary", type="twitter_name", crop="scale", width="2.0", format="png",
                                version=result["version"])[0]
     actual = result["eager"][0]["url"]
     self.assertEqual(parse_url(actual).path, parse_url(url).path)
Beispiel #22
0
 def test_explicit(self):
     """should support explicit """
     result = uploader.explicit("cloudinary", type="twitter_name", eager=[dict(crop="scale", width="2.0")])
     url = utils.cloudinary_url("cloudinary", type="twitter_name", crop="scale", width="2.0", format="png",
                                version=result["version"])[0]
     if result["eager"][0]["url"].startswith("/res/"):
         actual = result["eager"][0]["url"][4:]
     else:
         actual = result["eager"][0]["url"]
     self.assertEqual(actual, parse_url(url).path)
    def test_explicit_responsive_breakpoints_cache(self):
        """Should save responsive breakpoints to cache after explicit"""
        cache = responsive_breakpoints_cache.instance
        cache.set_cache_adapter(KeyValueCacheAdapter(DummyCacheStorage()))

        upload_result = uploader.upload(TEST_IMAGE)
        explicit_result = uploader.explicit(upload_result["public_id"], **self.rbp_params)

        cache_value = cache.get(explicit_result["public_id"], transformation=self.rbp_trans, format=self.rbp_format)

        self.assertEqual(self.rbp_values, cache_value)
Beispiel #24
0
    def test_explicit_responsive_breakpoints_cache(self):
        """Should save responsive breakpoints to cache after explicit"""
        cache = responsive_breakpoints_cache.instance
        cache.set_cache_adapter(KeyValueCacheAdapter(DummyCacheStorage()))

        upload_result = uploader.upload(TEST_IMAGE)
        explicit_result = uploader.explicit(upload_result["public_id"], **self.rbp_params)

        cache_value = cache.get(explicit_result["public_id"], transformation=self.rbp_trans, format=self.rbp_format)

        self.assertEqual(self.rbp_values, cache_value)
Beispiel #25
0
 def test_explicit(self):
     """should support explicit """
     result = uploader.explicit("cloudinary",
                                type="twitter_name",
                                eager=[dict(crop="scale", width="2.0")])
     url = utils.cloudinary_url("cloudinary",
                                type="twitter_name",
                                crop="scale",
                                width="2.0",
                                format="png",
                                version=result["version"])[0]
     self.assertEqual(result["eager"][0]["url"], url)
Beispiel #26
0
    def pre_validate(self, form):
        if self._is_uploaded_file(
                self.data) and not self.is_file_allowed(self.data.filename):
            raise ValidationError(gettext('Invalid file extension'))

        # Handle overwriting existing content
        if not self._is_uploaded_file(self.data):
            return

        if not self._allow_overwrite and 'public_id' in uploader.explicit(
                f'reports/{self.data.filename}', type='upload'):
            raise ValidationError(
                gettext('File "%s" already exists.' % self.data.filename))
    def test_explicit(self):
        """Should support explicit """
        result = uploader.explicit("cloudinary",
                                   type="twitter_name",
                                   eager=[TEST_TRANS_SCALE2_PNG],
                                   tags=[UNIQUE_TAG])
        params = dict(TEST_TRANS_SCALE2_PNG,
                      type="twitter_name",
                      version=result["version"])
        url = utils.cloudinary_url("cloudinary", **params)[0]
        actual = result["eager"][0]["url"]
        self.assertEqual(parse_url(actual).path, parse_url(url).path)

        # Test explicit with metadata
        resource = uploader.upload(TEST_IMAGE, tags=[UNIQUE_TAG])
        result_metadata = uploader.explicit(resource['public_id'],
                                            type="upload",
                                            metadata=METADATA_FIELDS,
                                            tags=[UNIQUE_TAG])
        self.assertIn(METADATA_FIELD_UNIQUE_EXTERNAL_ID,
                      result_metadata['metadata'])
        self.assertEqual(
            result_metadata['metadata'].get(METADATA_FIELD_UNIQUE_EXTERNAL_ID),
            METADATA_FIELD_VALUE)
Beispiel #28
0
    def test_quality_analysis(self):
        """ should return quality analysis information """
        result = uploader.upload(TEST_IMAGE,
                                 tags=[UNIQUE_TAG],
                                 quality_analysis=True)

        self.assertIn("quality_analysis", result)
        self.assertIn("focus", result["quality_analysis"])
        self.assertIsInstance(result["quality_analysis"]["focus"], float)

        result = uploader.explicit(result["public_id"],
                                   type="upload",
                                   tags=[UNIQUE_TAG],
                                   quality_analysis=True)

        self.assertIn("quality_analysis", result)
        self.assertIn("focus", result["quality_analysis"])
        self.assertIsInstance(result["quality_analysis"]["focus"], float)
    def test_quality_analysis(self):
        """ should return quality analysis information """
        result = uploader.upload(
            TEST_IMAGE,
            tags=[UNIQUE_TAG],
            quality_analysis=True)

        self.assertIn("quality_analysis", result)
        self.assertIn("focus", result["quality_analysis"])
        self.assertIsInstance(result["quality_analysis"]["focus"], float)

        result = uploader.explicit(
            result["public_id"],
            type="upload",
            tags=[UNIQUE_TAG],
            quality_analysis=True)

        self.assertIn("quality_analysis", result)
        self.assertIn("focus", result["quality_analysis"])
        self.assertIsInstance(result["quality_analysis"]["focus"], float)