Updated portlet renderer settings
authorThierry Florac <tflorac@ulthar.net>
Fri, 04 Oct 2019 19:09:51 +0200
changeset 472 ebef14b037b3
parent 471 445f6edc944d
child 473 3e819a356396
Updated portlet renderer settings
src/pyams_default_theme/features/search/portlet/__init__.py
src/pyams_default_theme/features/search/portlet/interfaces.py
src/pyams_default_theme/features/search/portlet/templates/search-panel.pt
src/pyams_default_theme/shared/view/portlet/__init__.py
src/pyams_default_theme/shared/view/portlet/interfaces.py
src/pyams_default_theme/shared/view/portlet/templates/view-items-summary.pt
--- a/src/pyams_default_theme/features/search/portlet/__init__.py	Fri Oct 04 19:08:38 2019 +0200
+++ b/src/pyams_default_theme/features/search/portlet/__init__.py	Fri Oct 04 19:09:51 2019 +0200
@@ -22,7 +22,8 @@
 from pyams_default_theme.features.search.portlet.interfaces import ISearchResultHeader, \
     ISearchResultRenderer, ISearchResultTarget, ISearchResultTitle, \
     ISearchResultsPortletDefaultRendererSettings, ISearchResultsPortletPanelsRendererSettings, \
-    ISearchResultsPortletRendererBaseSettings
+    ISearchResultsPortletRendererBaseSettings, HIDDEN_HEADER_DISPLAY, START_HEADER_DISPLAY, \
+    FULL_HEADER_DISPLAY
 from pyams_default_theme.interfaces import ISearchResultsPanelView, ISearchResultsView
 from pyams_default_theme.shared.common.interfaces import ISharedContentHeadViewletManager
 from pyams_i18n.interfaces import II18n
@@ -33,6 +34,7 @@
 from pyams_template.template import template_config
 from pyams_utils.adapter import NullAdapter, adapter_config
 from pyams_utils.factory import factory_config
+from pyams_utils.text import get_text_start
 from pyams_utils.url import canonical_url, relative_url
 from pyams_viewlet.viewlet import ViewContentProvider, Viewlet, viewlet_config
 
@@ -119,6 +121,9 @@
 class SearchResultsPortletPanelsRendererSettings(SearchResultsPortletRendererBaseSettings):
     """Search results portlet panel renderer settings"""
 
+    header_display_mode = FieldProperty(
+        ISearchResultsPortletPanelsRendererSettings['header_display_mode'])
+    start_length = FieldProperty(ISearchResultsPortletPanelsRendererSettings['start_length'])
     button_title = FieldProperty(ISearchResultsPortletPanelsRendererSettings['button_title'])
 
 
@@ -190,8 +195,17 @@
 
     @property
     def header(self):
-        return self.request.registry.queryMultiAdapter((self.context, self.request, self.view),
-                                                       ISearchResultHeader)
+        display_mode = FULL_HEADER_DISPLAY
+        settings = self.view.renderer_settings
+        if ISearchResultsPortletPanelsRendererSettings.providedBy(settings):
+            display_mode = settings.header_display_mode
+        if display_mode == HIDDEN_HEADER_DISPLAY:
+            return ''
+        header = self.request.registry.queryMultiAdapter((self.context, self.request, self.view),
+                                                         ISearchResultHeader)
+        if display_mode == START_HEADER_DISPLAY:
+            header = get_text_start(header, settings.start_length)
+        return header
 
 
 @adapter_config(context=(IWfSharedContent, IPyAMSUserLayer, ISearchResultsPanelView),
--- a/src/pyams_default_theme/features/search/portlet/interfaces.py	Fri Oct 04 19:08:38 2019 +0200
+++ b/src/pyams_default_theme/features/search/portlet/interfaces.py	Fri Oct 04 19:09:51 2019 +0200
@@ -9,15 +9,19 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
+
+from collections import OrderedDict
+
+from zope.contentprovider.interfaces import IContentProvider
+from zope.interface import Attribute, Interface
+from zope.schema import Bool, Choice, Int
+from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
+
 from pyams_i18n.schema import I18nTextLineField
 
 
 __docformat__ = 'restructuredtext'
 
-from zope.contentprovider.interfaces import IContentProvider
-from zope.interface import Interface, Attribute
-from zope.schema import Bool
-
 from pyams_default_theme import _
 
 
@@ -64,9 +68,35 @@
     """Search results portlet default renderer settings interface"""
 
 
+FULL_HEADER_DISPLAY = 'full'
+START_HEADER_DISPLAY = 'start'
+HIDDEN_HEADER_DISPLAY = 'none'
+
+PANELS_HEADER_DISPLAY_MODES = OrderedDict((
+    (FULL_HEADER_DISPLAY, _("Display full header")),
+    (START_HEADER_DISPLAY, _("Display only header start")),
+    (HIDDEN_HEADER_DISPLAY, _("Hide header"))
+), )
+
+PANELS_HEADER_DISPLAY_MODES_VOCABULARY = SimpleVocabulary(
+    [SimpleTerm(k, title=v) for k, v in PANELS_HEADER_DISPLAY_MODES.items()])
+
+
 class ISearchResultsPortletPanelsRendererSettings(ISearchResultsPortletRendererBaseSettings):
     """Search results portlet panels renderer settings interface"""
 
+    header_display_mode = Choice(title=_("Header display mode"),
+                                 description=_("Defines how results headers will be rendered"),
+                                 required=True,
+                                 vocabulary=PANELS_HEADER_DISPLAY_MODES_VOCABULARY,
+                                 default=FULL_HEADER_DISPLAY)
+
+    start_length = Int(title=_("Start length"),
+                       description=_("If you choose to display only header start, you can specify "
+                                     "maximum text length"),
+                       required=True,
+                       default=120)
+
     button_title = I18nTextLineField(title=_("Button's title"),
                                      description=_("Navigation button's title is normally defined "
                                                    "based on target's content type; you can "
--- a/src/pyams_default_theme/features/search/portlet/templates/search-panel.pt	Fri Oct 04 19:08:38 2019 +0200
+++ b/src/pyams_default_theme/features/search/portlet/templates/search-panel.pt	Fri Oct 04 19:09:51 2019 +0200
@@ -20,7 +20,7 @@
 		<div class="header"
 			 tal:define="header view.header"
 			 tal:condition="header">
-			${structure:tales:html(header)}
+			${structure:header}
 		</div>
 		<div class="action"
 			 tal:condition="button_title">
--- a/src/pyams_default_theme/shared/view/portlet/__init__.py	Fri Oct 04 19:08:38 2019 +0200
+++ b/src/pyams_default_theme/shared/view/portlet/__init__.py	Fri Oct 04 19:09:51 2019 +0200
@@ -16,15 +16,18 @@
 from zope.schema.fieldproperty import FieldProperty
 
 from pyams_content.shared.view.portlet.interfaces import IViewItemsPortletSettings
+from pyams_default_theme.features.search.portlet import HIDDEN_HEADER_DISPLAY, START_HEADER_DISPLAY
 from pyams_default_theme.shared.view.portlet.interfaces import \
     IViewItemsPortletHorizontalRendererSettings, IViewItemsPortletSummaryRendererSettings, \
     IViewItemsPortletVerticalRendererSettings
+from pyams_i18n.interfaces import II18n
 from pyams_portal.interfaces import IPortalContext, IPortletRenderer
 from pyams_portal.portlet import PortletRenderer
 from pyams_skin.layer import IPyAMSLayer
 from pyams_template.template import template_config
 from pyams_utils.adapter import adapter_config
 from pyams_utils.factory import factory_config
+from pyams_utils.text import get_text_start
 from pyams_utils.url import canonical_url, relative_url
 
 
@@ -121,6 +124,9 @@
         IViewItemsPortletSummaryRendererSettings['thumbnails_selection'])
     paginate = FieldProperty(IViewItemsPortletSummaryRendererSettings['paginate'])
     page_size = FieldProperty(IViewItemsPortletSummaryRendererSettings['page_size'])
+    header_display_mode = FieldProperty(
+        IViewItemsPortletSummaryRendererSettings['header_display_mode'])
+    start_length = FieldProperty(IViewItemsPortletSummaryRendererSettings['start_length'])
 
 
 @adapter_config(name='summary',
@@ -134,3 +140,13 @@
     weight = 3
 
     settings_interface = IViewItemsPortletSummaryRendererSettings
+
+    def get_header(self, item):
+        settings = self.renderer_settings
+        display_mode = settings.header_display_mode
+        if display_mode == HIDDEN_HEADER_DISPLAY:
+            return ''
+        header = II18n(item).query_attribute('header', request=self.request)
+        if display_mode == START_HEADER_DISPLAY:
+            header = get_text_start(header, settings.start_length)
+        return header
--- a/src/pyams_default_theme/shared/view/portlet/interfaces.py	Fri Oct 04 19:08:38 2019 +0200
+++ b/src/pyams_default_theme/shared/view/portlet/interfaces.py	Fri Oct 04 19:09:51 2019 +0200
@@ -14,6 +14,10 @@
 from zope.schema import Bool, Choice, Int
 from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
 
+from pyams_default_theme.features.search.portlet import FULL_HEADER_DISPLAY
+from pyams_default_theme.features.search.portlet.interfaces import \
+    PANELS_HEADER_DISPLAY_MODES_VOCABULARY
+
 
 __docformat__ = 'restructuredtext'
 
@@ -101,3 +105,15 @@
                     description=_("Number of items per page, if pagination is enabled"),
                     required=False,
                     default=9)
+
+    header_display_mode = Choice(title=_("Header display mode"),
+                                 description=_("Defines how results headers will be rendered"),
+                                 required=True,
+                                 vocabulary=PANELS_HEADER_DISPLAY_MODES_VOCABULARY,
+                                 default=FULL_HEADER_DISPLAY)
+
+    start_length = Int(title=_("Start length"),
+                       description=_("If you choose to display only header start, you can specify "
+                                     "maximum text length"),
+                       required=True,
+                       default=120)
--- a/src/pyams_default_theme/shared/view/portlet/templates/view-items-summary.pt	Fri Oct 04 19:08:38 2019 +0200
+++ b/src/pyams_default_theme/shared/view/portlet/templates/view-items-summary.pt	Fri Oct 04 19:09:51 2019 +0200
@@ -28,8 +28,9 @@
 						<h3>${i18n:item.title}</h3>
 					</a>
 					<div class="header"
-						 tal:define="header i18n:item.header">
-						${structure:tales:html(header)}
+						 tal:define="header view.get_header(item)"
+						 tal:condition="header">
+						${structure:header}
 					</div>
 				</tal:var>
 			</div>