Added sitemap and "robots.txt"
authorThierry Florac <thierry.florac@onf.fr>
Fri, 19 Oct 2018 17:19:36 +0200
changeset 1027 f5244268e151
parent 1026 2bc20b979c84
child 1028 3a608029647e
Added sitemap and "robots.txt"
src/pyams_content/features/sitemap/__init__.py
src/pyams_content/features/sitemap/skin/__init__.py
src/pyams_content/features/sitemap/skin/templates/humans.pt
src/pyams_content/features/sitemap/skin/templates/robots.pt
src/pyams_content/features/sitemap/skin/templates/root-sitemap.pt
src/pyams_content/features/sitemap/skin/templates/tool-sitemap.pt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/features/sitemap/__init__.py	Fri Oct 19 17:19:36 2018 +0200
@@ -0,0 +1,15 @@
+#
+# 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'
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/features/sitemap/skin/__init__.py	Fri Oct 19 17:19:36 2018 +0200
@@ -0,0 +1,99 @@
+#
+# 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'
+
+from datetime import datetime
+from itertools import product
+
+from hypatia.catalog import CatalogQuery
+from hypatia.interfaces import ICatalog
+from hypatia.query import Any, Eq
+from pyramid.view import view_config
+from zope.intid import IIntIds
+
+from pyams_catalog.query import CatalogResultSet
+from pyams_content.root import ISiteRoot, ISiteRootToolsConfiguration
+from pyams_content.shared.common import CONTENT_TYPES, IBaseSharedTool
+from pyams_i18n.interfaces import II18nManager
+from pyams_skin.layer import IPyAMSUserLayer
+from pyams_utils.list import unique_iter
+from pyams_utils.registry import get_all_utilities_registered_for, get_utility
+from pyams_workflow.interfaces import IWorkflow, IWorkflowPublicationInfo
+
+
+@view_config(name='robots.txt', context=ISiteRoot, request_type=IPyAMSUserLayer,
+             renderer='templates/robots.pt')
+def site_root_robots_view(request):
+    """Site root robots.txt view"""
+    request.response.content_type = 'text/plain'
+    return {
+        'tools_configuration': ISiteRootToolsConfiguration(request.root),
+        'disallow': [tool for tool in get_all_utilities_registered_for(IBaseSharedTool)
+                     if not tool.shared_content_menu]
+    }
+
+
+@view_config(name='humans.txt', context=ISiteRoot, request_type=IPyAMSUserLayer,
+             renderer='templates/humans.pt')
+def site_root_humans_view(request):
+    """Site root humans.txt view"""
+    request.response.content_type = 'text/plain'
+    return {}
+
+
+@view_config(name='sitemap.xml', context=ISiteRoot, request_type=IPyAMSUserLayer,
+             renderer='templates/root-sitemap.pt')
+class SiteRootSitemapView(object):
+    """Site root sitemap view"""
+
+    def __init__(self, request):
+        self.request = request
+
+    def __call__(self):
+        self.request.response.content_type = 'text/xml'
+        return {}
+
+    @property
+    def sources(self):
+        timestamp = datetime.utcnow().isoformat()
+        for tool in get_all_utilities_registered_for(IBaseSharedTool):
+            if not tool.shared_content_menu:
+                continue
+            publication_info = IWorkflowPublicationInfo(tool, None)
+            if (publication_info is None) or publication_info.is_visible(self.request):
+                yield timestamp, tool
+
+
+@view_config(name='sitemap.xml', context=IBaseSharedTool, request_type=IPyAMSUserLayer,
+             renderer='templates/tool-sitemap.pt')
+class SharedToolSitemapView(object):
+    """Shared tool sitemap view"""
+
+    def __init__(self, request):
+        self.request = request
+
+    def __call__(self):
+        self.request.response.content_type = 'text/xml'
+        return {}
+
+    @property
+    def contents(self):
+        context = self.request.context
+        catalog = get_utility(ICatalog)
+        intids = get_utility(IIntIds)
+        workflow = IWorkflow(context)
+        params = Eq(catalog['parents'], intids.register(context)) & \
+                 Any(catalog['content_type'], CONTENT_TYPES.keys()) & \
+                 Any(catalog['workflow_state'], workflow.published_states)
+        for version in unique_iter(CatalogResultSet(CatalogQuery(catalog).query(params))):
+            yield from product(II18nManager(version).get_languages(), (version,))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/features/sitemap/skin/templates/humans.pt	Fri Oct 19 17:19:36 2018 +0200
@@ -0,0 +1,14 @@
+/* THANKS */
+Main architect and developer: Thierry Florac
+From: Paris, France
+
+PyAMS contributor: Damien Correia
+From: Paris, France
+
+/* SITE */
+Doctype: HTML5
+Standards: HTML5, CSS3, A11y
+IDE: PyCharm, The GIMP, Mercurial
+Components: JQuery, Bootstrap, Python 3, Hypatia
+Software: Pyramid, PyAMS, PostgreSQL, Elasticsearch, Redis
+Language: english, french
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/features/sitemap/skin/templates/robots.pt	Fri Oct 19 17:19:36 2018 +0200
@@ -0,0 +1,7 @@
+Sitemap: <tal:var define="url tales:absolute_url(request.root)">${url}</tal:var>/sitemap.xml
+
+User-agent: *
+Disallow: /--static--/
+Disallow: /api/
+Disallow: /${tools_configuration.tables_name}/<tal:loop repeat="tool disallow">
+Disallow: /${tools_configuration.tools_name}/${tool.__name__}/</tal:loop>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/features/sitemap/skin/templates/root-sitemap.pt	Fri Oct 19 17:19:36 2018 +0200
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
+	<tal:loop repeat="(ts, source) view.sources">
+		<sitemap>
+			<loc>${tales:absolute_url(source)}/sitemap.xml</loc>
+			<lastmod>${ts}</lastmod>
+		</sitemap>
+	</tal:loop>
+</sitemapindex>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/features/sitemap/skin/templates/tool-sitemap.pt	Fri Oct 19 17:19:36 2018 +0200
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
+	<url tal:repeat="(lang, content) view.contents">
+		<loc tal:define="url tales:canonical_url(content)">${url}?lang=${lang}</loc>
+		<lastmod>${tales:timestamp(content, 'iso')}</lastmod>
+	</url>
+</urlset>