def _save_file(request, uploaded_file): filename = get_upload_filename(uploaded_file.name, request.user) img_name, img_format = os.path.splitext(filename) IMAGE_QUALITY = getattr(settings, "IMAGE_QUALITY", 60) MEDIA_ROOT = getattr(settings, "MEDIA_ROOT") allow_nonimages = getattr(settings, 'CKEDITOR_ALLOW_NONIMAGE_FILES', True) saved_path = default_storage.save(filename, uploaded_file) img_format = str(img_format).lower() if img_format == ".png": img = Image.open(uploaded_file) img = img.resize(calculate_size(img), Image.ANTIALIAS) img.save(os.path.join(MEDIA_ROOT, saved_path), quality=IMAGE_QUALITY, optimize=True) elif img_format == ".jpg" or img_format == ".jpeg": img = Image.open(uploaded_file) img = img.resize(calculate_size(img), Image.ANTIALIAS) img.save(os.path.join(MEDIA_ROOT, saved_path), quality=IMAGE_QUALITY, optimize=True) elif not allow_nonimages: raise TypeError('Invalid file type') return saved_path
def _save_file(request, uploaded_file): filename = get_upload_filename(uploaded_file.name, request.user) img_name, img_format = os.path.splitext(filename) IMAGE_QUALITY = getattr(settings, "IMAGE_QUALITY", 60) if str(img_format).lower() == "png": img = Image.open(uploaded_file) img = img.resize(img.size, Image.ANTIALIAS) saved_path = default_storage.save("{}.jpg".format(img_name), uploaded_file) img.save("{}.jpg".format(img_name), quality=IMAGE_QUALITY, optimize=True) elif str(img_format).lower() == "jpg" or str( img_format).lower() == "jpeg": img = Image.open(uploaded_file) img = img.resize(img.size, Image.ANTIALIAS) saved_path = default_storage.save(filename, uploaded_file) img.save(saved_path, quality=IMAGE_QUALITY, optimize=True) else: cloud_img = cloudinary.uploader.upload(uploaded_file) saved_path = cloud_img['url'] return saved_path
def test_no_request_parameter_deprecation_warning(self): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") generated_filename = get_upload_filename(MOCK_FILENAME, self.mock_request) self.assertEqual(len(w), 1) self.assertTrue(issubclass(w[0].category, PendingDeprecationWarning)) self.assertIn(GENERATED_MOCK_FILENAME, generated_filename)
def test_incompatible_parameters(self): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") generated_filename = get_upload_filename(MOCK_FILENAME, self.mock_request) self.assertEqual(len(w), 1) self.assertIn(MOCK_FILENAME, generated_filename) self.assertNotIn(GENERATOR_PREFIX, generated_filename)
def test_compatible_parameters(self): # `self.assertWarns()` is not available in Python 2 with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") generated_filename = get_upload_filename(MOCK_FILENAME, self.mock_request) self.assertEqual(len(w), 0) self.assertIn(GENERATED_MOCK_FILENAME, generated_filename)