Description:
It's very handy to be able to move zope objects around from python.
You can save yourself a lot of point and click in the ZMI.
For example, today I had to restructure a folder heirarchy.
The things I was moving all had the same name and were
in parallel folders. I would have had to go into each
directory, copy or cut the object in question, then paste
it where I wanted it. That's a lot of clicks.
A quick little python script does the job. Below are presented
the methods you need to know, and an example i've actually used.
##### to copy something from a folder
copy_info = some_folder.manage_copyObjects(('object_foo',
'object_bar',
'object_baz')
)
# The argument to manage_copyObjects is a tuple of object IDs which must exist
# in the folder where you call the method.
# The returned result is clipboard data, suitable for putting in a cookie.
# but we don't need cookies if we're copying and pasting in one
# script.
## If you WANT the clipboard in a cookie, maybe because you're
# going to paste it later in some other script, you need to
# pass in a REQUEST like so:
copy_info = some_folder.manage_copyObjects(('object_foo',
'object_bar',
'object_baz'),
REQUEST
)
# The cookie is set in REQUEST.RESPONSE.
#### To cut stuff from a folder
copy_info = some_folder.manage_cutObjects(('object_foo',
'object_bar',
'object_baz')
)
# ... it's just like manage_copyObjects
#### To delete things completely - no cut, no paste, just gone
some_folder.manage_delObjects(('object_foo',
'object_bar')
)
#### To paste the result of a cut or copy into a folder
some_other_folder.manage_pasteObjects(copy_info)
########## A complete copy / paste example
# get the source and destination parent folders
dest_base = context.restrictedTraverse('/foo/bar/baz')
src_base = context.restrictedTraverse('/fool/bear/booze')
folds = src_base.objectItems('Folder') # where the objects to copy live
for src_id, src_obj in folds:
# prepare the destination
try:
# we might have run the script already, or the destination
# might just exist already.
dest = getattr(dest_base, src_id)
print 'folder exists already',
except AttributeError:
# make sure the destination is there.
dest_base.manage_addProduct['OFSP'].manage_addFolder(src_id, '')
dest = getattr(dest, s_id)
print dest.absolute_url()
# now the real work, actually quite easy
try:
copy_info = src_obj.manage_copyObjects(('stylesheet_properties',))
print " copied...",
d.manage_pasteObjects(copy_info)
print "pasted!"
except:
print "...couldn't paste it there."
return printed
Comments:
help me by malice120 - 2004-10-14
i want to move item news from a folder to another folder depending of the subjet of the news and automatically after adding the news!