From 81d169321f6446fe12a310bf5ba206d0b0d9d88f Mon Sep 17 00:00:00 2001 From: Frankie Dintino Date: Tue, 5 Sep 2023 12:10:57 -0400 Subject: [PATCH] fixup! chore: Django 4.2 compatibility --- cropduster/models.py | 17 +++++++++-------- cropduster/standalone/models.py | 3 +-- cropduster/standalone/views.py | 3 ++- cropduster/views/__init__.py | 16 +++------------- tests/settings.py | 6 ++++-- tests/standalone/test_admin.py | 2 -- tox.ini | 2 +- 7 files changed, 20 insertions(+), 29 deletions(-) diff --git a/cropduster/models.py b/cropduster/models.py index b2a1755a..08c8f570 100644 --- a/cropduster/models.py +++ b/cropduster/models.py @@ -259,8 +259,8 @@ def get_file_for_size(image, size_name='original', tmp=False): @classmethod def save_preview_file(cls, image_file, preview_w=None, preview_h=None): - with image_file as f: - f.open() + storage = image_file.storage + with storage.open(image_file.name, "rb") as f: pil_img = PIL.Image.open(BytesIO(f.read())) pil_img.filename = f.name orig_w, orig_h = pil_img.size @@ -376,8 +376,7 @@ def get_image_size(self, size_name=None): return (self.width, self.height) else: try: - with self.image as f: - f.open() + with self.image_file_open() as f: img = PIL.Image.open(BytesIO(f.read())) img.filename = f.name except (IOError, ValueError, TypeError): @@ -416,8 +415,7 @@ def save_size(self, size, thumb=None, image=None, tmp=False, standalone=False, raise Exception("Cannot save sizes without an image") if not image: - with self.image as f: - f.open() + with self.image_file_open() as f: image = PIL.Image.open(BytesIO(f.read())) image.filename = f.name @@ -496,10 +494,13 @@ def _save_standalone_thumb(self, size, image=None, thumb=None, commit=True): return thumb + def image_file_open(self): + storage = self._meta.get_field("image").storage + return storage.open(self.image.name, "rb") + def _save_thumb(self, size, image=None, thumb=None, ref_thumb=None, tmp=False, commit=True): if not image: - with self.image as f: - f.open() + with self.image_file_open() as f: image = PIL.Image.open(BytesIO(f.read())) image.filename = f.name if not thumb and self.pk: diff --git a/cropduster/standalone/models.py b/cropduster/standalone/models.py index 067af721..15ad4311 100644 --- a/cropduster/standalone/models.py +++ b/cropduster/standalone/models.py @@ -64,8 +64,7 @@ class Meta: def save(self, **kwargs): if not self.md5 and self.image: md5_hash = hashlib.md5() - with self.image.related_object.image as f: - f.open() + with self.image.related_object.image_file_open() as f: md5_hash.update(f.read()) self.md5 = md5_hash.hexdigest() super(StandaloneImage, self).save(**kwargs) diff --git a/cropduster/standalone/views.py b/cropduster/standalone/views.py index 8d63d498..5ca9b263 100644 --- a/cropduster/standalone/views.py +++ b/cropduster/standalone/views.py @@ -1,4 +1,5 @@ from django.utils.functional import cached_property +from django.views.decorators.clickjacking import xframe_options_exempt from cropduster.models import Size, Thumb from cropduster.standalone.metadata import MetadataImageFile @@ -81,4 +82,4 @@ def orig_image(self): return self.image_file.get_for_size('original') -index = CropDusterStandaloneIndex.as_view() +index = xframe_options_exempt(CropDusterStandaloneIndex.as_view()) diff --git a/cropduster/views/__init__.py b/cropduster/views/__init__.py index 04b8af48..38c2a3f9 100644 --- a/cropduster/views/__init__.py +++ b/cropduster/views/__init__.py @@ -47,15 +47,7 @@ from django.utils.encoding import force_str from django.utils.functional import cached_property from django.views.decorators.csrf import csrf_exempt -try: - from django.views.decorators.clickjacking import xframe_options_exempt -except ImportError: - def xframe_options_exempt(fn): - @functools.wraps(fn) - def wrapper(*args, **kwargs): - return fn(*args, **kwargs) - return wrapper - +from django.views.decorators.clickjacking import xframe_options_exempt import PIL.Image @@ -309,8 +301,7 @@ def fit_preview(im): data['crop']['orig_image'] = data['orig_image'] = cropduster_image.image.name data['url'] = cropduster_image.get_image_url('_preview') - with cropduster_image.image as f: - f.open() + with cropduster_image.image_file_open() as f: img = PIL.Image.open(BytesIO(f.read())) img.filename = f.name preview_file_path = cropduster_image.get_image_path('_preview') @@ -367,8 +358,7 @@ def crop(request): db_image = Image(image=crop_data['orig_image']) try: - with db_image.image as f: - f.open() + with db_image.image_file_open() as f: pil_image = PIL.Image.open(BytesIO(f.read())) pil_image.filename = f.name except IOError: diff --git a/tests/settings.py b/tests/settings.py index 1d261565..c12f37be 100755 --- a/tests/settings.py +++ b/tests/settings.py @@ -35,11 +35,13 @@ if os.environ.get('S3') == '1': if django.VERSION >= (4, 2): - STORAGES = {"default": "storages.backends.s3boto3.S3Boto3Storage"} + STORAGES = { + "default": {"BACKEND": "storages.backends.s3boto3.S3Boto3Storage"}, + "staticfiles": {"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage"}, + } else: DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage' AWS_STORAGE_BUCKET_NAME = 'ollie-cropduster-media-test-bucket-dev' - AWS_DEFAULT_ACL = 'public-read' AWS_LOCATION = 'cropduster/%s/' % uuid.uuid4().hex AWS_S3_SIGNATURE_VERSION = 's3v4' diff --git a/tests/standalone/test_admin.py b/tests/standalone/test_admin.py index dd94b95a..a20bb53e 100644 --- a/tests/standalone/test_admin.py +++ b/tests/standalone/test_admin.py @@ -56,8 +56,6 @@ def _post_teardown(self): self.ckeditor_override.disable() def setUp(self): - if django.VERSION >= (3, 2): - raise SkipTest("django-ckeditor not compatible with this version of Django") super(TestStandaloneAdmin, self).setUp() self.is_s3 = os.environ.get('S3') == '1' diff --git a/tox.ini b/tox.ini index 0fc63310..70b131e0 100644 --- a/tox.ini +++ b/tox.ini @@ -57,7 +57,7 @@ deps = dj32-grp: django-grappelli>=2.15,<2.16 dj40-grp: django-grappelli>=3.0,<3.1 lxml - -e git+https://github.com/theatlantic/django-ckeditor.git@v4.5.7+atl.8.2\#egg=django-ckeditor + -e git+https://github.com/theatlantic/django-ckeditor.git@v4.5.7+atl.8.4\#egg=django-ckeditor [testenv:coverage-report] skip_install = true