Added "pyams.tags" content provider
authorThierry Florac <tflorac@ulthar.net>
Wed, 12 Sep 2018 18:12:22 +0200
changeset 145 b4ba3762cbd6
parent 144 7182af4510e6
child 146 3cdc2e89fb1c
Added "pyams.tags" content provider
src/pyams_default_theme/viewlet/tag/__init__.py
src/pyams_default_theme/viewlet/tag/templates/tags.pt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_default_theme/viewlet/tag/__init__.py	Wed Sep 12 18:12:22 2018 +0200
@@ -0,0 +1,44 @@
+#
+# Copyright (c) 2008-2018 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.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_content.component.theme import ITagsTarget, ITagsInfo, ITagsManager
+from pyams_skin.layer import IPyAMSLayer
+
+# import packages
+from pyams_sequence.reference import get_reference_target
+from pyams_template.template import template_config
+from pyams_viewlet.viewlet import contentprovider_config, ViewContentProvider
+from pyramid.decorator import reify
+from zope.interface import Interface
+
+
+@contentprovider_config(name='pyams.tags', context=ITagsTarget, layer=IPyAMSLayer, view=Interface)
+@template_config(template='templates/tags.pt', layer=IPyAMSLayer)
+class TagsContentProvider(ViewContentProvider):
+    """Tags content provider"""
+
+    @reify
+    def search_target(self):
+        manager = ITagsManager(self.request.root, None)
+        if (manager is not None) and manager.enable_tags_search and manager.tags_search_target:
+            return get_reference_target(manager.tags_search_target)
+
+    @property
+    def tags(self):
+        tags = ITagsInfo(self.context).tags or ()
+        yield from sorted(tags, key=lambda x: (x.order or 999, x.alt or x.label))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_default_theme/viewlet/tag/templates/tags.pt	Wed Sep 12 18:12:22 2018 +0200
@@ -0,0 +1,12 @@
+<div i18n:domain="pyams_default_theme"
+	 tal:define="target view.search_target">
+	<tal:loop repeat="tag view.tags">
+		<span class="label label-primary">
+			<a tal:omit-tag="target is None"
+			   tal:define="href tales:absolute_url(target)"
+			   href="${href}/++tag++${tag.label}">
+				${tag.alt or tag.label}
+			</a>
+		</span>
+	</tal:loop>
+</div>
\ No newline at end of file