Commit 76207531 authored by Rafael Monnerat's avatar Rafael Monnerat

Added support to use strings to upgradeObjectClass.

This change allows use python scripts to upgrade Object Class without write external methods for it. The from_class and to_class arguments can be used like followed:

 from_class="Products.ERP5Type.Document.Folder.Folder"
 to_class="Products.ERP5Type.Document.Category.Category"

or use previous behaviour:

 from_class = folder.__class__
 to_class = category.__class__




git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@36278 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 924a595e
...@@ -1084,13 +1084,28 @@ class Folder(CopyContainer, CMFBTreeFolder, CMFHBTreeFolder, Base, FolderMixIn, ...@@ -1084,13 +1084,28 @@ class Folder(CopyContainer, CMFBTreeFolder, CMFHBTreeFolder, Base, FolderMixIn,
security.declareProtected( Permissions.ModifyPortalContent, 'upgradeObjectClass' ) security.declareProtected( Permissions.ModifyPortalContent, 'upgradeObjectClass' )
def upgradeObjectClass(self, test_before, from_class, to_class, test_after): def upgradeObjectClass(self, test_before, from_class, to_class, test_after):
""" """
upgrade the class of all objects inside this Upgrade the class of all objects inside this particular folder:
particular folder
test_before and test_after have to be a method with one parameter. test_before and test_after have to be a method with one parameter.
migrations is a dictionnary of class, { from_class : to_class }
from_class and to_class can be classes (o.__class___) or strings like:
'Products.ERP5Type.Document.Folder.Folder'
""" """
#LOG("upradeObjectClass: folder ",0,self.id) #LOG("upgradeObjectClass: folder ", 0, self.id)
test_list = [] test_list = []
def getClassFromString(a_klass):
from_module = '.'.join(a_klass.split('.')[:-1])
real_klass = a_klass.split('.')[-1]
# XXX It is possible that API Change for Python 2.6.
mod = __import__(from_module, globals(), locals(), [real_klass])
return getattr(mod, real_klass)
if isinstance(from_class, type('')):
from_class = getClassFromString(from_class)
if isinstance(to_class, type('')):
to_class = getClassFromString(to_class)
folder = self.getObject() folder = self.getObject()
for o in self.listFolderContents(): for o in self.listFolderContents():
# Make sure this sub object is not the same as object # Make sure this sub object is not the same as object
......
...@@ -221,7 +221,25 @@ class TestFolder(ERP5TypeTestCase, LogInterceptor): ...@@ -221,7 +221,25 @@ class TestFolder(ERP5TypeTestCase, LogInterceptor):
self.assertNotEquals(self.folder[subobject.getId()][obj.getId()].__class__, from_class) self.assertNotEquals(self.folder[subobject.getId()][obj.getId()].__class__, from_class)
self.assertEquals([1, 1], result) self.assertEquals([1, 1], result)
def test_upgradeObjectClassWithStrings(self):
""" Test if it changes Object Class """
type_list = ['Folder', 'Category' ]
self._setAllowedContentTypesForFolderType(type_list)
obj = self.folder.newContent(portal_type="Category")
from_class_as_string = 'Products.ERP5Type.Document.Category.Category'
to_class_as_string = 'Products.ERP5Type.Document.Folder.Folder'
from_class = obj.__class__
to_class = self.folder.__class__
createZODBPythonScript(self.getPortal().portal_skins.custom,
"test_upgradeObject", 'x',
'return [1]')
test_script = self.getPortal().portal_skins.custom.test_upgradeObject
result = self.folder.upgradeObjectClass(test_script, from_class_as_string,
to_class_as_string, test_script)
transaction.commit()
self.assertEquals(self.folder[obj.getId()].__class__, to_class)
self.assertNotEquals(self.folder[obj.getId()].__class__, from_class)
self.assertEquals([1], result)
def test_suite(): def test_suite():
suite = unittest.TestSuite() suite = unittest.TestSuite()
......
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