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())
if dispatcher is None:
return default
try: try:
p = dispatcher[self.product] p = container.manage_addProduct[product]
except AttributeError: except AttributeError:
LOG('Types Tool', ERROR, '_queryFactoryMethod raised an exception', pass
error=exc_info()) else:
return default if temp_object:
factory = factory[:3] == 'add' and 'newTemp' + factory[3:] or ''
m = getattr(p, self.factory, None) m = getattr(p, factory, None)
if m is None:
if m is not None: return ValueError('Product factory for %s was invalid'
try: % self.getId())
# validate() can either raise Unauthorized or return 0 to if temp_object:
# mean unauthorized. return m
permission = self.permission permission = self.permission
if permission: if permission:
if _checkPermission(permission, container): if _checkPermission(permission, container):
return m return m
else: else:
return default try:
elif getSecurityManager().validate(p, p, self.factory, m): # validate() can either raise Unauthorized or return 0 to
# mean unauthorized.
if getSecurityManager().validate(p, p, factory, m):
return m return m
except zExceptions_Unauthorized: # Catch *all* Unauths! except zExceptions_Unauthorized, e:
pass return e
return AccessControl_Unauthorized('Cannot create %s' % self.getId())
return default security.declarePublic('isConstructionAllowed')
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,25 +351,10 @@ class ERP5TypeInformation(XMLObject, ...@@ -348,25 +351,10 @@ 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:])
ob = m(id, container)
if hasattr(ob, '_setPortalTypeName'):
ob.portal_type = self.getId()
else:
# This is part is copied from CMFCore/TypesTool/constructInstance
# In case of temp object, we don't want to check security
if (not (hasattr(container, 'isTempObject')
and container.isTempObject())
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 # Portal type has to be set before setting other attributes
# in order to initialize aq_dynamic # in order to initialize aq_dynamic
...@@ -376,6 +364,7 @@ class ERP5TypeInformation(XMLObject, ...@@ -376,6 +364,7 @@ class ERP5TypeInformation(XMLObject,
# workflow and it is annoyning without security setted # workflow and it is annoyning without security setted
ob.portal_type = self.getId() ob.portal_type = self.getId()
if not temp_object:
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