Added method to use canonical or relative URL according to portlet settings
authorThierry Florac <tflorac@ulthar.net>
Wed, 23 Jan 2019 19:49:50 +0100
changeset 396 fcc70be2fc80
parent 395 54d4b7b7c996
child 397 b82fbbc89eec
Added method to use canonical or relative URL according to portlet settings
src/pyams_default_theme/shared/view/portlet/__init__.py
src/pyams_default_theme/shared/view/portlet/templates/view-items-horiz-list.pt
src/pyams_default_theme/shared/view/portlet/templates/view-items-list.pt
--- a/src/pyams_default_theme/shared/view/portlet/__init__.py	Wed Jan 23 18:46:33 2019 +0100
+++ b/src/pyams_default_theme/shared/view/portlet/__init__.py	Wed Jan 23 19:49:50 2019 +0100
@@ -18,16 +18,26 @@
 from zope.schema.fieldproperty import FieldProperty
 
 from pyams_content.shared.view.portlet.interfaces import IViewItemsPortletSettings
-from pyams_default_theme.shared.view.portlet.interfaces import IViewItemsPortletVerticalRendererSettings, \
-    IViewItemsPortletHorizontalRendererSettings
+from pyams_default_theme import _
+from pyams_default_theme.shared.view.portlet.interfaces import IViewItemsPortletHorizontalRendererSettings, \
+    IViewItemsPortletVerticalRendererSettings
 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.url import canonical_url, relative_url
 
-from pyams_default_theme import _
+
+class BaseViewItemsPortletRenderer(PortletRenderer):
+    """Base view items portlet renderer"""
+
+    def get_url(self, target):
+        if self.settings.force_canonical_url:
+            return canonical_url(target, self.request)
+        else:
+            return relative_url(target, self.request)
 
 
 #
@@ -49,7 +59,7 @@
 @adapter_config(context=(IPortalContext, IPyAMSLayer, Interface, IViewItemsPortletSettings),
                 provides=IPortletRenderer)
 @template_config(template='templates/view-items-list.pt', layer=IPyAMSLayer)
-class ViewItemsPortletVerticalRenderer(PortletRenderer):
+class ViewItemsPortletVerticalRenderer(BaseViewItemsPortletRenderer):
     """View items portlet vertical renderer"""
 
     label = _("Simple vertical view")
@@ -73,7 +83,7 @@
                 context=(IPortalContext, IPyAMSLayer, Interface, IViewItemsPortletSettings),
                 provides=IPortletRenderer)
 @template_config(template='templates/view-items-horiz-list.pt', layer=IPyAMSLayer)
-class ViewItemsPortletHorizontalRenderer(PortletRenderer):
+class ViewItemsPortletHorizontalRenderer(BaseViewItemsPortletRenderer):
     """View items portlet horizontal renderer"""
 
     label = _("Simple horizontal thumbnails view")
--- a/src/pyams_default_theme/shared/view/portlet/templates/view-items-horiz-list.pt	Wed Jan 23 18:46:33 2019 +0100
+++ b/src/pyams_default_theme/shared/view/portlet/templates/view-items-horiz-list.pt	Wed Jan 23 19:49:50 2019 +0100
@@ -12,7 +12,7 @@
 		<tal:loop repeat="item items">
 			<div class="thumbnail col-lg-${lg_width} col-md-${md_width} col-sm-${sm_width} col-xs-${xs_width}"
 				 tal:define="illustration tales:pyams_illustration(item);
-							 target tales:relative_url(item);"
+							 target view.get_url(item);"
 				 tal:condition="illustration">
 				<a href="${target}">
 					<tal:if define="image i18n:illustration.data;
--- a/src/pyams_default_theme/shared/view/portlet/templates/view-items-list.pt	Wed Jan 23 18:46:33 2019 +0100
+++ b/src/pyams_default_theme/shared/view/portlet/templates/view-items-list.pt	Wed Jan 23 19:49:50 2019 +0100
@@ -14,7 +14,7 @@
 					 xs_width 12 if selection == 'pano' else 2;">
 		<tal:loop repeat="item items">
 			<div tal:define="global count count +1;
-							 target tales:relative_url(item);">
+							 target view.get_url(item);">
 				<tal:if condition="renderer_settings.display_illustrations">
 					<div class="thumbnail pull-left col-lg-3 col-md-3 col-sm-4 hidden-xs"
 						 tal:define="illustration tales:pyams_illustration(item)"