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,
"""
return default
#
# Agent methods
#
def _queryFactoryMethod(self, container, default=None):
if not self.product or not self.factory or container is None:
return default
# In case we aren't wrapped.
dispatcher = getattr(container, 'manage_addProduct', None)
if dispatcher is None:
return default
# The following 2 methods should not be used.
_getFactoryMethod = deprecated(FactoryTypeInformation._getFactoryMethod)
_constructInstance = deprecated(FactoryTypeInformation._constructInstance)
def _queryFactoryMethod(self, container, temp_object=0):
product = self.product
factory = self.factory
if not product or not factory:
return ValueError('Product factory for %s was undefined'
% self.getId())
try:
p = dispatcher[self.product]
p = container.manage_addProduct[product]
except AttributeError:
LOG('Types Tool', ERROR, '_queryFactoryMethod raised an exception',
error=exc_info())
return default
m = getattr(p, self.factory, None)
if m is not None:
try:
# validate() can either raise Unauthorized or return 0 to
# mean unauthorized.
pass
else:
if temp_object:
factory = factory[:3] == 'add' and 'newTemp' + factory[3:] or ''
m = getattr(p, factory, None)
if m is None:
return ValueError('Product factory for %s was invalid'
% self.getId())
if temp_object:
return m
permission = self.permission
if permission:
if _checkPermission(permission, container):
return m
else:
return default
elif getSecurityManager().validate(p, p, self.factory, m):
try:
# validate() can either raise Unauthorized or return 0 to
# mean unauthorized.
if getSecurityManager().validate(p, p, factory, m):
return m
except zExceptions_Unauthorized: # Catch *all* Unauths!
pass
except zExceptions_Unauthorized, e:
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')
def constructInstance(self, container, id, created_by_builder=0,
......@@ -348,25 +351,10 @@ class ERP5TypeInformation(XMLObject,
Call the init_script for the portal_type.
Returns the object.
"""
if temp_object:
if not self.factory or not self.factory.startswith('add'):
raise ValueError('Product factory for %s is invalid: %s'
% (self.getId(), self.factory))
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)
m = self._queryFactoryMethod(container, temp_object)
if isinstance(m, Exception):
raise m
ob = m(id, **kw)
# Portal type has to be set before setting other attributes
# in order to initialize aq_dynamic
......@@ -376,6 +364,7 @@ class ERP5TypeInformation(XMLObject,
# workflow and it is annoyning without security setted
ob.portal_type = self.getId()
if not temp_object:
self.updateLocalRolesOnDocument(ob, reindex=False)
# notify workflow after generating local roles, in order to prevent
......
......@@ -457,6 +457,7 @@ class DocumentConstructor(Method):
if kw: o._edit(force_update=1, **kw)
if REQUEST is not None:
REQUEST['RESPONSE'].redirect( 'manage_main' )
return o
class TempDocumentConstructor(DocumentConstructor):
......@@ -480,9 +481,8 @@ class TempDocumentConstructor(DocumentConstructor):
self.klass = TempDocument
def __call__(self, folder, id, REQUEST=None, **kw):
# CMF constructInstance is never used to build temp objects
# so we never return the id.
def __call__(self, folder, id, REQUEST=None,
activate_kw=None, is_indexable=None, reindex_kw=None, **kw):
o = self.klass(id)
if folder.isTempObject():
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