Skip to content

Commit

Permalink
Merge pull request #129 from plone/maurits-custom-title
Browse files Browse the repository at this point in the history
Get title from ImageScale class.
  • Loading branch information
mauritsvanrees authored Jul 14, 2022
2 parents 9514416 + 1f14d31 commit f851f65
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
4 changes: 4 additions & 0 deletions news/128.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Get title from ImageScale class.
Prevents a traceback when the context is a tile.
When no title can we found, fall back to an empty string.
[maurits]
22 changes: 20 additions & 2 deletions plone/namedfile/scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ def srcset_attribute(self):
srcset_attr = ", ".join(_srcset_attr)
return srcset_attr

@property
def title(self):
"""Get the title from the context.
Let's not fail when we cannot find a title.
"""
try:
# Most Plone content items.
return self.context.Title()
except AttributeError:
pass
try:
# Can work on a tile and most other things.
return self.context.title
except AttributeError:
pass
return ""

def tag(
self,
height=_marker,
Expand All @@ -123,9 +141,9 @@ def tag(
width = getattr(self, "width", self.data._width)

if alt is _marker:
alt = self.context.Title()
alt = self.title
if title is _marker:
title = self.context.Title()
title = self.title

values = [
("src", self.url),
Expand Down
30 changes: 30 additions & 0 deletions plone/namedfile/tests/test_scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from plone.namedfile.file import NamedImage
from plone.namedfile.interfaces import IAvailableSizes
from plone.namedfile.interfaces import IImageScaleTraversable
from plone.namedfile.scaling import ImageScale
from plone.namedfile.scaling import ImageScaling
from plone.namedfile.testing import PLONE_NAMEDFILE_FUNCTIONAL_TESTING
from plone.namedfile.testing import PLONE_NAMEDFILE_INTEGRATION_TESTING
Expand Down Expand Up @@ -296,6 +297,13 @@ def get_info_by_hash(self, hash):
return value


class TitleImageScale(ImageScale):
"""ImageScale class with its own title property.
"""

title = "title from class"


# @patch.multiple(
# "plone.namedfile.scaling.Img2PictureTag",
# allowed_scales=patch_Img2PictureTag_allowed_scales,
Expand Down Expand Up @@ -926,6 +934,28 @@ def test_height(self):
self.assertEqual(scale_uid.data.info["uid"], "uid-0")
self.assertIs(scale_uid.data, scale2.data)

def test_title(self):
# Test that a custom title property on an ImageScale class is used.
item = FakeImage("abcdef", "jpeg")
scaling = ImageScaling(item, None)
scaling._scale_view_class = TitleImageScale
self.assertEqual(
scaling.tag("image"),
'<img src="http://fake.image/@@images/image.jpeg" alt="title from class" title="title from class" height="4" width="6" />'
)
self.assertEqual(
scaling.tag("image", alt="own alt"),
'<img src="http://fake.image/@@images/image.jpeg" alt="own alt" title="title from class" height="4" width="6" />'
)
self.assertEqual(
scaling.tag("image", title="own title"),
'<img src="http://fake.image/@@images/image.jpeg" alt="title from class" title="own title" height="4" width="6" />'
)
self.assertEqual(
scaling.tag("image", alt="own alt", title="own title"),
'<img src="http://fake.image/@@images/image.jpeg" alt="own alt" title="own title" height="4" width="6" />'
)


def test_suite():
from unittest import defaultTestLoader
Expand Down

0 comments on commit f851f65

Please sign in to comment.