Use generic function to generate all required shared tools
authorThierry Florac <thierry.florac@onf.fr>
Wed, 14 Jun 2017 15:22:24 +0200
changeset 86 5d179f78817b
parent 85 f6474843036e
child 87 f382a0fb2375
Use generic function to generate all required shared tools
src/pyams_content/generations/__init__.py
--- a/src/pyams_content/generations/__init__.py	Wed Jun 14 15:19:38 2017 +0200
+++ b/src/pyams_content/generations/__init__.py	Wed Jun 14 15:22:24 2017 +0200
@@ -79,6 +79,12 @@
                                                                  'discriminator': 'reference'})]
 
 
+REQUIRED_TOOLS = [('views', ViewsManager),
+                  ('news', NewsManager),
+                  ('forms', FormsManager),
+                  ('imagemaps', ImageMapsManager)]
+
+
 def get_required_indexes():
     indexes = REQUIRED_INDEXES
     registry = get_current_registry()
@@ -112,7 +118,8 @@
         check_required_indexes(site, get_required_indexes())
         registry = get_current_registry()
         tools_configuration = ISiteRootToolsConfiguration(site)
-        # check tools manager
+
+        # 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:
@@ -128,59 +135,20 @@
             tools_configuration.tools_name = tools_name
         else:
             tools_manager = site[tools_name]
-        # check views shared tool
-        factory = registry.settings.get('pyams_content.config.views_tool_factory')
-        if (factory is None) or (factory.upper() not in ('NONE', '--')):
-            views_tool_name = tools_configuration.views_tool_name or \
-                              registry.settings.get('pyams_content.config.views_tool_name', 'views')
-            if views_tool_name not in tools_manager:
-                if factory is not None:
-                    factory = DottedNameResolver().resolve(factory)
-                else:
-                    factory = ViewsManager
-                tool = factory()
-                registry.notify(ObjectCreatedEvent(tool))
-                tools_manager[views_tool_name] = tool
-                tools_configuration.views_tool_name = views_tool_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
-        # check image maps shared tool
-        factory = registry.settings.get('pyams_content.config.imagemaps_tool_factory')
-        if (factory is None) or (factory.upper() not in ('NONE', '--')):
-            maps_tool_name = tools_configuration.imagemaps_tool_name or \
-                             registry.settings.get('pyams_content.config.imagemaps_tool_name', 'imgmaps')
-            if maps_tool_name not in tools_manager:
-                if factory is not None:
-                    factory = DottedNameResolver().resolve(factory)
-                else:
-                    factory = ImageMapsManager
-                tool = factory()
-                registry.notify(ObjectCreatedEvent(tool))
-                tools_manager[maps_tool_name] = tool
-                tools_configuration.imagemaps_tool_name = maps_tool_name
-        # check forms shared tool
-        factory = registry.settings.get('pyams_content.config.forms_tool_factory')
-        if (factory is None) or (factory.upper() not in ('NONE', '--')):
-            forms_tool_name = tools_configuration.forms_tool_name or \
-                              registry.settings.get('pyams_content.config.forms_tool_name', 'forms')
-            if forms_tool_name not in tools_manager:
-                if factory is not None:
-                    factory = DottedNameResolver().resolve(factory)
-                else:
-                    factory = FormsManager
-                tool = factory()
-                registry.notify(ObjectCreatedEvent(tool))
-                tools_manager[forms_tool_name] = tool
-                tools_configuration.forms_tool_name = forms_tool_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)