I have contents that needs to change its ownership. By default the owner of the content type is admin, from code obj.getOwner()
but the Creator of the content type is not admin, e.g. [email protected].
I have followed this link: http://developer.plone.org/content/ownership.html and created a view method for the content type. The following is my code snippet:
def change_owner(self):
membership = getToolByName(self.context,'portal_membership')
path='/'.join(self.context.getPhysicalPath())
brains=self.portal_catalog.searchResults(path={'query':path,'depth':0})
for brain in brains:
creator = brain.Creator
crtor_obj = membership.getMemberById(crtor1).getUser()
brain.getObject().setCreators(creator,)
brain.getObject().changeOwnership(crtor_obj)
brain.getObject().reindexObjectSecurity()
return
It seems that my code didn't change the ownership of the content. The owner is still admin and the Creator is still the original Creator. Is there any problem with my code?
I see three problems:
You call getObject()
repeatedly. That is a relatively expensive operation, cache the result in a local variable instead:
obj = brain.getObject()
You called setCreators()
with one argument, not a list:
obj.setCreators([creator])
You don't change the Owner
role:
roles = list(obj.get_local_roles_for_userid(crtor_obj))
if 'Owner' not in roles:
roles.append('Owner')
obj.manage_setLocalRoles(crtor_obj, roles)
There is a plone.app.changeownership
product that takes care of all these details for you.