Commit 60050ef0 authored by Bryton Lacquement's avatar Bryton Lacquement 🚪

ERP5Site: register missing tools

... so that queryUtility can later access the correct ones.

Note: we do not necessarily want to register all tools.
parent 6df37dea
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
Portal class Portal class
""" """
from six.moves import map
import threading import threading
from weakref import ref as weakref from weakref import ref as weakref
from OFS.Application import Application, AppInitializer from OFS.Application import Application, AppInitializer
...@@ -36,7 +37,8 @@ from Products.ERP5Type.ERP5Type import ERP5TypeInformation ...@@ -36,7 +37,8 @@ from Products.ERP5Type.ERP5Type import ERP5TypeInformation
from Products.ERP5Type.Log import log as unrestrictedLog from Products.ERP5Type.Log import log as unrestrictedLog
from Products.CMFActivity.Errors import ActivityPendingError from Products.CMFActivity.Errors import ActivityPendingError
import ERP5Defaults import ERP5Defaults
from Products.ERP5Type.TransactionalVariable import getTransactionalVariable from Products.ERP5Type.TransactionalVariable import \
getTransactionalVariable, TransactionalResource
from Products.ERP5Type.dynamic.portal_type_class import synchronizeDynamicModules from Products.ERP5Type.dynamic.portal_type_class import synchronizeDynamicModules
from zLOG import LOG, INFO, WARNING, ERROR from zLOG import LOG, INFO, WARNING, ERROR
...@@ -226,6 +228,7 @@ class _site(threading.local): ...@@ -226,6 +228,7 @@ class _site(threading.local):
getSite, setSite = _site() getSite, setSite = _site()
_missing_tools_registered = None
class ERP5Site(FolderMixIn, CMFSite, CacheCookieMixin): class ERP5Site(FolderMixIn, CMFSite, CacheCookieMixin):
""" """
...@@ -327,7 +330,7 @@ class ERP5Site(FolderMixIn, CMFSite, CacheCookieMixin): ...@@ -327,7 +330,7 @@ class ERP5Site(FolderMixIn, CMFSite, CacheCookieMixin):
from Products.Localizer.MessageCatalog import ( from Products.Localizer.MessageCatalog import (
message_catalog_alias_sources message_catalog_alias_sources
) )
sm = self.getSiteManager() sm = self._components
for message_catalog in self.Localizer.objectValues(): for message_catalog in self.Localizer.objectValues():
sm.registerUtility(message_catalog, sm.registerUtility(message_catalog,
provided=ITranslationDomain, provided=ITranslationDomain,
...@@ -337,21 +340,26 @@ class ERP5Site(FolderMixIn, CMFSite, CacheCookieMixin): ...@@ -337,21 +340,26 @@ class ERP5Site(FolderMixIn, CMFSite, CacheCookieMixin):
provided=ITranslationDomain, provided=ITranslationDomain,
name=alias) name=alias)
def _doInitialSiteManagerMigration(self): def _registerMissingTools(self):
self._createInitialSiteManager() from Products.CMFCore import interfaces, utils
# Now that we have a sitemanager, se can do things that require tool_id_list = ("portal_skins", "portal_types", "portal_membership",
# one. Including setting up ZTK style utilities and adapters. We "portal_url", "portal_workflow")
# can even call setSite(self), as long as we roll back that later, if (None in map(self.get, tool_id_list) or not
# since we are actually in the middle of a setSite() call. TransactionalResource.registerOnce(__name__, 'site_manager', self.id)):
from zope.site.hooks import getSite, setSite return
old_site = getSite() sm = self._components
try: for tool_id in tool_id_list:
setSite(self) tool = self[tool_id]
# setSite(self) is not really necessary for the migration below, but tool_interface = utils._tool_interface_registry.get(tool_id)
# could be needed by other migrations to be added here. if tool_interface is not None:
self._doTranslationDomainRegistration() # Note: already registered tools will be either:
finally: # - updated
setSite(old_site) # - registered again after being unregistered
sm.registerUtility(aq_base(tool), tool_interface)
def markRegistered(txn):
global _missing_tools_registered
_missing_tools_registered = self.id
TransactionalResource(tpc_finish=markRegistered)
# backward compatibility auto-migration # backward compatibility auto-migration
def getSiteManager(self): def getSiteManager(self):
...@@ -368,14 +376,29 @@ class ERP5Site(FolderMixIn, CMFSite, CacheCookieMixin): ...@@ -368,14 +376,29 @@ class ERP5Site(FolderMixIn, CMFSite, CacheCookieMixin):
# OFS.ObjectManager.ObjectManager.getSiteManager(), and is exactly # OFS.ObjectManager.ObjectManager.getSiteManager(), and is exactly
# as cheap as it is on the case that self._components is already # as cheap as it is on the case that self._components is already
# set. # set.
if self.id == _missing_tools_registered:
return self._components # fast path
_components = self._components _components = self._components
if _components is not None: if _components is None:
# only create _components
self._createInitialSiteManager()
_components = self._components
# Now that we have a sitemanager, se can do things that require
# one. Including setting up ZTK style utilities and adapters. We
# can even call setSite(self), as long as we roll back that later,
# since we are actually in the middle of a setSite() call.
from zope.site.hooks import getSite, setSite
old_site = getSite()
try:
setSite(self)
self._doTranslationDomainRegistration()
self._registerMissingTools()
finally:
setSite(old_site)
else:
self._registerMissingTools()
return _components return _components
# This method below can take as (reasonably) long as it pleases
# since it will not be run ever again
self._doInitialSiteManagerMigration()
assert self._components is not None, 'Migration Failed!'
return self._components
security.declareProtected(Permissions.View, 'view') security.declareProtected(Permissions.View, 'view')
def view(self): def view(self):
......
...@@ -462,7 +462,6 @@ def synchronizeDynamicModules(context, force=False): ...@@ -462,7 +462,6 @@ def synchronizeDynamicModules(context, force=False):
portal.portal_skins.changeSkin(None) portal.portal_skins.changeSkin(None)
TransactionalResource(tpc_finish=lambda txn: TransactionalResource(tpc_finish=lambda txn:
_bootstrapped.add(portal.id)) _bootstrapped.add(portal.id))
transaction.get().note('Site migrated')
LOG('ERP5Site', INFO, 'Transition successful, please update your' LOG('ERP5Site', INFO, 'Transition successful, please update your'
' business templates') ' business templates')
else: else:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment