src/pyams_content/generations/__init__.py
changeset 490 30fcd0659601
parent 470 2b552177c142
child 603 eadaa6149824
equal deleted inserted replaced
489:dcdb0ce828f8 490:30fcd0659601
    20 # import interfaces
    20 # import interfaces
    21 from pyams_catalog.interfaces import MINUTE_RESOLUTION, DATE_RESOLUTION
    21 from pyams_catalog.interfaces import MINUTE_RESOLUTION, DATE_RESOLUTION
    22 from pyams_content.interfaces import IBaseContent
    22 from pyams_content.interfaces import IBaseContent
    23 from pyams_content.root.interfaces import ISiteRootToolsConfiguration
    23 from pyams_content.root.interfaces import ISiteRootToolsConfiguration
    24 from pyams_content.shared.common.interfaces import IWfSharedContent
    24 from pyams_content.shared.common.interfaces import IWfSharedContent
       
    25 from pyams_content.shared.form.interfaces import IFormsManagerFactory
       
    26 from pyams_content.shared.imagemap.interfaces import IImageMapManagerFactory
       
    27 from pyams_content.shared.logo.interfaces import ILogosManagerFactory
       
    28 from pyams_content.shared.news.interfaces import INewsManagerFactory
       
    29 from pyams_content.shared.view.interfaces import IViewsManagerFactory
    25 from pyams_utils.interfaces.site import ISiteGenerations
    30 from pyams_utils.interfaces.site import ISiteGenerations
    26 from pyams_utils.interfaces.traversing import IPathElements
    31 from pyams_utils.interfaces.traversing import IPathElements
    27 from pyams_workflow.interfaces import IWorkflowState, IWorkflowPublicationInfo
    32 from pyams_workflow.interfaces import IWorkflowState, IWorkflowPublicationInfo
    28 from zope.dublincore.interfaces import IZopeDublinCore
    33 from zope.dublincore.interfaces import IZopeDublinCore
    29 
    34 
    33 from pyams_catalog.nltk import NltkFullTextProcessor
    38 from pyams_catalog.nltk import NltkFullTextProcessor
    34 from pyams_catalog.site import check_required_indexes
    39 from pyams_catalog.site import check_required_indexes
    35 from pyams_content.reference import ReferencesManager
    40 from pyams_content.reference import ReferencesManager
    36 from pyams_content.reference.pictograms import PictogramTable
    41 from pyams_content.reference.pictograms import PictogramTable
    37 from pyams_content.shared.common.manager import SharedToolContainer
    42 from pyams_content.shared.common.manager import SharedToolContainer
    38 from pyams_content.shared.form.manager import FormsManager
       
    39 from pyams_content.shared.imagemap.manager import ImageMapsManager
       
    40 from pyams_content.shared.logo.manager import LogosManager
       
    41 from pyams_content.shared.news.manager import NewsManager
       
    42 from pyams_content.shared.view.manager import ViewsManager
       
    43 from pyams_i18n.index import I18nTextIndexWithInterface
    43 from pyams_i18n.index import I18nTextIndexWithInterface
    44 from pyams_security.index import PrincipalsRoleIndex
    44 from pyams_security.index import PrincipalsRoleIndex
    45 from pyams_utils.registry import utility_config, get_global_registry
    45 from pyams_utils.registry import utility_config, get_global_registry
    46 from pyams_utils.site import check_required_utilities
    46 from pyams_utils.site import check_required_utilities
    47 from pyramid.path import DottedNameResolver
    47 from pyramid.path import DottedNameResolver
    50 
    50 
    51 def get_fulltext_lexicon(language):
    51 def get_fulltext_lexicon(language):
    52     return Lexicon(NltkFullTextProcessor(language=language))
    52     return Lexicon(NltkFullTextProcessor(language=language))
    53 
    53 
    54 
    54 
       
    55 RENAMED_CLASSES = {
       
    56     'pyams_content.shared.common.review ReviewComment': 'pyams_content.features.review ReviewComment',
       
    57     'pyams_content.shared.common.review ReviewCommentsContainer':
       
    58         'pyams_content.features.review ReviewCommentsContainer'
       
    59 }
       
    60 
       
    61 
    55 REQUIRED_UTILITIES = ()
    62 REQUIRED_UTILITIES = ()
    56 
    63 
    57 REQUIRED_TABLES = (
    64 REQUIRED_TABLES = (
    58     ('pictograms_table_name', 'pictograms', PictogramTable),
    65     ('pictograms_table_name', 'pictograms', PictogramTable),
    59 )
    66 )
    60 
    67 
    61 REQUIRED_TOOLS = [
    68 REQUIRED_TOOLS = [
    62     ('views', ViewsManager),
    69     ('views', IViewsManagerFactory),
    63     ('logos', LogosManager),
    70     ('logos', ILogosManagerFactory),
    64     ('imagemaps', ImageMapsManager),
    71     ('imagemaps', IImageMapManagerFactory),
    65     ('forms', FormsManager),
    72     ('forms', IFormsManagerFactory),
    66     ('news', NewsManager)
    73     ('news', INewsManagerFactory)
    67 ]
    74 ]
    68 
    75 
    69 REQUIRED_INDEXES = [
    76 REQUIRED_INDEXES = [
    70     ('content_type', FieldIndexWithInterface, {'interface': IBaseContent,
    77     ('content_type', FieldIndexWithInterface, {'interface': IBaseContent,
    71                                                'discriminator': 'content_type'}),
    78                                                'discriminator': 'content_type'}),
   109 #
   116 #
   110 # Checker for required shared tables
   117 # Checker for required shared tables
   111 #
   118 #
   112 
   119 
   113 def check_required_tables(site, tables=REQUIRED_TABLES, registry=None):
   120 def check_required_tables(site, tables=REQUIRED_TABLES, registry=None):
   114     """Check for required reference tables"""
   121     """Check for required reference tables
       
   122 
       
   123     :param site: site root to check for tables
       
   124     :param tables: iterable of reference tables to check or create; each occurrence is a tuple of three elements
       
   125         containing configuration attribute name, default table name and table factory
       
   126     :param registry: optional registry object
       
   127 
       
   128     For each required table, the checking process is:
       
   129     - get table name from current site configuration, or from configuration settings using
       
   130         'pyams_content.config.{table_name}' parameter where 'table_name' is the attribute name given as first
       
   131         tuple argument
       
   132     - if table doesn't exists, create table using given factory and given table name, and store this name into
       
   133         current site configuration
       
   134     """
   115 
   135 
   116     def get_tables_manager():
   136     def get_tables_manager():
   117         # check references tables manager
   137         # check references tables manager
   118         tables_name = tools_configuration.tables_name or \
   138         tables_name = tools_configuration.tables_name or \
   119                       registry.settings.get('pyams_content.config.tables_name', 'references')
   139                       registry.settings.get('pyams_content.config.tables_name', 'references')
   140         if registry is None:
   160         if registry is None:
   141             registry = get_global_registry()
   161             registry = get_global_registry()
   142         tables_manager = get_tables_manager()
   162         tables_manager = get_tables_manager()
   143         for attr, name, factory in tables:
   163         for attr, name, factory in tables:
   144             table_name = getattr(tools_configuration, attr, None) or \
   164             table_name = getattr(tools_configuration, attr, None) or \
   145                          registry.settings.get('pyams_config.config.{0}'.format(attr), name)
   165                          registry.settings.get('pyams_content.config.{0}'.format(attr), name)
   146             if table_name not in tables_manager:
   166             if table_name not in tables_manager:
   147                 table = factory()
   167                 table = factory()
   148                 logger.info("Creating table {0!r}...".format(table))
   168                 logger.info("Creating table {0!r}...".format(table))
   149                 registry.notify(ObjectCreatedEvent(table))
   169                 registry.notify(ObjectCreatedEvent(table))
   150                 tables_manager[table_name] = table
   170                 tables_manager[table_name] = table
   154 #
   174 #
   155 # Checker for required shared tools
   175 # Checker for required shared tools
   156 #
   176 #
   157 
   177 
   158 def check_required_tools(site, config_interface, tools):
   178 def check_required_tools(site, config_interface, tools):
   159     """Check for required shared tools"""
   179     """Check for required shared tools
       
   180 
       
   181     :param site: site root to check for tools
       
   182     :param config_interface: site configuration interface; configuration attributes are updated
       
   183         when tools are created
       
   184     :param tools: iterable of shared tools to check or create; each occurrence is a tuple of two elements
       
   185         containing the tool name and it's factory interface.
       
   186 
       
   187     For each required tool, the checking process is:
       
   188     - get factory name from settings; the settings parameter name is 'pyams_content.config.{name}_tool_factory',
       
   189         where 'name' is the first element of tool's tuple configuration
       
   190     - if factory name is set as 'None' or '--', tool checking and creation is skipped
       
   191     - otherwise, we look for requested tool
       
   192     - if there is no shared tool with given name, we look for factory: this can be given as dotted Python name in
       
   193         configuration settings, as a named adapter from site manager to given factory interface (with name set as
       
   194         requested tool name), as an anonymous adapter from site manager to given factory interface, or as a utility
       
   195         providing given factory interface
       
   196     - if factory is found, the shared tool is created and it's name is stored into current site configuration
       
   197     """
   160 
   198 
   161     def get_tools_manager(site, config, registry=None):
   199     def get_tools_manager(site, config, registry=None):
   162         """Check for shared tools manager"""
   200         """Check for shared tools manager"""
   163         if registry is None:
   201         if registry is None:
   164             registry = get_global_registry()
   202             registry = get_global_registry()
   187 
   225 
   188     def get_required_tools(manager, config, tools=REQUIRED_TOOLS, registry=None):
   226     def get_required_tools(manager, config, tools=REQUIRED_TOOLS, registry=None):
   189         """Create required shared tools"""
   227         """Create required shared tools"""
   190         if registry is None:
   228         if registry is None:
   191             registry = get_global_registry()
   229             registry = get_global_registry()
   192         for name, default_factory in tools:
   230         for name, factory_interface in tools:
   193             factory = registry.settings.get('pyams_content.config.{name}_tool_factory'.format(name=name))
   231             factory = registry.settings.get('pyams_content.config.{name}_tool_factory'.format(name=name))
   194             if (factory is None) or (factory.upper() not in ('NONE', '--')):
   232             if (factory is None) or (factory.upper() not in ('NONE', '--')):
   195                 attr_name = '{name}_tool_name'.format(name=name)
   233                 attr_name = '{name}_tool_name'.format(name=name)
   196                 tool_name = getattr(config, attr_name, None) or \
   234                 tool_name = getattr(config, attr_name, None) or \
   197                             registry.settings.get('pyams_content.config.{name}'.format(name=attr_name), name)
   235                             registry.settings.get('pyams_content.config.{name}'.format(name=attr_name), name)
   198                 if tool_name not in manager:
   236                 if tool_name not in manager:
   199                     if factory is not None:
   237                     if factory is not None:
   200                         factory = DottedNameResolver().resolve(factory)
   238                         factory = DottedNameResolver().resolve(factory)
   201                     else:
   239                     if factory is None:
   202                         factory = default_factory
   240                         factory = registry.queryAdapter(manager, factory_interface, name=name)
   203                     tool = factory()
   241                         if factory is None:
   204                     registry.notify(ObjectCreatedEvent(tool))
   242                             factory = registry.queryAdapter(manager, factory_interface)
   205                     manager[tool_name] = tool
   243                             if factory is None:
   206                     setattr(config, attr_name, tool_name)
   244                                 factory = registry.queryUtility(factory_interface)
       
   245                     if factory is not None:
       
   246                         tool = factory()
       
   247                         registry.notify(ObjectCreatedEvent(tool))
       
   248                         manager[tool_name] = tool
       
   249                         setattr(config, attr_name, tool_name)
   207 
   250 
   208     configuration = config_interface(site, None)
   251     configuration = config_interface(site, None)
   209     if configuration is not None:
   252     if configuration is not None:
   210         # check shared tools manager
   253         # check shared tools manager
   211         manager = get_tools_manager(site, configuration)
   254         manager = get_tools_manager(site, configuration)