[Product-Developers] Re: Intercepting the copy event to generate a custom id

Maurits van Rees m.van.rees at zestsoftware.nl
Fri Jun 6 19:30:54 UTC 2008


Arno Blumer, on 2008-06-06:
> Yes, I'm going to use that event now.
> However, is there a better way of testing whether an object has been copied,
> or whether there
> is something on the clipboard?
> At the moment I use:
> if event.newName.startswith('copy'),
> and this is of course only the case when the object is copied to somewhere
> in the same folder.

You can call context.cb_dataValid() to see if whatever is on the
clipboard is valid data for copying.

You can call context.cb_dataItems() to get the actual items that are
referenced on the clipboard.  You can walk through those items and see
if one of them matches the object that the ObjectAddedEvent is fired
for.

As an example, here is some code I wrote for a client at Zest
Software.  It is a variation on object_paste.cpy.  In this case it
pastes objects and sets the old object as a related item on the new
object.

  results = context.manage_pasteObjects(request['__cp'])
  try:
      originals = context.cb_dataItems()
  except AttributeError:
      # This happens when doing a cut/paste.
      pass
  else:
      # Set related items on the copy.
      for result in results:
          new = context[result['new_id']]
          old = [o for o in originals if o.id == result['id']][0]
          new.setRelatedItems([old])
  transaction_note('Pasted content to %s' %
                   (context.absolute_url()))

> I thought of testing with event.oldParent, but this always seems to
> be None, whether the object is copied or newly created.

This file contains some comments by me about when old and new parent
are None.  It is for the more general ObjectMovedEvent, of which
ObjectAddedEvent is a subset, but most of the triggered events I
mention are in fact ObjectAddedEvents.

http://dev.plone.org/collective/browser/xm.booking/trunk/xm/booking/timing/events.py

-- 
Maurits van Rees | http://maurits.vanrees.org/
            Work | http://zestsoftware.nl/
"This is your day, don't let them take it away." [Barlow Girl]





More information about the Product-Developers mailing list