Reputation: 227
In my Plone instance I want every new created page to be moved to its own folder after creation. The folder should have the same title and description as that page. So I wrote this function as event handler of zope.lifecycleevent.interfaces.IObjectAddedEvent :
def notifyDocumentIsAdded(document, event):
portal = api.portal.get()
context = aq_inner(document)
folder = aq_parent(context)
if getattr(document,"title") != getattr(folder,"title"):
newfolder = api.content.create(
type='Folder',
title=getattr(document,"title"),
container=folder,
description=getattr(document,"description"))
api.content.move(source=document,target=newfolder,safe_id=True)
This works until the last line where I want to move the just added new Page to the new folder. I get
ERROR Products.ZCatalog A different document with value 'c31f15e4923e4f2683dedc829d2f773d' already exists in the index.'
What is meant by Document with that value? Is it a conflict of an ID? Or is it a problem to move the object that triggered the event?
Update: After using the python debugger I understand the actual problem now better: The page is moved correctly, despite my statement above. The problem is: After my handler was executed the Page will be indexed by the catalog with the outdated location, while it was moved by my handler. Even with a transaction.commit() after the movement, the page will still be indexed with its old location and the new one as well. therefore the error described above. How can I prevent that from happening.
Upvotes: 2
Views: 141
Reputation: 1324
I think this is happening because you are trying to move an object which is not fully created.
You may want to either change the logic of the event handler or register it for the IObjectCreatedEvent, otherwise you are going to end in a loop.
IObjectAddedEvent is called any time you add an object to the container, and this also happens after you move it.
Upvotes: 2