src/pyams_content/generations/__init__.py
changeset 0 7c0001cacf8e
child 31 0505d35a472d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/generations/__init__.py	Thu Oct 08 13:37:29 2015 +0200
@@ -0,0 +1,140 @@
+#
+# Copyright (c) 2008-2015 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.
+#
+from pyams_content.component.links.interfaces import IInternalLink
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_catalog.interfaces import DATE_RESOLUTION
+from pyams_content.interfaces import IBaseContent
+from pyams_content.root.interfaces import ISiteRootToolsConfiguration
+from pyams_content.shared.common.interfaces import IWfSharedContent
+from pyams_utils.interfaces.site import ISiteGenerations
+from pyams_utils.interfaces.traversing import IPathElements
+from pyams_workflow.interfaces import IWorkflowState
+from zope.dublincore.interfaces import IZopeDublinCore
+from zope.site.interfaces import INewLocalSite
+
+# import packages
+from hypatia.text.lexicon import Lexicon
+from pyams_catalog.index import FieldIndexWithInterface, KeywordIndexWithInterface, DatetimeIndexWithInterface
+from pyams_catalog.nltk import NltkFullTextProcessor
+from pyams_catalog.site import check_required_indexes
+from pyams_content.shared.common.manager import SharedToolContainer
+from pyams_content.shared.news.manager import NewsManager
+from pyams_i18n.index import I18nTextIndexWithInterface
+from pyams_security.index import PrincipalsRoleIndex
+from pyams_utils.registry import utility_config
+from pyams_utils.site import check_required_utilities
+from pyramid.events import subscriber
+from pyramid.path import DottedNameResolver
+from pyramid.threadlocal import get_current_registry
+from zope.lifecycleevent import ObjectCreatedEvent
+
+
+def get_fulltext_lexicon(language):
+    return Lexicon(NltkFullTextProcessor(language=language))
+
+
+REQUIRED_UTILITIES = ()
+
+
+REQUIRED_INDEXES = [('content_type', FieldIndexWithInterface, {'interface': IBaseContent,
+                                                               'discriminator': 'content_type'}),
+                    ('role:owner', PrincipalsRoleIndex, {'role_id': 'pyams.Owner'}),
+                    ('role:pilot', PrincipalsRoleIndex, {'role_id': 'pyams.Pilot'}),
+                    ('role:manager', PrincipalsRoleIndex, {'role_id': 'pyams.Manager'}),
+                    ('role:contributor', PrincipalsRoleIndex, {'role_id': 'pyams.Contributor'}),
+                    ('parents', KeywordIndexWithInterface, {'interface': IPathElements,
+                                                            'discriminator': 'parents'}),
+                    ('workflow_state', FieldIndexWithInterface, {'interface': IWorkflowState,
+                                                                 'discriminator': 'state'}),
+                    ('workflow_principal', FieldIndexWithInterface, {'interface': IWorkflowState,
+                                                                     'discriminator': 'state_principal'}),
+                    ('modifiers', KeywordIndexWithInterface, {'interface': IWfSharedContent,
+                                                              'discriminator': 'modifiers'}),
+                    ('created_date', DatetimeIndexWithInterface, {'interface': IZopeDublinCore,
+                                                                  'discriminator': 'created',
+                                                                  'resolution': DATE_RESOLUTION}),
+                    ('modified_date', DatetimeIndexWithInterface, {'interface': IZopeDublinCore,
+                                                                   'discriminator': 'modified',
+                                                                   'resolution': DATE_RESOLUTION}),
+                    ('link_reference', FieldIndexWithInterface, {'interface': IInternalLink,
+                                                                 'discriminator': 'reference'})]
+
+
+def get_required_indexes():
+    indexes = REQUIRED_INDEXES
+    registry = get_current_registry()
+    for code, language in map(lambda x: x.split(':'),
+                              registry.settings.get('pyams_content.lexicon.languages', 'en:english').split()):
+        indexes.append(('title:{0}'.format(code), I18nTextIndexWithInterface,
+                        {'language': code,
+                         'interface': IBaseContent,
+                         'discriminator': 'title',
+                         'lexicon': lambda: get_fulltext_lexicon(language)}))
+    return indexes
+
+
+@subscriber(INewLocalSite)
+def handle_new_local_site(event):
+    """Check for required utilities when a site is created"""
+    site = event.manager.__parent__
+    check_required_utilities(site, REQUIRED_UTILITIES)
+    check_required_indexes(site, get_required_indexes())
+
+
+@utility_config(name='PyAMS content', provides=ISiteGenerations)
+class WebsiteGenerationsChecker(object):
+    """PyAMS content package generations checker"""
+
+    generation = 1
+
+    def evolve(self, site, current=None):
+        """Check for required utilities"""
+        check_required_utilities(site, REQUIRED_UTILITIES)
+        check_required_indexes(site, get_required_indexes())
+        registry = get_current_registry()
+        tools_configuration = ISiteRootToolsConfiguration(site)
+        # check tools manager
+        tools_name = tools_configuration.tools_name or \
+                     registry.settings.get('pyams_content.config.tools_name', 'tools')
+        if tools_name not in site:
+            tools_manager = SharedToolContainer()
+            registry.notify(ObjectCreatedEvent(tools_manager))
+            tools_manager.title = {'en': "Shared tools",
+                                   'fr': "Outils partagés"}
+            tools_manager.short_name = {'en': "Shared tools",
+                                        'fr': "Outils partagés"}
+            tools_manager.navigation_name = {'en': "Shared tools",
+                                             'fr': "Outils partagés"}
+            site[tools_name] = tools_manager
+            tools_configuration.tools_name = tools_name
+        else:
+            tools_manager = site[tools_name]
+        # check news shared tool
+        factory = registry.settings.get('pyams_content.config.news_tool_factory')
+        if (factory is None) or (factory.upper() not in ('NONE', '--')):
+            news_tool_name = tools_configuration.news_tool_name or \
+                             registry.settings.get('pyams_content.config.news_tool_name', 'news')
+            if news_tool_name not in tools_manager:
+                if factory is not None:
+                    factory = DottedNameResolver().resolve(factory)
+                else:
+                    factory = NewsManager
+                tool = factory()
+                registry.notify(ObjectCreatedEvent(tool))
+                tools_manager[news_tool_name] = tool
+                tools_configuration.news_tool_name = news_tool_name