Added options to logos paragraph renderer
authorThierry Florac <tflorac@ulthar.net>
Fri, 13 Nov 2020 13:30:18 +0100
changeset 532 717f4480b5c6
parent 531 d900ecb7c6f6
child 533 6ad33b7488b7
Added options to logos paragraph renderer
src/pyams_default_theme/shared/logo/__init__.py
src/pyams_default_theme/shared/logo/interfaces.py
src/pyams_default_theme/shared/logo/templates/logos-default.pt
--- a/src/pyams_default_theme/shared/logo/__init__.py	Fri Nov 13 13:29:12 2020 +0100
+++ b/src/pyams_default_theme/shared/logo/__init__.py	Fri Nov 13 13:30:18 2020 +0100
@@ -10,14 +10,24 @@
 # FOR A PARTICULAR PURPOSE.
 #
 
-__docformat__ = 'restructuredtext'
+from persistent import Persistent
+from zope.location import Location
+from zope.schema.fieldproperty import FieldProperty
 
 from pyams_content.features.renderer.interfaces import IContentRenderer
 from pyams_content.shared.logo.interfaces import ILogosParagraph
 from pyams_default_theme.features.renderer import BaseContentRenderer
+from pyams_default_theme.shared.logo.interfaces import DISABLED_LINK, \
+    ILogosParagraphRendererSettings, \
+    INTERNAL_FIRST
 from pyams_skin.layer import IPyAMSLayer
 from pyams_template.template import template_config
-from pyams_utils.adapter import adapter_config
+from pyams_utils.adapter import adapter_config, get_annotation_adapter
+from pyams_utils.factory import factory_config
+from pyams_utils.url import canonical_url, relative_url
+
+
+__docformat__ = 'restructuredtext'
 
 from pyams_default_theme import _
 
@@ -26,7 +36,28 @@
 # Logos paragraph default renderer
 #
 
-@adapter_config(name='default', context=(ILogosParagraph, IPyAMSLayer), provides=IContentRenderer)
+LOGOS_PARAGRAPH_RENDERER_SETTINGS_KEY = 'pyams_content.logos.renderer:default'
+
+
+@factory_config(ILogosParagraphRendererSettings)
+class LogosParagraphDefaultRendererSettings(Persistent, Location):
+    """Logos paragraph default renderer settings"""
+
+    target_priority = FieldProperty(ILogosParagraphRendererSettings['target_priority'])
+    force_canonical_url = FieldProperty(ILogosParagraphRendererSettings['force_canonical_url'])
+
+
+@adapter_config(context=ILogosParagraph,
+                provides=ILogosParagraphRendererSettings)
+def logos_paragraph_default_renderer_settings_factory(context):
+    """Logos paragraph default renderer settings factory"""
+    return get_annotation_adapter(context, LOGOS_PARAGRAPH_RENDERER_SETTINGS_KEY,
+                                  ILogosParagraphRendererSettings)
+
+
+@adapter_config(name='default',
+                context=(ILogosParagraph, IPyAMSLayer),
+                provides=IContentRenderer)
 @template_config(template='templates/logos-default.pt', layer=IPyAMSLayer)
 class LogosParagraphDefaultRenderer(BaseContentRenderer):
     """Logos paragraph default renderer"""
@@ -34,3 +65,32 @@
     label = _("Default logos renderer")
 
     i18n_context_attrs = ('title', )
+
+    settings_interface = ILogosParagraphRendererSettings
+
+    def get_internal_url(self, logo):
+        target = logo.target
+        if target is not None:
+            if self.settings.force_canonical_url:
+                url_getter = canonical_url
+            else:
+                url_getter = relative_url
+            return url_getter(target, request=self.request)
+        return None
+
+    @staticmethod
+    def get_external_url(logo):
+        return logo.url
+
+    def get_url(self, logo):
+        priority = self.settings.target_priority
+        if priority == DISABLED_LINK:
+            return None
+        order = [self.get_external_url, self.get_internal_url]
+        if priority == INTERNAL_FIRST:
+            order = reversed(order)
+        for getter in order:
+            result = getter(logo)
+            if result is not None:
+                return result
+        return None
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_default_theme/shared/logo/interfaces.py	Fri Nov 13 13:30:18 2020 +0100
@@ -0,0 +1,57 @@
+#
+# Copyright (c) 2015-2020 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+"""PyAMS_default_theme.shared.logo.skin interfaces module
+
+"""
+
+from zope.interface import Interface
+from zope.schema import Bool, Choice
+from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
+
+
+__docformat__ = 'restructuredtext'
+
+from pyams_default_theme import _
+
+
+DISABLED_LINK = 'disabled'
+INTERNAL_FIRST = 'internal'
+EXTERNAL_FIRST = 'external'
+
+TARGET_PRIORITY = (
+    {'id': DISABLED_LINK, 'title': _("Disabled link")},
+    {'id': INTERNAL_FIRST, 'title': _("Use internal reference first")},
+    {'id': EXTERNAL_FIRST, 'title': _("Use external URL first")}
+)
+
+TARGET_PRIORITY_VOCABULARY = SimpleVocabulary([SimpleTerm(item['id'], title=item['title'])
+                                               for item in TARGET_PRIORITY])
+
+
+class ILogosParagraphRendererSettings(Interface):
+    """Logos paragraph renderer settings"""
+
+    target_priority = Choice(title=_("Links priority"),
+                             description=_("Order in which internal and external links are "
+                                           "evaluated"),
+                             required=True,
+                             vocabulary=TARGET_PRIORITY_VOCABULARY,
+                             default=EXTERNAL_FIRST)
+
+    force_canonical_url = Bool(title=_("Force canonical URL"),
+                               description=_("By default, internal links use a \"relative\" URL, "
+                                             "which tries to display link target in the current "
+                                             "context; by using a canonical URL, you can display "
+                                             "target in it's attachment context (if defined)"),
+                               required=False,
+                               default=False)
--- a/src/pyams_default_theme/shared/logo/templates/logos-default.pt	Fri Nov 13 13:29:12 2020 +0100
+++ b/src/pyams_default_theme/shared/logo/templates/logos-default.pt	Fri Nov 13 13:30:18 2020 +0100
@@ -7,10 +7,15 @@
 	<tal:loop repeat="logo logos">
 		<div class="logo col-lg-2 col-md-2 col-sm-2 col-xs-3"
 			 tal:condition="logo is not None">
-			<a tal:omit-tag="not:logo.url"
-			   href="${logo.url}" title="${i18n:logo.title}">
-				${structure:tales:picture(logo.image, lg_thumb='square', lg_width=2, md_thumb='square', md_width=2,
-										  sm_thumb='square', sm_width=2, xs_thumb='square', xs_width=3, css_class='thumbnail')}
+			<a tal:define="url view.get_url(logo)"
+			   tal:omit-tag="not:url"
+			   href="${url}" title="${logo.get_title(request)}">
+				${structure:tales:picture(logo.image,
+										  lg_thumb='square', lg_width=2,
+										  md_thumb='square', md_width=2,
+										  sm_thumb='square', sm_width=2,
+										  xs_thumb='square', xs_width=3,
+										  css_class='thumbnail')}
 			</a>
 		</div>
 	</tal:loop>