Skip to content

Commit

Permalink
fixup! chore: Django 4.2 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
fdintino committed Sep 5, 2023
1 parent 919c229 commit 81d1693
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 29 deletions.
17 changes: 9 additions & 8 deletions cropduster/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions cropduster/standalone/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 2 additions & 1 deletion cropduster/standalone/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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())
16 changes: 3 additions & 13 deletions cropduster/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
2 changes: 0 additions & 2 deletions tests/standalone/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 81d1693

Please sign in to comment.