--- a/src/pyams_content/generations/__init__.py Fri Nov 10 11:57:19 2017 +0100
+++ b/src/pyams_content/generations/__init__.py Fri Nov 10 11:57:54 2017 +0100
@@ -39,11 +39,10 @@
from pyams_content.shared.view.manager import ViewsManager
from pyams_i18n.index import I18nTextIndexWithInterface
from pyams_security.index import PrincipalsRoleIndex
-from pyams_utils.registry import utility_config
+from pyams_utils.registry import utility_config, get_global_registry
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
@@ -54,6 +53,12 @@
REQUIRED_UTILITIES = ()
+REQUIRED_TOOLS = [('views', ViewsManager),
+ ('news', NewsManager),
+ ('forms', FormsManager),
+ ('imagemaps', ImageMapsManager)]
+
+
REQUIRED_INDEXES = [('content_type', FieldIndexWithInterface, {'interface': IBaseContent,
'discriminator': 'content_type'}),
('role:owner', PrincipalsRoleIndex, {'role_id': 'pyams.Owner'}),
@@ -96,15 +101,62 @@
'discriminator': 'references'})]
-REQUIRED_TOOLS = [('views', ViewsManager),
- ('news', NewsManager),
- ('forms', FormsManager),
- ('imagemaps', ImageMapsManager)]
+def get_tools_manager(site, config, registry=None):
+ """Check for shared tools manager"""
+ if registry is None:
+ registry = get_global_registry()
+ name = config.tools_name or \
+ registry.settings.get('pyams_content.config.tools_name', 'tools')
+ if name not in site:
+ manager = SharedToolContainer()
+ registry.notify(ObjectCreatedEvent(manager))
+ manager.title = {'en': "Shared tools",
+ 'fr': "Outils partagés"}
+ manager.short_name = {'en': "Shared tools",
+ 'fr': "Outils partagés"}
+ manager.navigation_name = {'en': "Shared tools",
+ 'fr': "Outils partagés"}
+ site[name] = manager
+ config.tools_name = name
+ else:
+ manager = site[name]
+ return manager
+
+
+def get_required_tools(manager, config, tools=REQUIRED_TOOLS, registry=None):
+ """Create required shared tools"""
+ if registry is None:
+ registry = get_global_registry()
+ for name, default_factory in tools:
+ factory = registry.settings.get('pyams_content.config.{name}_tool_factory'.format(name=name))
+ if (factory is None) or (factory.upper() not in ('NONE', '--')):
+ attr_name = '{name}_tool_name'.format(name=name)
+ tool_name = getattr(config, attr_name) or \
+ registry.settings.get('pyams_content.config.{name}'.format(name=attr_name), name)
+ if tool_name not in manager:
+ if factory is not None:
+ factory = DottedNameResolver().resolve(factory)
+ else:
+ factory = default_factory
+ tool = factory()
+ registry.notify(ObjectCreatedEvent(tool))
+ manager[tool_name] = tool
+ setattr(config, attr_name, tool_name)
+
+
+def check_required_tools(site, config_interface, tools):
+ """Check for required shared tools"""
+ configuration = config_interface(site, None)
+ if configuration is not None:
+ # check shared tools manager
+ manager = get_tools_manager(site, configuration)
+ # check for shared tools
+ get_required_tools(manager, configuration, tools)
def get_required_indexes():
indexes = REQUIRED_INDEXES
- registry = get_current_registry()
+ registry = get_global_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,
@@ -121,6 +173,7 @@
site = event.manager.__parent__
check_required_utilities(site, REQUIRED_UTILITIES)
check_required_indexes(site, get_required_indexes())
+ check_required_tools(site, ISiteRootToolsConfiguration, REQUIRED_TOOLS)
@utility_config(name='PyAMS content', provides=ISiteGenerations)
@@ -133,39 +186,4 @@
"""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 shared 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 for shared tools
- for name, default_factory in REQUIRED_TOOLS:
- factory = registry.settings.get('pyams_content.config.{name}_tool_factory'.format(name=name))
- if (factory is None) or (factory.upper() not in ('NONE', '--')):
- attr_name = '{name}_tool_name'.format(name=name)
- tool_name = getattr(tools_configuration, attr_name) or \
- registry.settings.get('pyams_content.config.{name}'.format(name=attr_name), name)
- if tool_name not in tools_manager:
- if factory is not None:
- factory = DottedNameResolver().resolve(factory)
- else:
- factory = default_factory
- tool = factory()
- registry.notify(ObjectCreatedEvent(tool))
- tools_manager[tool_name] = tool
- setattr(tools_configuration, attr_name, tool_name)
+ check_required_tools(site, ISiteRootToolsConfiguration, REQUIRED_TOOLS)