Arno Blumer, on 2008-06-06:
> I am trying to intercept the "copy" event for a specific type, so that I can
> generate a customized id for the copied object.
> In my old Plone 2.5.2 system I have always overridden generatUniqueId, where
> I generate a custom id for this type when it
> is created, in the format 'XYZ.CE001'.
> Now, in Plone 3, I also want to generate the same custom id when an object
> is copied, so I use generateUniqueId again in my handler.
> (Both the id and title of the object is set to the same custom generated id)
>
> I have the following subscriber in my configure.zcml:
>
><subscriber
> for="Products.ProjectManager.interfaces.ICostEstimate
> zope.lifecycleevent.interfaces.IObjectCopiedEvent"
> handler=".events.handleCostEstimateCopied"
> />
>
> Then I have the following handler:
>
> def handleCostEstimateCopied(self, event):
> """
> """
> assert self.portal_type == 'CostEstimate'
Just for style: this is not a method on a class, so do not use 'self'
but for example context, object or obj.
> # intercept copy event and generate the correct id and title for
> # the copied object
>
> import transaction
> transaction.commit()
> ce_id = event.original.generateUniqueId('CostEstimate')
> event.object.setId(ce_id)
> event.object.setTitle(ce_id)
> event.object.reindexObject()
>
> Everything works fine, except for setId.
> The title is set correctly, but the id remains, for example
> 'copy_of_XYZ.CE007', whereas it should be 'XYZ.CE008'.
>
> Which method must I use instead of setId() or edit(id=newid), since these do
> not change the id.
I would do that setting on self (or context, object, obj) instead of
event.object, but that may not make a difference.
So, after this event handler has been executed, is the title
'XYZ.CE008' and the id 'copy_of_XYZ.CE007'? In that case the
generateUniqueId method should be fine and setId is not functioning or
gets overwritten later.
Or is the title also 'copy_of_XYZ.CE007'?
I'm not sure if the transaction.commit() is necessary though it might
be.
Perhaps adding 'self.unmarkCreationFlag()' somewhere before the
reindexObject helps; you may want to try that instead of the
transaction.commit(). Look at the processForm method in
Archetypes/BaseObject.py which does some renaming when this flag has
been set. Also, processForm gets overwritten by LinguaPlone in case
you have that installed; this renames the id in some more cases.
As wild guess, perhaps the setId method is overwritten somewhere,
making it non-functional. Perhaps try context.update(id=ce_id).
I hope that helps.
--
Maurits van Rees | http://maurits.vanrees.org/
Work | http://zestsoftware.nl/
"This is your day, don't let them take it away." [Barlow Girl]
_______________________________________________
Product-Developers mailing list
[email protected]
http://lists.plone.org/mailman/listinfo/product-developers