Added Opensearch metas adapter and views
authorThierry Florac <thierry.florac@onf.fr>
Tue, 29 Jan 2019 15:58:25 +0100
changeset 409 fc623fa4f4ac
parent 408 e6dd4e74ae8b
child 410 61788d2c539f
Added Opensearch metas adapter and views
src/pyams_default_theme/features/search/manager.py
src/pyams_default_theme/features/search/templates/opensearch.pt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_default_theme/features/search/manager.py	Tue Jan 29 15:58:25 2019 +0100
@@ -0,0 +1,67 @@
+#
+# Copyright (c) 2008-2019 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'
+
+from pyramid.exceptions import NotFound
+from pyramid.view import view_config
+from zope.interface import Interface
+
+from pyams_content.features.search.interfaces import ISearchManagerInfo
+from pyams_content.root import ISiteRoot
+from pyams_i18n.interfaces import II18n
+from pyams_skin.interfaces.metas import IHTMLContentMetas
+from pyams_skin.layer import IPyAMSUserLayer
+from pyams_skin.metas import LinkMeta
+from pyams_utils.adapter import ContextRequestViewAdapter, adapter_config
+from pyams_utils.url import absolute_url
+
+
+@adapter_config(name='opensearch',
+                context=(Interface, IPyAMSUserLayer, Interface), provides=IHTMLContentMetas)
+class OpenSearchMetasAdapter(ContextRequestViewAdapter):
+    """OpenSearch metas adapter"""
+
+    def __new__(cls, context, request, view):
+        search_info = ISearchManagerInfo(request.root, None)
+        if (search_info is None) or (search_info.target is None):
+            return None
+        return ContextRequestViewAdapter.__new__(cls)
+
+    order = 25
+
+    def get_metas(self):
+        search_info = ISearchManagerInfo(self.request.root, None)
+        yield LinkMeta('search', 'application/opensearchdescription+xml',
+                       href=absolute_url(self.request.root, self.request, 'opensearch.xml'),
+                       title=II18n(search_info).query_attribute('name', request=self.request))
+
+
+@view_config(name='opensearch.xml', context=ISiteRoot, request_type=IPyAMSUserLayer,
+             renderer='templates/opensearch.pt')
+class OpenSearchDescriptionView(object):
+    """Site root Opensearch description"""
+
+    def __init__(self, request):
+        self.request = request
+        self.context = request.context
+
+    def __call__(self):
+        self.request.response.content_type = 'application/opensearchdescription+xml'
+        return {}
+
+    @property
+    def search_info(self):
+        search_info = ISearchManagerInfo(self.context, None)
+        if (search_info is None) or (search_info.target is None):
+            raise NotFound()
+        return search_info
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_default_theme/features/search/templates/opensearch.pt	Tue Jan 29 15:58:25 2019 +0100
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/"
+	tal:define="manager_info view.search_info">
+	<ShortName>${i18n:manager_info.name}</ShortName>
+	<Description>${i18n:manager_info.description}</Description>
+	<Url type="text/html"
+		 tal:define="target tales:absolute_url(manager_info.target)"
+		 template="${target}?user_search={searchTerms}"></Url>
+</OpenSearchDescription>