Skip to content

Commit 48baed1

Browse files
committed
Refactor ideal_size template tag to use explicit file existence checks
1 parent a6e95b5 commit 48baed1

2 files changed

Lines changed: 23 additions & 28 deletions

File tree

apps/sponsors/templatetags/sponsors.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,16 @@ def benefit_name_for_display(benefit, package):
8181
def ideal_size(image, ideal_dimension):
8282
"""Scale an image width to fit within the given ideal dimension area."""
8383
ideal_dimension = int(ideal_dimension)
84+
85+
# image could be an ImageFieldFile; if it's falsey, it means no file is associated with it.
86+
if not image:
87+
return ideal_dimension
88+
8489
try:
8590
w, h = image.width, image.height
86-
except (FileNotFoundError, ValueError) as e:
91+
except FileNotFoundError:
8792
# local dev doesn't have all images if DB is a copy from prod environment
8893
# in that case, we return a fallback size to avoid 500 errors.
89-
# we only catch the specific ValueError raised by Django when a file is missing.
90-
if isinstance(e, ValueError) and "no file associated with it" not in str(e):
91-
raise
9294
return ideal_dimension
9395

9496
return int(w * math.sqrt((100 * ideal_dimension) / (w * h)))

apps/sponsors/tests/test_templatetags.py

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -91,43 +91,36 @@ def test_display_name_for_display_from_benefit(self, mocked_name_for_display):
9191
mocked_name_for_display.assert_called_once_with(package=package)
9292

9393

94-
class IdealSizeTemplateTagTests(TestCase):
94+
def test_ideal_size_handles_missing_file_association(self):
95+
class MockImageWithoutFile:
96+
def __bool__(self):
97+
return False
98+
99+
size = ideal_size(MockImageWithoutFile(), 250)
100+
# Should return ideal_dimension directly as fallback
101+
self.assertEqual(size, 250)
102+
95103
def test_ideal_size_scales_properly(self):
96104
class MockImage:
97105
width = 400
98106
height = 200
99107

108+
def __bool__(self):
109+
return True
110+
100111
size = ideal_size(MockImage(), 200)
101112
# int(400 * sqrt(20000 / 80000)) = int(400 * 0.5) = 200
102113
self.assertEqual(size, 200)
103114

104115
def test_ideal_size_handles_file_not_found(self):
105-
class MockImageWithoutFile:
116+
class MockImageWithMissingFileOnDisk:
106117
@property
107118
def width(self):
108119
raise FileNotFoundError
109120

110-
size = ideal_size(MockImageWithoutFile(), 300)
111-
# Should return ideal_dimension directly as fallback
112-
self.assertEqual(size, 300)
113-
114-
def test_ideal_size_handles_value_error(self):
115-
class MockImageWithoutFileValue:
116-
@property
117-
def width(self):
118-
msg = "The 'web_logo' attribute has no file associated with it."
119-
raise ValueError(msg)
121+
def __bool__(self):
122+
return True
120123

121-
size = ideal_size(MockImageWithoutFileValue(), 250)
124+
size = ideal_size(MockImageWithMissingFileOnDisk(), 300)
122125
# Should return ideal_dimension directly as fallback
123-
self.assertEqual(size, 250)
124-
125-
def test_ideal_size_raises_other_value_errors(self):
126-
class MockImageWithOtherValueError:
127-
@property
128-
def width(self):
129-
msg = "Other error"
130-
raise ValueError(msg)
131-
132-
with self.assertRaises(ValueError):
133-
ideal_size(MockImageWithOtherValueError(), 250)
126+
self.assertEqual(size, 300)

0 commit comments

Comments
 (0)