[Zope-dev] Creating and pasting to a ZClass within a single transaction does not work

Jim Fulton jim@digicool.com
Wed, 25 Aug 1999 20:29:30 -0400


Kevin Dangoor wrote:
> 
> Hi,
> 
>     Last week, there was an exchange going on because I was having trouble
> copying and pasting in DTML (or Python) from a Folder to a ZClass instance.
> The thread started with
> http://www.zope.org/pipermail/zope-dev/1999-August/001248.html
> 
>     It looks like the problem is that I was creating the ZClass instance and
> doing the copy/paste operation all at once. If I use one DTML method to
> create the new objects, and then a second method to do the copy/paste,
> everything works just fine.
> 
>     Should there be a problem pasting objects to a new object within the
> same transaction?

The problem is that manage_pasteObjects needs to be called on
an object that is already in the database.  A new object doesn't
actually get inserted into the database until the transaction commits.
The new object needs to have a _p_jar attribute (a database connection)
that is not None, which it can then use to reconstitute the source object
from the moniker.  You cold get around this with sub-transactions, 
but there is an easier way. 

Rather than going through monikers, it would easier to use manage_clone 
which copies and pastes objects directly.

I think you want something like:

  def candp(fobj, tobj, REQUEST):
    "Copy and paste"

    for id in fobj.objectIds():
       tobj.manage_clone(getattr(fobj,id),id,REQUEST)

Note that you need pass REQUEST for security purposes, 
since manage_clone tries to make sure that you are allowed
to add the objects to the destination.

Jim

--
Jim Fulton           mailto:jim@digicool.com   Python Powered!        
Technical Director   (888) 344-4332            http://www.python.org  
Digital Creations    http://www.digicool.com   http://www.zope.org    

Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email
address may not be added to any commercial mail list with out my
permission.  Violation of my privacy with advertising or SPAM will
result in a suit for a MINIMUM of $500 damages/incident, $1500 for
repeats.