Commit 38f4b7d3 authored by Nicolas Dumazet's avatar Nicolas Dumazet

backport some equivalence of Zope2.12 getSite to be able to retrieve

the site from anywhere.
Also setSite() in places where the published object is not in the site
or before an ERP5 site installation: this is required for portal types
as classes in 2.12 as PARENTS is not anymore there in the request.

If some traces appear in the logs for 2.12 instances, the proper fix
is to add a setSite() in the codepath leading to the point using getSite()


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@38613 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent bafa77b1
......@@ -36,6 +36,7 @@ from Products.ERP5Type.Log import log as unrestrictedLog
from Products.CMFActivity.Errors import ActivityPendingError
import ERP5Defaults
from Products.ERP5Type.TransactionalVariable import getTransactionalVariable
from zope.site.hooks import setSite
from zLOG import LOG, INFO
from string import join
......@@ -351,6 +352,14 @@ class ERP5Site(FolderMixIn, CMFSite):
security.declareProtected(Permissions.AccessContentsInformation, 'getPath')
getPath = getUrl
security.declareProtected(Permissions.AccessContentsInformation, 'objectValues')
def objectValues(self, *args, **kw):
# When stepping in an ERP5Site from outside,
# (e.g. left hand tree frame in {zope root}/manage )
# we need to set up the site to load portal types inside each site
setSite(self)
return super(FolderMixIn, self).objectValues(*args, **kw)
security.declareProtected(Permissions.AccessContentsInformation, 'searchFolder')
def searchFolder(self, **kw):
"""
......@@ -359,6 +368,7 @@ class ERP5Site(FolderMixIn, CMFSite):
"""
if not kw.has_key('parent_uid'):
kw['parent_uid'] = self.uid
setSite(self)
return self.portal_catalog.searchResults(**kw)
security.declareProtected(Permissions.AccessContentsInformation, 'countFolder')
......@@ -369,6 +379,7 @@ class ERP5Site(FolderMixIn, CMFSite):
"""
if not kw.has_key('parent_uid'):
kw['parent_uid'] = self.uid
setSite(self)
return self.portal_catalog.countResults(**kw)
# Proxy methods for security reasons
......@@ -1466,6 +1477,23 @@ class ERP5Generator(PortalGenerator):
# Return the fully wrapped object.
p = parent.this()._getOb(id)
setSite(p)
try:
sm = p.getSiteManager()
except:
# Zope 2.8, ot site manager, no DefaultTraversable, dont care
pass
else:
import zope
# XXX hackish, required to setUpERP5Core while in tests,
# could probably be better
# (for some reason calling setSite here does not allow
# the newly created site to find the global DefaultTraversable
# object)
sm.registerAdapter(zope.traversing.adapters.DefaultTraversable,
required=(zope.interface.Interface,))
erp5_sql_deferred_connection_string = erp5_sql_connection_string
p._setProperty('erp5_catalog_storage',
erp5_catalog_storage, 'string')
......
from zLOG import LOG
module_name = 'zope.site.hooks'
native_setsite = False
try:
hooks = __import__(module_name, {}, {}, module_name)
native = True
except ImportError:
# backwards compatibility for Zope < 2.12
import imp, sys
sys.modules[module_name] = hooks = imp.new_module(module_name)
def setSite(foo=None):
pass
hooks.setSite = setSite
def getSite(foo=None):
return None
hooks.getSite = getSite
# patch getSite so that it works everywhere
from Products.ERP5Type.Globals import get_request
oldgetsite = hooks.getSite
def getSite():
try:
x = oldgetsite()
# this should be enough for Zope 2.12
if x is not None:
return x
except:
if native_setsite:
# this should not happen on 2.12, log it
import traceback; traceback.print_stack()
LOG('getSite() cant retrieve the site', 100,
'setSite() should have been called earlier on')
pass
# and Zope 2.8 needs to use the request
parents = get_request()['PARENTS']
portal = None
for item in parents:
if item.meta_type == 'ERP5 Site':
portal = item
break
if portal is None:
# not perfect?
try:
portal = parents[-1].getPortalObject()
except:
import traceback; traceback.print_stack()
raise AttributeError("getSite() cant retrieve the site.")
return portal
hooks.getSite = getSite
from Products.CMFActivity.ActivityTool import ActivityTool
ActivityTool_process_timer = ActivityTool.process_timer
def process_timer(self, *args, **kw):
portal = self.getPortalObject()
hooks.setSite(portal)
return ActivityTool_process_timer(self, *args, **kw)
ActivityTool.process_timer = process_timer
......@@ -46,13 +46,6 @@ def get_request():
Products.ERP5Type.Utils.get_request = get_request
Globals.get_request = get_request
try:
from zope.site.hooks import setSite
except ImportError:
# BACK: Zope 2.8. setSite is somewhere else, and we can't use it anyway
# since ERP5Site is not yet an ISite. Remove once we drop support for 2.8
def setSite(site=None):
pass
try:
import itools.zope
......@@ -917,6 +910,8 @@ class ERP5TypeTestCase(ProcessingNodeTestCase, PortalTestCase):
transaction.commit()
self.portal = portal
setSite(portal)
# Make sure skins are correctly set-up (it's not implicitly set up
# by Acquisition on Zope 2.12 as it is on 2.8)
portal.setupCurrentSkin(portal.REQUEST)
......
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