Updated thumbnails adapter to get thumbnail for a given selection in a single step
--- a/src/pyams_file/templates/picture.pt Wed Oct 24 09:25:11 2018 +0200
+++ b/src/pyams_file/templates/picture.pt Wed Oct 24 09:26:47 2018 +0200
@@ -1,11 +1,13 @@
<picture
class="${css_class}"
tal:define="image_url tales:absolute_url(image);
- timestamp tales:timestamp(image);">
+ thumbnails tales:thumbnails(image);">
<tal:if condition="xs_width">
<!-- xs source -->
<source media="(max-width: 767px)"
- tal:define="width round(768 / 12 * xs_width)"
+ tal:define="width str(round(768 / 12 * xs_width));
+ thumb thumbnails.get_thumbnail(xs_thumb + ':w' + width);
+ timestamp tales:timestamp(thumb);"
srcset="${image_url}/++thumb++${xs_thumb}:w${width}?_=${timestamp}" />
</tal:if>
<source media="(max-width: 767px)"
@@ -14,7 +16,9 @@
<tal:if condition="sm_width">
<!-- sm source -->
<source media="(max-width: 991px)"
- tal:define="width round(992 / 12 * sm_width)"
+ tal:define="width str(round(992 / 12 * sm_width));
+ thumb thumbnails.get_thumbnail(sm_thumb + ':w' + width);
+ timestamp tales:timestamp(thumb);"
srcset="${image_url}/++thumb++${sm_thumb}:w${width}?_=${timestamp}" />
</tal:if>
<source media="(max-width: 991px)"
@@ -23,7 +27,9 @@
<tal:if condition="md_width">
<!-- md source -->
<source media="(max-width: 1199px)"
- tal:define="width round(1200 / 12 * md_width)"
+ tal:define="width str(round(1200 / 12 * md_width));
+ thumb thumbnails.get_thumbnail(md_thumb + ':w' + width);
+ timestamp tales:timestamp(thumb);"
srcset="${image_url}/++thumb++${md_thumb}:w${width}?_=${timestamp}" />
</tal:if>
<source media="(max-width: 1199px)"
@@ -32,7 +38,9 @@
<tal:if condition="lg_width">
<!-- lg source -->
<source media="(min-width: 1200px)"
- tal:define="width round(1600 / 12 * lg_width)"
+ tal:define="width str(round(1600 / 12 * lg_width));
+ thumb thumbnails.get_thumbnail(lg_thumb + ':w' + width);
+ timestamp tales:timestamp(thumb);"
srcset="${image_url}/++thumb++${lg_thumb}:w${width}?_=${timestamp}" />
</tal:if>
<source media="(min-width: 1200px)"
@@ -40,5 +48,6 @@
srcset="/--static--/pyams_skin/img/dot.png" />
<!-- fallback image -->
<img style="width: 100%;"
+ tal:define="timestamp tales:timestamp(image)"
alt="${alt}" src="${image_url}?_=${timestamp}" />
</picture>
--- a/src/pyams_file/thumbnail.py Wed Oct 24 09:25:11 2018 +0200
+++ b/src/pyams_file/thumbnail.py Wed Oct 24 09:26:47 2018 +0200
@@ -12,29 +12,25 @@
__docformat__ = 'restructuredtext'
+import re
-# import standard library
-import re
import transaction
-
-# import interfaces
-from pyams_file.interfaces import IImage, IThumbnails, IThumbnailer, IFileModifiedEvent, IWatermarker, IThumbnailFile
-from pyams_utils.interfaces.tales import ITALESExtension
+from BTrees import OOBTree
+from persistent.dict import PersistentDict
+from pyramid.events import subscriber
+from pyramid.threadlocal import get_current_registry
+from zope.interface import Interface, alsoProvides
+from zope.lifecycleevent import ObjectAddedEvent, ObjectCreatedEvent, ObjectRemovedEvent
+from zope.location import locate
from zope.traversing.interfaces import ITraversable
-# import packages
-from BTrees import OOBTree
-from persistent.dict import PersistentDict
from pyams_file.file import FileFactory
+from pyams_file.interfaces import IFileModifiedEvent, IImage, IThumbnailFile, IThumbnailer, IThumbnails, IWatermarker
from pyams_file.zmi.image import render_image
from pyams_utils.adapter import ContextAdapter, ContextRequestViewAdapter, adapter_config, get_annotation_adapter
+from pyams_utils.interfaces.tales import ITALESExtension
from pyams_utils.registry import query_utility
from pyams_utils.request import check_request
-from pyramid.events import subscriber
-from pyramid.threadlocal import get_current_registry
-from zope.interface import alsoProvides, Interface
-from zope.lifecycleevent import ObjectAddedEvent, ObjectRemovedEvent, ObjectCreatedEvent
-from zope.location import locate
THUMBNAIL_ANNOTATIONS_KEY = 'pyams_file.image.thumbnails'
@@ -107,7 +103,7 @@
geometries[selection_name] = geometry
for current_thumbnail_name in list(self.thumbnails.keys()):
if (current_thumbnail_name == selection_name) or \
- current_thumbnail_name.startswith('{0}:'.format(selection_name)):
+ current_thumbnail_name.startswith('{0}:'.format(selection_name)):
self.delete_thumbnail(current_thumbnail_name)
def clear_geometries(self):
@@ -156,6 +152,13 @@
# check for existing thumbnail
if thumbnail_name in self.thumbnails:
return self.thumbnails[thumbnail_name]
+ # check for selection thumbnail, in "selection:size" format
+ if ':' in thumbnail_name:
+ selection_name, size = thumbnail_name.split(':', 1)
+ selection = self.get_selection(selection_name)
+ if selection is not None:
+ thumbnails = IThumbnails(selection)
+ return thumbnails.get_thumbnail(size)
# check for matching one
name, size = self.get_thumbnail_name(thumbnail_name, with_size=True)
if name: