Added interface and default adapter to render search results
authorThierry Florac <tflorac@ulthar.net>
Fri, 21 Dec 2018 13:57:53 +0100
changeset 299 66ef165934a9
parent 298 2e28f2f2a3d0
child 300 f20595749ba6
Added interface and default adapter to render search results
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-result.pt
src/pyams_default_theme/features/search/portlet/templates/search-results.pt
--- a/src/pyams_default_theme/features/search/portlet/__init__.py	Wed Dec 19 16:56:36 2018 +0100
+++ b/src/pyams_default_theme/features/search/portlet/__init__.py	Fri Dec 21 13:57:53 2018 +0100
@@ -19,8 +19,10 @@
 from zope.schema.fieldproperty import FieldProperty
 
 from pyams_content.features.search.portlet import ISearchResultsPortletSettings
+from pyams_content.shared.common import IWfSharedContent
 from pyams_default_theme import _
-from pyams_default_theme.features.search.portlet.interfaces import ISearchResultsPortletDefaultRendererSettings
+from pyams_default_theme.features.search.portlet.interfaces import ISearchResultRenderer, \
+    ISearchResultsPortletDefaultRendererSettings
 from pyams_default_theme.interfaces import ISearchResultsView
 from pyams_portal.interfaces import IPortalContext, IPortletRenderer
 from pyams_portal.portlet import PortletRenderer
@@ -30,6 +32,7 @@
 from pyams_utils.adapter import NullAdapter, adapter_config
 from pyams_utils.factory import factory_config
 from pyams_utils.url import canonical_url, relative_url
+from pyams_viewlet.viewlet import ViewContentProvider
 
 
 @factory_config(provided=ISearchResultsPortletDefaultRendererSettings)
@@ -58,13 +61,27 @@
             self.request.GET['length'] = '999'
         super(SearchResultsPortletDefaultRenderer, self).update()
 
-    def get_url(self, target):
-        if self.settings.force_canonical_url:
-            return canonical_url(target, self.request)
+    def render_item(self, item):
+        renderer = self.request.registry.queryMultiAdapter((item, self.request, self), ISearchResultRenderer)
+        if renderer is not None:
+            renderer.update()
+            return renderer.render()
         else:
-            return relative_url(target, self.request)
+            return ''
 
 
 @adapter_config(context=(ILocation, IPyAMSUserLayer, ISearchResultsView), provides=IBreadcrumbs)
 class BreadcrumbsAdapter(NullAdapter):
     """Disable breadcrumbs in search results view"""
+
+
+@adapter_config(context=(IWfSharedContent, IPyAMSUserLayer, ISearchResultsView), provides=ISearchResultRenderer)
+@template_config(template='templates/search-result.pt', layer=IPyAMSUserLayer)
+class WfSharedContentSearchResultRenderer(ViewContentProvider):
+    """Shared content search result renderer"""
+
+    def get_url(self, target):
+        if self.view.settings.force_canonical_url:
+            return canonical_url(target, self.request)
+        else:
+            return relative_url(target, self.request)
--- a/src/pyams_default_theme/features/search/portlet/interfaces.py	Wed Dec 19 16:56:36 2018 +0100
+++ b/src/pyams_default_theme/features/search/portlet/interfaces.py	Fri Dec 21 13:57:53 2018 +0100
@@ -12,12 +12,17 @@
 
 __docformat__ = 'restructuredtext'
 
+from zope.contentprovider.interfaces import IContentProvider
 from zope.interface import Interface
 from zope.schema import Bool
 
 from pyams_default_theme import _
 
 
+class ISearchResultRenderer(IContentProvider):
+    """Search result renderer interface"""
+
+
 class ISearchResultsPortletDefaultRendererSettings(Interface):
     """Search results portlet default renderer settings interface"""
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_default_theme/features/search/portlet/templates/search-result.pt	Fri Dec 21 13:57:53 2018 +0100
@@ -0,0 +1,26 @@
+<tal:var define="target view.get_url(context)"
+		 i18n:domain="pyams_default_theme">
+	<div class="thumbnail pull-left col-lg-3 col-md-3 col-sm-4 hidden-xs"
+		 tal:define="illustration tales:pyams_illustration(context)"
+		 tal:condition="illustration">
+		<a href="${target}">
+			<tal:if define="image i18n:illustration.data;
+							alt i18n:illustration.alt_title;"
+					condition="image">
+				${structure:tales:picture(image, lg_thumb='pano', lg_width=3, md_thumb='pano', md_width=3, sm_thumb='pano',
+										  sm_width=4, xs_thumb='pano', xs_width=12, alt=alt, css_class='result_media')}
+			</tal:if>
+		</a>
+	</div>
+	<div>
+		<a href="${target}">
+			<h3>${i18n:context.title}</h3>
+		</a>
+		<div class="breadcrumbs">${structure:tales:breadcrumbs(context)}</div>
+		<div class="tags">${structure:tales:tags(context)}</div>
+		<div class="header"
+			 tal:define="header i18n:context.header">
+			${structure:tales:html(header)}
+		</div>
+	</div>
+</tal:var>
--- a/src/pyams_default_theme/features/search/portlet/templates/search-results.pt	Wed Dec 19 16:56:36 2018 +0100
+++ b/src/pyams_default_theme/features/search/portlet/templates/search-results.pt	Fri Dec 21 13:57:53 2018 +0100
@@ -18,31 +18,7 @@
 			<hr />
 		</header>
 		<tal:loop repeat="item items">
-			<tal:var define="target view.get_url(item)">
-				<div class="thumbnail pull-left col-lg-3 col-md-3 col-sm-4 hidden-xs"
-					 tal:define="illustration tales:pyams_illustration(item)"
-					 tal:condition="illustration">
-					<a href="${target}">
-						<tal:if define="image i18n:illustration.data;
-										alt i18n:illustration.alt_title;"
-								condition="image">
-							${structure:tales:picture(image, lg_thumb='pano', lg_width=3, md_thumb='pano', md_width=3, sm_thumb='pano',
-													  sm_width=4, xs_thumb='pano', xs_width=12, alt=alt, css_class='result_media')}
-						</tal:if>
-					</a>
-				</div>
-				<div>
-					<a href="${target}">
-						<h3>${i18n:item.title}</h3>
-					</a>
-					<div class="breadcrumbs">${structure:tales:breadcrumbs(item)}</div>
-					<div class="tags">${structure:tales:tags(item)}</div>
-					<div class="header"
-						 tal:define="header i18n:item.header">
-						${structure:tales:html(header)}
-					</div>
-				</div>
-			</tal:var>
+			${structure:view.render_item(item)}
 			<div class="clearfix"></div>
 		</tal:loop>
 	</div>