Updated default renderer to be able to specify description formatter
authorThierry Florac <tflorac@ulthar.net>
Fri, 18 Jan 2019 15:30:29 +0100
changeset 382 f0c399c2d649
parent 381 1ab0b78f0480
child 383 02c3ce467bde
Updated default renderer to be able to specify description formatter
src/pyams_default_theme/component/association/__init__.py
src/pyams_default_theme/component/association/interfaces.py
src/pyams_default_theme/component/association/templates/association-default.pt
src/pyams_default_theme/component/association/templates/association-viewlet.pt
src/pyams_default_theme/component/association/viewlet.py
--- a/src/pyams_default_theme/component/association/__init__.py	Fri Jan 18 15:29:24 2019 +0100
+++ b/src/pyams_default_theme/component/association/__init__.py	Fri Jan 18 15:30:29 2019 +0100
@@ -25,8 +25,8 @@
 from pyams_content.component.paragraph.interfaces import IParagraphContainer, IParagraphContainerTarget
 from pyams_content.features.renderer.interfaces import IContentRenderer
 from pyams_default_theme import _
-from pyams_default_theme.component.association.interfaces import IAssociationParagraphRemoteContentRendererSettings, \
-    IAssociationParagraphSlicedRemoteContentRendererSettings
+from pyams_default_theme.component.association.interfaces import IAssociationParagraphDefaultRendererSettings, \
+    IAssociationParagraphRemoteContentRendererSettings, IAssociationParagraphSlicedRemoteContentRendererSettings
 from pyams_default_theme.features.renderer import BaseContentRenderer
 from pyams_skin.layer import IPyAMSLayer
 from pyams_template.template import template_config
@@ -38,6 +38,23 @@
 # Associations paragraph default renderer
 #
 
+ASSOCIATION_PARAGRAPH_DEFAULT_RENDERER_SETTINGS_KEY = 'pyams_content.association.renderer:default'
+
+
+@factory_config(IAssociationParagraphDefaultRendererSettings)
+class AssociationParagraphDefaultRendererSettings(Persistent, Location):
+    """Association paragraph default renderer settings"""
+
+    description_format = FieldProperty(IAssociationParagraphDefaultRendererSettings['description_format'])
+
+
+@adapter_config(context=IAssociationParagraph, provides=IAssociationParagraphDefaultRendererSettings)
+def association_paragraph_default_renderer_settings_factory(context):
+    """Associations paragraph default renderer settings factory"""
+    return get_annotation_adapter(context, ASSOCIATION_PARAGRAPH_DEFAULT_RENDERER_SETTINGS_KEY,
+                                  IAssociationParagraphDefaultRendererSettings)
+
+
 @adapter_config(name='default', context=(IAssociationParagraph, IPyAMSLayer), provides=IContentRenderer)
 @template_config(template='templates/association-default.pt', layer=IPyAMSLayer)
 class AssociationParagraphDefaultRenderer(BaseContentRenderer):
@@ -46,8 +63,12 @@
     label = _("Default associations renderer")
     i18n_context_attrs = ('title',)
 
-    links = None
-    attachments = None
+    settings_interface = IAssociationParagraphDefaultRendererSettings
+
+    def __init__(self, *args, **kwargs):
+        super(AssociationParagraphDefaultRenderer, self).__init__(*args, **kwargs)
+        self.attachments = []
+        self.links = []
 
     def get_associations(self):
         yield from IAssociationContainer(self.context).get_visible_items(self.request)
@@ -58,8 +79,6 @@
 
     def update(self):
         super().update()
-        self.attachments = []
-        self.links = []
         for item in self.get_associations():
             if IExtFile.providedBy(item):
                 self.attachments.append(item)
@@ -71,8 +90,10 @@
 # Associations paragraph remote content renderer
 #
 
-ASSOCIATION_PARAGRAPH_SLICED_REMOTE_CONTENT_RENDERER_SETTINGS_KEY = 'pyams_content.association.renderer:sliced-remote-content'
-ASSOCIATION_PARAGRAPH_REMOTE_CONTENT_RENDERER_SETTINGS_KEY = 'pyams_content.association.renderer:remote-content'
+ASSOCIATION_PARAGRAPH_SLICED_REMOTE_CONTENT_RENDERER_SETTINGS_KEY = \
+    'pyams_content.association.renderer:sliced-remote-content'
+ASSOCIATION_PARAGRAPH_REMOTE_CONTENT_RENDERER_SETTINGS_KEY = \
+    'pyams_content.association.renderer:remote-content'
 
 
 @factory_config(IAssociationParagraphSlicedRemoteContentRendererSettings)
--- a/src/pyams_default_theme/component/association/interfaces.py	Fri Jan 18 15:29:24 2019 +0100
+++ b/src/pyams_default_theme/component/association/interfaces.py	Fri Jan 18 15:30:29 2019 +0100
@@ -13,11 +13,21 @@
 __docformat__ = 'restructuredtext'
 
 from zope.interface import Interface
-from zope.schema import Bool, Set, Choice, Int
+from zope.schema import Bool, Choice, Int, Set
 
 from pyams_content.component.paragraph.interfaces import PARAGRAPH_FACTORIES_VOCABULARY
+from pyams_default_theme import _
+from pyams_utils.text import PYAMS_HTML_RENDERERS_VOCABULARY
 
-from pyams_default_theme import _
+
+class IAssociationParagraphDefaultRendererSettings(Interface):
+    """Associations paragraph default renderer settings interface"""
+
+    description_format = Choice(title=_("Description format"),
+                                description=_("Formatting style used for description in associations"),
+                                vocabulary=PYAMS_HTML_RENDERERS_VOCABULARY,
+                                required=True,
+                                default='text')
 
 
 class IBaseAssociationParagraphRemoteContentRendererSettings(Interface):
--- a/src/pyams_default_theme/component/association/templates/association-default.pt	Fri Jan 18 15:29:24 2019 +0100
+++ b/src/pyams_default_theme/component/association/templates/association-default.pt	Fri Jan 18 15:30:29 2019 +0100
@@ -1,2 +1,4 @@
-<h3 tal:content="view.title">§ title</h3>
-${structure:provider:pyams.associations}
+<tal:var define="settings view.settings">
+	<h3 tal:content="view.title">§ title</h3>
+	${structure:provider:pyams.associations(settings)}
+</tal:var>
\ No newline at end of file
--- a/src/pyams_default_theme/component/association/templates/association-viewlet.pt	Fri Jan 18 15:29:24 2019 +0100
+++ b/src/pyams_default_theme/component/association/templates/association-viewlet.pt	Fri Jan 18 15:30:29 2019 +0100
@@ -1,4 +1,5 @@
-<ul class="associations">
+<ul class="associations"
+	tal:define="renderer_style view.description_format + chr(59) + 'glossary'">
 	<li tal:define="prefix view.attachments_title_prefix"
 		tal:repeat="item view.attachments">
 		<tal:var define="item_info view.get_link_info(item);
@@ -6,8 +7,8 @@
 						 data i18n:item.data">
 			<p>
 				<tal:if condition="description">
-					${structure:tales:html(description, 'text;glossary')}
-					<br />
+					${structure:tales:html(description, renderer_style)}
+					<br tal:condition="view.description_format == 'text'" />
 				</tal:if>
 				<a tal:define="href_data tales:absolute_url(data);"
 				   href="${href_data}" target="_blank">
@@ -21,9 +22,9 @@
 						 description i18n:link.description;">
 			<p>
 				<tal:if condition="description">
-					${structure:tales:html(description, 'text;glossary')}
-					<br />
-				</tal:if
+					${structure:tales:html(description, renderer_style)}
+					<br tal:condition="view.description_format == 'text'" />
+				</tal:if>
 				<a tal:define="href link.get_url(request);
 							   target None if href.startswith(request.application_url) else '_blank'"
 				   href="${href}" target="${target}">
--- a/src/pyams_default_theme/component/association/viewlet.py	Fri Jan 18 15:29:24 2019 +0100
+++ b/src/pyams_default_theme/component/association/viewlet.py	Fri Jan 18 15:30:29 2019 +0100
@@ -14,11 +14,11 @@
 
 from zope.interface import Interface
 
-from pyams_skin.layer import IPyAMSUserLayer
 from pyams_content.component.association.interfaces import IAssociationContainer, IAssociationInfo
 from pyams_content.component.extfile.interfaces import IExtFile, IExtFileManagerInfo
 from pyams_content.component.links.interfaces import IBaseLink
 from pyams_i18n.interfaces import II18n
+from pyams_skin.layer import IPyAMSUserLayer
 from pyams_template.template import template_config
 from pyams_viewlet.viewlet import ViewContentProvider, contentprovider_config
 
@@ -28,6 +28,8 @@
 class AssociationContentProvider(ViewContentProvider):
     """Association default content provider"""
 
+    description_format = 'text'
+
     links = None
     attachments = None
 
@@ -43,8 +45,10 @@
     def get_link_info(link):
         return IAssociationInfo(link)
 
-    def update(self):
+    def update(self, settings=None):
         super().update()
+        if settings is not None:
+            self.description_format = settings.description_format
         for item in self.get_associations():
             if IExtFile.providedBy(item):
                 self.attachments.append(item)