From 2b4650295704246184c38834355a95354d4fd666 Mon Sep 17 00:00:00 2001 From: Maurits van Rees Date: Wed, 22 Dec 2021 16:52:36 +0100 Subject: [PATCH] Add support for tiles in DefaultImageScalingFactory. plone.app.tiles has its [own image scale factory](https://github.com/plone/plone.app.tiles/blob/3.2.2/plone/app/tiles/imagescaling.py#L110). It [passes this around](https://github.com/plone/plone.app.tiles/blob/3.2.2/plone/app/tiles/imagescaling.py#L180) in a [deprecated way](https://github.com/plone/plone.scale/blob/3.1.2/plone/scale/storage.py#L207-L212). Its scale factory does almost the same as the DefaultImageScalingFactory, although it is structured differently, as a function instead of a class. Result is that the tiles scale factory is missing some features and fixes from the default, at least support for SVG and passing a quality parameter. The only *intended* difference of the tile scale factory, is that it gets the original field value from the [tile data](https://github.com/plone/plone.app.tiles/blob/3.2.2/plone/app/tiles/imagescaling.py#L114), instead of a [field on the context](https://github.com/plone/plone.namedfile/blob/5.5.1/plone/namedfile/scaling.py#L214). The current commit makes the DefaultImageScalingFactory work for tiles. Next step would be to update plone.app.tiles to use this. This mostly involves removing code. (Maybe something similar would be useful/needed for images in portlets, but I did not check. Do scales of images fields in portlets even work today?) This will also make future support for focal points easier: there will be just one scaling factory to fix, instead of two. --- news/104.feature | 2 ++ plone/namedfile/scaling.py | 43 ++++++++++++++++++++++++++++++++++++-- setup.py | 2 +- 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 news/104.feature diff --git a/news/104.feature b/news/104.feature new file mode 100644 index 00000000..66667f26 --- /dev/null +++ b/news/104.feature @@ -0,0 +1,2 @@ +Add support for tiles in DefaultImageScalingFactory. +[maurits] diff --git a/plone/namedfile/scaling.py b/plone/namedfile/scaling.py index d7cd7a4a..279c2e88 100644 --- a/plone/namedfile/scaling.py +++ b/plone/namedfile/scaling.py @@ -36,6 +36,11 @@ import six +try: + from plone.tiles.interfaces import IPersistentTile +except ImportError: + IPersistentTile = None + logger = logging.getLogger(__name__) _marker = object() @@ -186,6 +191,32 @@ def traverse(self, name, furtherPath): class DefaultImageScalingFactory(object): def __init__(self, context): self.context = context + # fieldname will be set for real in the __call__ method. + self.fieldname = None + if IPersistentTile is not None and IPersistentTile.providedBy(context): + self.is_tile = True + # We get data from the tile: + self.data_context = context.data + # This is the actual content item (Page, News Item, etc): + self.content_context = context.context + else: + self.is_tile = False + self.data_context = context + self.content_context = context + + def get_original_value(self): + if self.fieldname is None: + return + if self.is_tile: + return self.data_context.get(self.fieldname) + return getattr(self.content_context, self.fieldname, None) + + def url(self): + # The url is useful when logging a problem. + base = self.content_context.absolute_url() + if not self.is_tile: + return base + return "{}/@@{}/{}".format(base, self.context.__name__, self.context.id) def get_quality(self): """Get plone.app.imaging's quality setting""" @@ -211,7 +242,15 @@ def __call__( """Factory for image scales`. """ - orig_value = getattr(self.context, fieldname, None) + if fieldname is None: + primary = IPrimaryFieldInfo(self.context, None) + if primary is None: + return + fieldname = primary.fieldname + # Safe self.fieldname for use in self.get_original_value. + self.fieldname = fieldname + + orig_value = self.get_original_value() if orig_value is None: return @@ -258,7 +297,7 @@ def __call__( except Exception: logger.exception( 'Could not scale "{0!r}" of {1!r}'.format( - orig_value, self.context.absolute_url(), + orig_value, self.url(), ), ) return diff --git a/setup.py b/setup.py index 2fb94f25..d7732ff2 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ import os -version = '5.5.2.dev0' +version = '5.6.0.dev0' description = 'File types and fields for images, files and blob files with ' \ 'filenames'