Commit d7d6f603 authored by Julien Muchembled's avatar Julien Muchembled

Fixes to zope.site.hooks.getSite for Zope 2.8 and unit tests

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@38639 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 39cc1f00
from zLOG import LOG from Products.ERP5Type import Globals
module_name = 'zope.site.hooks' module_name = 'zope.site.hooks'
native_setsite = False
try: try:
hooks = __import__(module_name, {}, {}, module_name) hooks = __import__(module_name, {}, {}, module_name)
native = True _getSite = hooks.getSite
_setSite = hooks.setSite
except ImportError: except ImportError:
# backwards compatibility for Zope < 2.12 # backwards compatibility for Zope < 2.12
import imp, sys import imp, sys
...@@ -12,49 +12,29 @@ except ImportError: ...@@ -12,49 +12,29 @@ except ImportError:
site = zope.site site = zope.site
except: except:
sys.modules['zope.site'] = zope.site = imp.new_module('zope.site') sys.modules['zope.site'] = zope.site = imp.new_module('zope.site')
sys.modules[module_name] = hooks = imp.new_module(module_name) sys.modules[module_name] = hooks = imp.new_module(module_name)
def setSite(foo=None):
pass def _getSite():
hooks.setSite = setSite
def getSite(foo=None):
return None return None
hooks.getSite = getSite def _setSite(site=None):
request = Globals.get_request()
if site is not None and site not in request.get('PARENTS', ()):
request['PARENTS'] = [site]
# patch getSite so that it works everywhere # patch getSite so that it works everywhere
from Products.ERP5Type.Globals import get_request
oldgetsite = hooks.getSite
def getSite(): def getSite():
try: site = _getSite()
x = oldgetsite() if site is None:
# this should be enough for Zope 2.12 parents = Globals.get_request()['PARENTS']
if x is not None: for site in parents[::-1]:
return x if getattr(site, 'getPortalObject', None) is not None:
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 break
else:
if portal is None: raise AttributeError("getSite() can't retrieve the site")
# not perfect? return site
try:
portal = parents[-1].getPortalObject()
except:
import traceback; traceback.print_stack()
raise AttributeError("getSite() cant retrieve the site.")
return portal
hooks.getSite = getSite hooks.getSite = getSite
def setSite(site=None):
_setSite(site)
# TODO: check if caches related to portal types as classes must be invalidated
hooks.setSite = setSite
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