Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get_size should always return size as tuple of int #111

Merged
merged 4 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions djangocms_picture/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,23 @@ def get_size(self, width=None, height=None):
if crop:
if not height and width:
if self.picture.width > self.picture.height:
height = int(width / PICTURE_RATIO)
height = width / PICTURE_RATIO
else:
height = int(width * PICTURE_RATIO)
height = width * PICTURE_RATIO

elif not width and height:
if self.picture.width > self.picture.height:
width = int(height * PICTURE_RATIO)
width = height * PICTURE_RATIO
else:
width = int(height / PICTURE_RATIO)
width = height / PICTURE_RATIO

width = width or self.picture.width
height = height or self.picture.height

# ensure width and height are int
fsbraun marked this conversation as resolved.
Show resolved Hide resolved
width = int(width) if width is not None else width
height = int(height) if height is not None else height

options = {
'size': (width, height),
'crop': crop,
Expand Down
11 changes: 11 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def setUp(self):
link_target=LINK_TARGET[0][0],
link_attributes="{'data-type', 'picture'}",
)

self.picture_portrait = Picture.objects.create(
template="default",
picture=get_filer_image(size=(600, 800)),
Expand All @@ -49,6 +50,11 @@ def setUp(self):
link_target=LINK_TARGET[0][0],
link_attributes="{'data-type', 'picture'}",
)

# Ensure picture.picture is the one loaded from DB and not the one given for object creation
self.picture.refresh_from_db()
self.picture_portrait.refresh_from_db()

self.external_picture = 'https://www.google.com/images/logo.png'

def tearDown(self):
Expand Down Expand Up @@ -120,6 +126,11 @@ def test_get_size(self):
instance.get_size(),
{"size": (800, 600), "crop": False, "upscale": False},
)
self.assertIsInstance(instance.get_size()["size"][0], int)
self.assertIsInstance(instance.get_size()["size"][1], int)
self.assertIsInstance(instance_portrait.get_size()["size"][0], int)
self.assertIsInstance(instance_portrait.get_size()["size"][1], int)

instance.use_crop = True
instance_portrait.use_crop = True
self.assertEqual(
Expand Down