Added vidoe renderer
authorThierry Florac <tflorac@ulthar.net>
Sun, 04 Mar 2018 16:36:30 +0100
changeset 439 2a61d39de0fc
parent 438 117089568313
child 440 b6554140ba7d
Added vidoe renderer
src/pyams_content/component/paragraph/interfaces/video.py
src/pyams_content/component/paragraph/video.py
src/pyams_content/component/paragraph/zmi/video.py
--- a/src/pyams_content/component/paragraph/interfaces/video.py	Fri Mar 02 17:54:01 2018 +0100
+++ b/src/pyams_content/component/paragraph/interfaces/video.py	Sun Mar 04 16:36:30 2018 +0100
@@ -17,11 +17,12 @@
 
 # import interfaces
 from pyams_content.component.paragraph.interfaces import IBaseParagraph
+from pyams_content.features.renderer.interfaces import IRenderedContent
 
 # import packages
 from pyams_file.schema import VideoField
 from pyams_i18n.schema import I18nHTMLField, I18nTextField
-from zope.schema import TextLine
+from zope.schema import TextLine, Choice
 
 from pyams_content import _
 
@@ -31,9 +32,10 @@
 #
 
 VIDEO_PARAGRAPH_TYPE = 'Video'
+VIDEO_PARAGRAPH_RENDERERS = 'PyAMS.paragraph.video.renderers'
 
 
-class IVideoParagraph(IBaseParagraph):
+class IVideoParagraph(IRenderedContent, IBaseParagraph):
     """Video paragraph"""
 
     body = I18nHTMLField(title=_("Body"),
@@ -50,3 +52,8 @@
     data = VideoField(title=_("Video data"),
                       description=_("Video file content"),
                       required=True)
+
+    renderer = Choice(title=_("Video template"),
+                      description=_("Presentation template used for this video"),
+                      vocabulary=VIDEO_PARAGRAPH_RENDERERS,
+                      default='default')
--- a/src/pyams_content/component/paragraph/video.py	Fri Mar 02 17:54:01 2018 +0100
+++ b/src/pyams_content/component/paragraph/video.py	Sun Mar 04 16:36:30 2018 +0100
@@ -19,7 +19,8 @@
 from pyams_content.component.extfile.interfaces import IExtFileContainerTarget
 from pyams_content.component.links.interfaces import ILinkContainerTarget
 from pyams_content.component.paragraph.interfaces import IParagraphFactory
-from pyams_content.component.paragraph.interfaces.video import IVideoParagraph, VIDEO_PARAGRAPH_TYPE
+from pyams_content.component.paragraph.interfaces.video import IVideoParagraph, VIDEO_PARAGRAPH_TYPE, \
+    VIDEO_PARAGRAPH_RENDERERS
 from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE
 from pyams_i18n.interfaces import II18nManager, INegotiator, II18n
 from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent
@@ -27,19 +28,23 @@
 # import packages
 from pyams_content.component.paragraph import BaseParagraph, BaseParagraphContentChecker, BaseParagraphFactory
 from pyams_content.component.paragraph.html import check_associations
+from pyams_content.features.renderer import RenderedContentMixin, IContentRenderer
 from pyams_file.property import FileProperty
 from pyams_utils.adapter import adapter_config
 from pyams_utils.registry import utility_config, get_utility
+from pyams_utils.request import check_request
 from pyams_utils.traversing import get_parent
+from pyams_utils.vocabulary import vocabulary_config
 from pyramid.events import subscriber
 from zope.interface import implementer
 from zope.schema.fieldproperty import FieldProperty
+from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
 
 from pyams_content import _
 
 
 @implementer(IVideoParagraph, IExtFileContainerTarget, ILinkContainerTarget)
-class VideoParagraph(BaseParagraph):
+class VideoParagraph(RenderedContentMixin, BaseParagraph):
     """Video paragraph class"""
 
     icon_class = 'fa-film'
@@ -49,6 +54,7 @@
     description = FieldProperty(IVideoParagraph['description'])
     author = FieldProperty(IVideoParagraph['author'])
     data = FileProperty(IVideoParagraph['data'])
+    renderer = FieldProperty(IVideoParagraph['renderer'])
 
 
 @utility_config(name=VIDEO_PARAGRAPH_TYPE, provides=IParagraphFactory)
@@ -102,3 +108,19 @@
             if not value:
                 output.append(translate(MISSING_VALUE).format(field=translate(IVideoParagraph[attr].title)))
         return output
+
+
+@vocabulary_config(name=VIDEO_PARAGRAPH_RENDERERS)
+class VideoParagraphRendererVocabulary(SimpleVocabulary):
+    """Video paragraph renderers vocabulary"""
+
+    def __init__(self, context=None):
+        request = check_request()
+        translate = request.localizer.translate
+        registry = request.registry
+        if not IVideoParagraph.providedBy(context):
+            context = VideoParagraph()
+        terms = [SimpleTerm(name, title=translate(adapter.label))
+                 for name, adapter in sorted(registry.getAdapters((context, request), IContentRenderer),
+                                             key=lambda x: x[1].weight)]
+        super(VideoParagraphRendererVocabulary, self).__init__(terms)
--- a/src/pyams_content/component/paragraph/zmi/video.py	Fri Mar 02 17:54:01 2018 +0100
+++ b/src/pyams_content/component/paragraph/zmi/video.py	Sun Mar 04 16:36:30 2018 +0100
@@ -23,7 +23,6 @@
 from pyams_content.component.paragraph.zmi.interfaces import IParagraphInnerEditor, IParagraphContainerView
 from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION
 from pyams_form.interfaces.form import IInnerForm, IEditFormButtons
-from pyams_i18n.interfaces import II18n
 from pyams_skin.interfaces.viewlet import IToolbarAddingMenu
 from pyams_skin.layer import IPyAMSLayer
 from pyams_zmi.interfaces import IPropertiesEditForm
@@ -35,9 +34,10 @@
 from pyams_content.component.paragraph.video import VideoParagraph
 from pyams_content.component.paragraph.zmi import BaseParagraphAJAXAddForm, BaseParagraphAJAXEditForm, \
     BaseParagraphAddMenu, BaseParagraphPropertiesEditForm
+from pyams_content.features.renderer.zmi import BaseRenderedContentPreview
+from pyams_content.features.renderer.zmi.widget import RendererFieldWidget
 from pyams_form.group import NamedWidgetsGroup
 from pyams_pagelet.pagelet import pagelet_config
-from pyams_template.template import template_config
 from pyams_utils.adapter import adapter_config
 from pyams_viewlet.viewlet import viewlet_config, BaseContentProvider
 from pyams_zmi.form import AdminDialogAddForm
@@ -85,7 +85,7 @@
                                              switch=True,
                                              hide_if_empty=True))
             self.add_group(NamedWidgetsGroup(self, 'data_group', self.widgets,
-                                             ('description', 'author', 'data'),
+                                             ('description', 'author', 'data', 'renderer'),
                                              bordered=False))
 
     def create(self, data):
@@ -111,6 +111,8 @@
     icon_css_class = 'fa fa-fw fa-film'
 
     fields = field.Fields(IVideoParagraph).omit('__parent__', '__name__', 'visible')
+    fields['renderer'].widgetFactory = RendererFieldWidget
+
     ajax_handler = 'properties.json'
     edit_permission = MANAGE_CONTENT_PERMISSION
 
@@ -128,7 +130,7 @@
                                              switch=True,
                                              hide_if_empty=True))
             self.add_group(NamedWidgetsGroup(self, 'data_group', self.widgets,
-                                             ('description', 'author', 'data'),
+                                             ('description', 'author', 'data', 'renderer'),
                                              bordered=False))
 
 
@@ -183,18 +185,21 @@
                             'object_name': associations_table.id,
                             'table': associations_table.render()}
             })
-        if 'data' in updated:
-            # we have to commit transaction to be able to handle blobs...
-            ITransactionManager(self.context).get().commit()
+        if ('data' in updated) or ('renderer' in updated):
+            if 'data' in updated:
+                # we have to commit transaction to be able to handle blobs...
+                ITransactionManager(self.context).get().commit()
             form = VideoParagraphPropertiesInnerEditForm(self.context, self.request)
             form.update()
             output.setdefault('events', []).append({
                 'event': 'myams.refresh',
-                'options': {'object_id': '{0}_{1}_{2}'.format(
-                                self.context.__class__.__name__,
-                                getattr(form.getContent(), '__name__', 'noname').replace('++', ''),
-                                form.id),
-                            'content': form.render()}
+                'options': {
+                    'object_id': '{0}_{1}_{2}'.format(
+                        self.context.__class__.__name__,
+                        getattr(form.getContent(), '__name__', 'noname').replace('++', ''),
+                        form.id),
+                    'content': form.render()
+                }
             })
         return output
 
@@ -204,17 +209,5 @@
 #
 
 @adapter_config(context=(IVideoParagraph, IPyAMSLayer), provides=IParagraphPreview)
-@template_config(template='templates/video-preview.pt', layer=IPyAMSLayer)
-class VideoParagraphPreview(BaseContentProvider):
+class VideoParagraphPreview(BaseRenderedContentPreview):
     """Video paragraph preview"""
-
-    language = None
-
-    def update(self):
-        i18n = II18n(self.context)
-        if self.language:
-            for attr in ('title', 'body', 'description'):
-                setattr(self, attr, i18n.get_attribute(attr, self.language, request=self.request))
-        else:
-            for attr in ('title', 'body', 'description'):
-                setattr(self, attr, i18n.query_attribute(attr, request=self.request))