Commit e5966ed9 authored by Kevin Deldycke's avatar Kevin Deldycke

Use WebSite_getDocumentValue instead of WebSite_getDocument (the latter is deprecated).

Don't import classes twice.
Set constant name to upper case to respect naming convention.
Little code layout changes.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@9434 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent ba26d463
...@@ -25,34 +25,38 @@ ...@@ -25,34 +25,38 @@
# #
############################################################################## ##############################################################################
from Acquisition import ImplicitAcquisitionWrapper, aq_base, aq_inner
from AccessControl import ClassSecurityInfo from AccessControl import ClassSecurityInfo
from AccessControl import getSecurityManager from AccessControl.User import emergency_user
from Acquisition import aq_base from AccessControl.SecurityManagement import getSecurityManager, newSecurityManager, setSecurityManager
from Products.CMFCore.utils import getToolByName from Products.CMFCore.utils import getToolByName
from Products.ERP5Type import Permissions, PropertySheet,\
Constraint, Interface, Cache
from Products.ERP5.Document.Domain import Domain from Products.ERP5.Document.Domain import Domain
from Acquisition import ImplicitAcquisitionWrapper, aq_base, aq_inner from Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface, Cache
from Products.ERP5Type.Base import TempBase from Products.ERP5Type.Base import TempBase
from Products.CMFCore.utils import UniqueObject, _checkPermission, _getAuthenticatedUser
from Globals import get_request from Globals import get_request
from Persistence import Persistent from Persistence import Persistent
from Products.CMFCore.utils import UniqueObject, _checkPermission,\
_getAuthenticatedUser
from AccessControl.SecurityManagement import getSecurityManager
from AccessControl.User import emergency_user
from AccessControl.SecurityManagement import newSecurityManager,\
setSecurityManager
from ZPublisher import BeforeTraverse from ZPublisher import BeforeTraverse
from zLOG import LOG from zLOG import LOG
website_key = 'web_site_value'
WEBSITE_KEY = 'web_site_value'
class WebSiteTraversalHook(Persistent): class WebSiteTraversalHook(Persistent):
"""This is used by WebSite to rewrite URLs in such way """
This is used by WebSite to rewrite URLs in such way
that once a user gets into a Web Site object, all that once a user gets into a Web Site object, all
documents referenced by the web site are accessed documents referenced by the web site are accessed
through the web site rather than directly. through the web site rather than directly.
We inherit for persistent, so that pickle mechanism ignores _v_request We inherit for persistent, so that pickle mechanism ignores _v_request .
""" """
def _physicalPathToVirtualPath(self, path): def _physicalPathToVirtualPath(self, path):
...@@ -63,7 +67,7 @@ class WebSiteTraversalHook(Persistent): ...@@ -63,7 +67,7 @@ class WebSiteTraversalHook(Persistent):
if type(path) is type(''): if type(path) is type(''):
path = path.split( '/') path = path.split( '/')
website_path = self._v_request.get(website_key, None) website_path = self._v_request.get(WEBSITE_KEY, None)
if website_path: if website_path:
website_path = tuple(website_path) # Make sure all path are tuples website_path = tuple(website_path) # Make sure all path are tuples
path = tuple(path) # Make sure all path are tuples path = tuple(path) # Make sure all path are tuples
...@@ -75,15 +79,15 @@ class WebSiteTraversalHook(Persistent): ...@@ -75,15 +79,15 @@ class WebSiteTraversalHook(Persistent):
i = 0 i = 0
path_len = len(path) path_len = len(path)
for name in website_path: for name in website_path:
if i >= path_len: break if i >= path_len:
break
if path[i] == name: if path[i] == name:
common_index = i common_index = i
i += 1 i += 1
# Insert the web site path after the common part of the path # Insert the web site path after the common part of the path
if path_len > common_index + 1: if path_len > common_index + 1:
path = website_path + path[common_index + 1:] path = website_path + path[common_index + 1:]
rpp = self._v_request.other.get('VirtualRootPhysicalPath', ('', ))
rpp = self._v_request.other.get('VirtualRootPhysicalPath', ('',))
i = 0 i = 0
for name in rpp[:len(path)]: for name in rpp[:len(path)]:
if path[i] == name: if path[i] == name:
...@@ -93,15 +97,19 @@ class WebSiteTraversalHook(Persistent): ...@@ -93,15 +97,19 @@ class WebSiteTraversalHook(Persistent):
return path[i:] return path[i:]
def __call__(self, container, request): def __call__(self, container, request):
"""Each time we are traversed, we patch the request instance with our """
rewritted version of physicalPathToVirtualPath""" Each time we are traversed, we patch the request instance with our
rewritted version of physicalPathToVirtualPath
"""
self._v_request = request self._v_request = request
request.physicalPathToVirtualPath = self._physicalPathToVirtualPath request.physicalPathToVirtualPath = self._physicalPathToVirtualPath
Domain_getattr = Domain.inheritedAttribute('__getattr__') Domain_getattr = Domain.inheritedAttribute('__getattr__')
# Use a request key to store access attributes and prevent infinite recursion # Use a request key to store access attributes and prevent infinite recursion
cache_key = 'web_site_aq_cache' CACHE_KEY = 'web_site_aq_cache'
class WebSite(Domain): class WebSite(Domain):
""" """
...@@ -141,10 +149,11 @@ class WebSite(Domain): ...@@ -141,10 +149,11 @@ class WebSite(Domain):
""" """
request = self.REQUEST request = self.REQUEST
# Register current web site physical path for later URL generation # Register current web site physical path for later URL generation
if not request.has_key(website_key): request[website_key] = self.getPhysicalPath() if not request.has_key(WEBSITE_KEY):
request[WEBSITE_KEY] = self.getPhysicalPath()
# First let us call the super method # First let us call the super method
dynamic = Domain._aq_dynamic(self, name) dynamic = Domain._aq_dynamic(self, name)
if dynamic is not None : if dynamic is not None:
return dynamic return dynamic
# Do some optimisation here for names which can not be names of documents # Do some optimisation here for names which can not be names of documents
if name.startswith('_') or name.startswith('portal_')\ if name.startswith('_') or name.startswith('portal_')\
...@@ -152,10 +161,10 @@ class WebSite(Domain): ...@@ -152,10 +161,10 @@ class WebSite(Domain):
or name.startswith('sort-') or name == 'getLayout' \ or name.startswith('sort-') or name == 'getLayout' \
or name == 'getListItemUrl' or name.startswith('WebSite_'): or name == 'getListItemUrl' or name.startswith('WebSite_'):
return None return None
if not request.has_key(cache_key): if not request.has_key(CACHE_KEY):
request[cache_key] = {} request[CACHE_KEY] = {}
elif request[cache_key].has_key(name): elif request[CACHE_KEY].has_key(name):
return request[cache_key][name] return request[CACHE_KEY][name]
try: try:
portal = self.getPortalObject() portal = self.getPortalObject()
# Use the webmaster identity to find documents # Use the webmaster identity to find documents
...@@ -163,14 +172,14 @@ class WebSite(Domain): ...@@ -163,14 +172,14 @@ class WebSite(Domain):
if user is not None: if user is not None:
old_manager = getSecurityManager() old_manager = getSecurityManager()
newSecurityManager(get_request(), user) newSecurityManager(get_request(), user)
document = self.WebSite_getDocument(portal, name) document = self.WebSite_getDocumentValue(portal, name)
request[cache_key][name] = document request[CACHE_KEY][name] = document
if user is not None: if user is not None:
setSecurityManager(old_manager) setSecurityManager(old_manager)
except: except:
# Cleanup non recursion dict in case of exception # Cleanup non recursion dict in case of exception
if request[cache_key].has_key(name): if request[CACHE_KEY].has_key(name):
del request[cache_key][name] del request[CACHE_KEY][name]
raise raise
return document return document
...@@ -185,8 +194,5 @@ class WebSite(Domain): ...@@ -185,8 +194,5 @@ class WebSite(Domain):
def manage_afterAdd(self, item, container): def manage_afterAdd(self, item, container):
if item is self: if item is self:
handle = self.meta_type + '/' + self.getId() handle = self.meta_type + '/' + self.getId()
BeforeTraverse.registerBeforeTraverse(item, BeforeTraverse.registerBeforeTraverse(item, WebSiteTraversalHook(), handle)
WebSiteTraversalHook(),
handle)
Domain.manage_afterAdd(self, item, container) Domain.manage_afterAdd(self, item, container)
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