Commit f993ad0c authored by Julien Muchembled's avatar Julien Muchembled

Cleanup code for creating an instance of a portal type

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@30354 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent b1e27b95
...@@ -299,45 +299,48 @@ class ERP5TypeInformation(XMLObject, ...@@ -299,45 +299,48 @@ class ERP5TypeInformation(XMLObject,
""" """
return default return default
# # The following 2 methods should not be used.
# Agent methods _getFactoryMethod = deprecated(FactoryTypeInformation._getFactoryMethod)
# _constructInstance = deprecated(FactoryTypeInformation._constructInstance)
def _queryFactoryMethod(self, container, default=None):
def _queryFactoryMethod(self, container, temp_object=0):
if not self.product or not self.factory or container is None: product = self.product
return default factory = self.factory
if not product or not factory:
# In case we aren't wrapped. return ValueError('Product factory for %s was undefined'
dispatcher = getattr(container, 'manage_addProduct', None) % self.getId())
try:
if dispatcher is None: p = container.manage_addProduct[product]
return default except AttributeError:
pass
try: else:
p = dispatcher[self.product] if temp_object:
except AttributeError: factory = factory[:3] == 'add' and 'newTemp' + factory[3:] or ''
LOG('Types Tool', ERROR, '_queryFactoryMethod raised an exception', m = getattr(p, factory, None)
error=exc_info()) if m is None:
return default return ValueError('Product factory for %s was invalid'
% self.getId())
m = getattr(p, self.factory, None) if temp_object:
return m
if m is not None: permission = self.permission
try: if permission:
# validate() can either raise Unauthorized or return 0 to if _checkPermission(permission, container):
# mean unauthorized. return m
permission = self.permission else:
if permission: try:
if _checkPermission(permission, container): # validate() can either raise Unauthorized or return 0 to
return m # mean unauthorized.
else: if getSecurityManager().validate(p, p, factory, m):
return default return m
elif getSecurityManager().validate(p, p, self.factory, m): except zExceptions_Unauthorized, e:
return m return e
except zExceptions_Unauthorized: # Catch *all* Unauths! return AccessControl_Unauthorized('Cannot create %s' % self.getId())
pass
security.declarePublic('isConstructionAllowed')
return default def isConstructionAllowed(self, container):
"""Test if user is allowed to create an instance in the given container
"""
return not isinstance(self._queryFactoryMethod(container), Exception)
security.declarePublic('constructInstance') security.declarePublic('constructInstance')
def constructInstance(self, container, id, created_by_builder=0, def constructInstance(self, container, id, created_by_builder=0,
...@@ -348,34 +351,20 @@ class ERP5TypeInformation(XMLObject, ...@@ -348,34 +351,20 @@ class ERP5TypeInformation(XMLObject,
Call the init_script for the portal_type. Call the init_script for the portal_type.
Returns the object. Returns the object.
""" """
if temp_object: m = self._queryFactoryMethod(container, temp_object)
if not self.factory or not self.factory.startswith('add'): if isinstance(m, Exception):
raise ValueError('Product factory for %s is invalid: %s' raise m
% (self.getId(), self.factory)) ob = m(id, **kw)
p = container.manage_addProduct[self.product]
m = getattr(p, 'newTemp' + self.factory[3:]) # Portal type has to be set before setting other attributes
ob = m(id, container) # in order to initialize aq_dynamic
if hasattr(ob, '_setPortalTypeName'): if hasattr(ob, '_setPortalTypeName'):
ob.portal_type = self.getId() #ob._setPortalTypeName(self.getId())
else: # XXX rafael: if we use _set because it is trigger by interaction
# This is part is copied from CMFCore/TypesTool/constructInstance # workflow and it is annoyning without security setted
# In case of temp object, we don't want to check security ob.portal_type = self.getId()
if (not (hasattr(container, 'isTempObject')
and container.isTempObject()) if not temp_object:
and not self.isConstructionAllowed(container)):
raise AccessControl_Unauthorized('Cannot create %s' % self.getId())
# Then keep on the construction process
ob = self._constructInstance(container, id, *args, **kw)
# Portal type has to be set before setting other attributes
# in order to initialize aq_dynamic
if hasattr(ob, '_setPortalTypeName'):
#ob._setPortalTypeName(self.getId())
# XXX rafael: if we use _set because it is trigger by interaction
# workflow and it is annoyning without security setted
ob.portal_type = self.getId()
self.updateLocalRolesOnDocument(ob, reindex=False) self.updateLocalRolesOnDocument(ob, reindex=False)
# notify workflow after generating local roles, in order to prevent # notify workflow after generating local roles, in order to prevent
......
...@@ -457,6 +457,7 @@ class DocumentConstructor(Method): ...@@ -457,6 +457,7 @@ class DocumentConstructor(Method):
if kw: o._edit(force_update=1, **kw) if kw: o._edit(force_update=1, **kw)
if REQUEST is not None: if REQUEST is not None:
REQUEST['RESPONSE'].redirect( 'manage_main' ) REQUEST['RESPONSE'].redirect( 'manage_main' )
return o
class TempDocumentConstructor(DocumentConstructor): class TempDocumentConstructor(DocumentConstructor):
...@@ -480,9 +481,8 @@ class TempDocumentConstructor(DocumentConstructor): ...@@ -480,9 +481,8 @@ class TempDocumentConstructor(DocumentConstructor):
self.klass = TempDocument self.klass = TempDocument
def __call__(self, folder, id, REQUEST=None, **kw): def __call__(self, folder, id, REQUEST=None,
# CMF constructInstance is never used to build temp objects activate_kw=None, is_indexable=None, reindex_kw=None, **kw):
# so we never return the id.
o = self.klass(id) o = self.klass(id)
if folder.isTempObject(): if folder.isTempObject():
folder._setObject(id, o) folder._setObject(id, o)
......
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