Converted images galleries to medias galleries, allowing any media
authortflorac@xsup98-004.onf.fr
Wed, 07 Feb 2018 09:22:03 +0100
changeset 362 8fc21a7ef206
parent 361 68f6464a97ea
child 363 86d1a240d4dc
Converted images galleries to medias galleries, allowing any media
src/pyams_content/component/gallery/interfaces/__init__.py
src/pyams_content/component/gallery/zmi/__init__.py
src/pyams_content/component/gallery/zmi/interfaces.py
src/pyams_content/component/gallery/zmi/paragraph.py
src/pyams_content/component/gallery/zmi/templates/gallery-images.pt
--- a/src/pyams_content/component/gallery/interfaces/__init__.py	Wed Feb 07 09:20:50 2018 +0100
+++ b/src/pyams_content/component/gallery/interfaces/__init__.py	Wed Feb 07 09:22:03 2018 +0100
@@ -21,7 +21,7 @@
 from zope.contentprovider.interfaces import IContentProvider
 
 # import packages
-from pyams_file.schema import FileField, ImageField
+from pyams_file.schema import MediaField, AudioField
 from pyams_i18n.schema import I18nTextLineField, I18nTextField
 from zope.annotation.interfaces import IAttributeAnnotatable
 from zope.container.constraints import contains, containers
@@ -46,8 +46,8 @@
                                   description=_("Alternate title used to describe image content"),
                                   required=False)
 
-    data = ImageField(title=_("Image data"),
-                      description=_("Image content"),
+    data = MediaField(title=_("Image or video data"),
+                      description=_("Image or video content"),
                       required=True)
 
     description = I18nTextField(title=_("Description"),
@@ -65,9 +65,9 @@
                           description=_("Number used to identify media into it's original source"),
                           required=False)
 
-    sound = FileField(title=_("Audio data"),
-                      description=_("Sound file associated with the current media"),
-                      required=False)
+    sound = AudioField(title=_("Audio data"),
+                       description=_("Sound file associated with the current media"),
+                       required=False)
 
     sound_title = I18nTextLineField(title=_("Sound title"),
                                     description=_("Title of associated sound file"),
--- a/src/pyams_content/component/gallery/zmi/__init__.py	Wed Feb 07 09:20:50 2018 +0100
+++ b/src/pyams_content/component/gallery/zmi/__init__.py	Wed Feb 07 09:22:03 2018 +0100
@@ -9,7 +9,6 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
-from pyams_file.interfaces import IFileInfo
 
 __docformat__ = 'restructuredtext'
 
@@ -17,16 +16,19 @@
 # import standard library
 import json
 import zipfile
+
 from io import BytesIO
 
 # import interfaces
-from pyams_content.component.gallery.interfaces import IGallery, IGalleryRenderer, IGalleryFile
+from pyams_content.component.gallery.interfaces import IGallery, IGalleryRenderer
 from pyams_content.component.gallery.zmi.interfaces import IGalleryImageAddFields, IGalleryImagesView
 from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION
+from pyams_file.interfaces import IFileInfo
 from pyams_form.interfaces.form import IWidgetsPrefixViewletsManager
 from pyams_i18n.interfaces import II18n
 from pyams_skin.layer import IPyAMSLayer
 from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION
+from pyramid.interfaces import IView
 
 # import packages
 from pyams_content.shared.common.zmi import WfSharedContentPermissionMixin
@@ -34,6 +36,7 @@
 from pyams_pagelet.pagelet import pagelet_config
 from pyams_template.template import template_config, get_view_template
 from pyams_utils.adapter import adapter_config, ContextRequestAdapter
+from pyams_utils.url import absolute_url
 from pyams_viewlet.viewlet import viewlet_config, Viewlet
 from pyams_zmi.form import AdminDialogEditForm, AdminDialogDisplayForm
 from pyramid.exceptions import NotFound
@@ -122,6 +125,12 @@
     def get_title(self, image):
         return II18n(image).query_attribute('title', request=self.request)
 
+    def get_thumbnail_target(self, media):
+        registry = self.request.registry
+        view = registry.queryMultiAdapter((media, self.request), IView, name='preview.html')
+        if view is not None:
+            return absolute_url(media, self.request, 'preview.html')
+
 
 @view_config(name='set-images-order.json', context=IGallery, request_type=IPyAMSLayer,
              permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
--- a/src/pyams_content/component/gallery/zmi/interfaces.py	Wed Feb 07 09:20:50 2018 +0100
+++ b/src/pyams_content/component/gallery/zmi/interfaces.py	Wed Feb 07 09:22:03 2018 +0100
@@ -41,6 +41,6 @@
                                     description=_("Comments relatives to author's rights management"),
                                     required=False)
 
-    images_data = ImageField(title=_("Images data"),
+    images_data = ImageField(title=_("Images or videos data"),
                              description=_("You can upload a single file or choose to upload a whole ZIP archive"),
                              required=True)
--- a/src/pyams_content/component/gallery/zmi/paragraph.py	Wed Feb 07 09:20:50 2018 +0100
+++ b/src/pyams_content/component/gallery/zmi/paragraph.py	Wed Feb 07 09:22:03 2018 +0100
@@ -15,6 +15,8 @@
 
 # import standard library
 
+from datetime import datetime
+
 # import interfaces
 from pyams_content.component.gallery.interfaces import IGalleryParagraph, IBaseGallery, IGalleryRenderer
 from pyams_content.component.gallery.zmi.interfaces import IGalleryImagesView
@@ -26,6 +28,7 @@
 from pyams_i18n.interfaces import II18n
 from pyams_skin.interfaces.viewlet import IToolbarAddingMenu, IWidgetTitleViewletManager
 from pyams_skin.layer import IPyAMSLayer
+from pyramid.interfaces import IView
 from z3c.form.interfaces import INPUT_MODE
 
 # import packages
@@ -37,6 +40,7 @@
 from pyams_skin.viewlet.toolbar import ToolbarAction
 from pyams_template.template import template_config
 from pyams_utils.adapter import adapter_config
+from pyams_utils.url import absolute_url
 from pyams_viewlet.viewlet import viewlet_config, BaseContentProvider
 from pyams_zmi.form import AdminDialogAddForm, InnerAdminDisplayForm
 from pyramid.view import view_config
@@ -159,6 +163,12 @@
     def get_title(self, image):
         return II18n(image).query_attribute('title', request=self.request)
 
+    def get_thumbnail_target(self, media):
+        registry = self.request.registry
+        view = registry.queryMultiAdapter((media, self.request), IView, name='preview.html')
+        if view is not None:
+            return absolute_url(media, self.request, 'preview.html')
+
 
 @viewlet_config(name='add-image.menu', context=IGalleryParagraph, view=GalleryContentsView,
                 manager=IWidgetTitleViewletManager)
--- a/src/pyams_content/component/gallery/zmi/templates/gallery-images.pt	Wed Feb 07 09:20:50 2018 +0100
+++ b/src/pyams_content/component/gallery/zmi/templates/gallery-images.pt	Wed Feb 07 09:22:03 2018 +0100
@@ -33,25 +33,44 @@
 				 tal:attributes="data-ams-element-name image.__name__">
 				<tal:var define="thumbnails extension:thumbnails(image.data);">
 					<tal:if condition="thumbnails">
-						<a class="fancyimg hint" data-toggle
-						   data-ams-hint-gravity="sw"
-						   title="Zoom image" i18n:attributes="title"
-						   tal:define="target thumbnails.get_thumbnail('800x600')"
-						   tal:attributes="class 'fancyimg hint {0}'.format('not-visible' if not image.visible else '');
-										   href extension:absolute_url(target);
-										   rel string:gallery_${context.__name__};">
-							<img class="thumbnail"
-								 data-ams-hint-gravity="s"
-								 tal:define="thumbnail thumbnails.get_thumbnail('128x128');
-											 image_size thumbnail.get_image_size();
-											 margin_left 64 - image_size[0] / 2;
-											 margin_top 64 - image_size[1] / 2;
-											 title i18n:image.title;"
-								 tal:attributes="src extension:absolute_url(thumbnail);
-												 id 'image_{0}_{1}'.format(context.__name__, image.__name__);
-												 original-title title or '--';
-												 style string:margin-left: ${margin_left}px;; margin-right: ${margin_left}px;; margin-top: ${margin_top}px;; margin-bottom: ${margin_top}px;;" />
-						</a>
+						<tal:if condition="image.data.content_type.startswith('image/')">
+							<a class="fancyimg hint" data-toggle
+							   data-ams-hint-gravity="sw"
+							   title="Zoom image" i18n:attributes="title"
+							   tal:define="target thumbnails.get_thumbnail('800x600')"
+							   tal:attributes="class 'fancyimg hint {0}'.format('not-visible' if not image.visible else '');
+											   href extension:absolute_url(target);
+											   rel string:gallery_${context.__name__};">
+								<img class="thumbnail"
+									 data-ams-hint-gravity="s"
+									 tal:define="thumbnail thumbnails.get_thumbnail('128x128');
+												 image_size thumbnail.get_image_size();
+												 margin_left 64 - image_size[0] / 2;
+												 margin_top 64 - image_size[1] / 2;
+												 title i18n:image.title;"
+									 tal:attributes="src extension:absolute_url(thumbnail);
+													 id 'image_{0}_{1}'.format(context.__name__, image.__name__);
+													 original-title title or '--';
+													 style string:margin-left: ${margin_left}px;; margin-right: ${margin_left}px;; margin-top: ${margin_top}px;; margin-bottom: ${margin_top}px;;" />
+							</a>
+						</tal:if>
+						<tal:if condition="not:image.data.content_type.startswith('image/')">
+							<a tal:define="thumbnail thumbnails.get_thumbnail('128x128');
+										   target view.get_thumbnail_target(image.data);
+										   image_size thumbnail.get_image_size();
+										   margin_left 64 - image_size[0] / 2;
+										   margin_top 64 - image_size[1] / 2;
+										   title i18n:image.title;"
+							   tal:omit-tag="not:target"
+							   tal:attributes="href target" data-toggle="modal">
+								<img class="thumbnail no-border"
+									 data-ams-hint-gravity="s"
+									 tal:attributes="src extension:absolute_url(thumbnail);
+													 id 'media_{0}_{1}'.format(context.__name__, image.__name__);
+													 original-title title or '--';
+													 style string:margin-left: ${margin_left}px;; margin-right: ${margin_left}px;; margin-top: ${margin_top}px;; margin-bottom: ${margin_top}px;;" />
+							</a>
+						</tal:if>
 					</tal:if>
 					<tal:if condition="not:thumbnails">
 						<img class="thumbnail hint" src="/--static--/pyams_skin/img/mimetypes/unknown.png"