Wednesday, April 14, 2010

Change creator programmatically

In order to change an object's Creator from a script or another piece of code, use setCreators(new_creator,). Mark the comma: apparently, the method takes a list as input.

I used this in a view class which mass-changes ownership:
    def changeOwnership(self, obj, userid):
""" Change ownership of obj to userid """
membership = getToolByName(self.context, 'portal_membership')
user = membership.getMemberById(userid)
obj.setCreators(userid,)
obj.changeOwnership(user)
roles = list(obj.get_local_roles_for_userid(userid))
if 'Owner' not in roles:
roles.append('Owner')
obj.manage_setLocalRoles(userid, roles)
obj.reindexObject()
return


I had a look at the internals of plone.app.changeownership to find out what i had to do in order to make this work.

1 comment:

David said...

You've saved me again. That's exactly what I've looking for and it works a charm. Thanks heaps!