Skip to content

Commit

Permalink
Add support for tiles in DefaultImageScalingFactory.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mauritsvanrees committed Dec 22, 2021
1 parent fcf666e commit 2b46502
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
2 changes: 2 additions & 0 deletions news/104.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add support for tiles in DefaultImageScalingFactory.
[maurits]
43 changes: 41 additions & 2 deletions plone/namedfile/scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
import six


try:
from plone.tiles.interfaces import IPersistentTile
except ImportError:
IPersistentTile = None

logger = logging.getLogger(__name__)
_marker = object()

Expand Down Expand Up @@ -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"""
Expand All @@ -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

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

0 comments on commit 2b46502

Please sign in to comment.