Added base previewer for contents using renderers
authorThierry Florac <thierry.florac@onf.fr>
Fri, 02 Mar 2018 15:02:15 +0100
changeset 430 3d44390d2ecd
parent 429 fafbf6bb246e
child 431 a162e9f348f7
Added base previewer for contents using renderers
src/pyams_content/features/renderer/zmi/__init__.py
--- a/src/pyams_content/features/renderer/zmi/__init__.py	Fri Mar 02 14:06:39 2018 +0100
+++ b/src/pyams_content/features/renderer/zmi/__init__.py	Fri Mar 02 15:02:15 2018 +0100
@@ -9,6 +9,7 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
+from pyams_i18n.interfaces import II18n
 
 __docformat__ = 'restructuredtext'
 
@@ -25,6 +26,7 @@
 from pyams_pagelet.pagelet import pagelet_config
 from pyams_template.template import get_view_template
 from pyams_utils.adapter import ContextRequestAdapter, adapter_config
+from pyams_viewlet.viewlet import BaseContentProvider
 from pyams_zmi.form import AdminDialogEditForm
 from pyramid.view import view_config
 from z3c.form import field
@@ -79,6 +81,36 @@
     """Renderer properties edit form, JSON renderer"""
 
 
+class BaseRendererPreview(BaseContentProvider):
+    """Base renderer preview"""
+
+    language = None
+    context_attrs = ()
+    i18n_context_attrs = ()
+
+    def __init__(self, context, request):
+        super(BaseRendererPreview, self).__init__(context, request)
+        self.renderer = self.context.get_renderer()
+
+    def update(self):
+        renderer = self.renderer
+        if renderer is not None:
+            renderer.language = self.language
+            for attr in self.context_attrs:
+                setattr(renderer, attr, getattr(self.context, attr, None))
+            if self.i18n_context_attrs:
+                i18n = II18n(self.context, None)
+                if i18n is not None:
+                    for attr in self.i18n_context_attrs:
+                        setattr(renderer, attr, i18n.get_attribute(attr, lang=self.language, request=self.request))
+            renderer.update()
+
+    def render(self):
+        if self.renderer is None:
+            return ''
+        return self.renderer.render()
+
+
 #
 # Default common renderers
 #