[Zope] Re: Zope File Library help

J Cameron Cooper jccooper at jcameroncooper.com
Mon Aug 11 14:34:49 EDT 2003


> I came across your email  in a zope archive.  I am new to zope and am 
> trying to do something similar.  Actually, something that the example 
> file library in zope does for the most part.  I just want a directory 
> where people can upload files and down load them.  Make changes, and 
> put them back.  The example works great until you try to upload a file 
> that is already there.  It only works to add a new file.  I have spent 
> at least a day trying to figure out exactly how to change the 
> manage_addFile to something that would just overwrite the existing 
> file without any luck.  Did you find a solution?  Do you have an 
> idea?  I have looked at manage_upload and have not figured out how to 
> make it work.
>
> ...
>
> The code for the add script is
> """
> Adds a file to the library.
> """
> from Products.PythonScripts.standard import url_quote
>
> # create the file
> container.Files.manage_addProduct['OFSP'].manage_addFile(id='', 
> title='', file=file)
>
> # create a success message
> message="File '%s' uploaded successfully." % file.filename
>
> # redirect to main page - this is necessary to make all the URLs
> # on the main page work correctly.
> return context.REQUEST.RESPONSE.redirect("%s?message=%s" % 
> (container.absolute_url(), url_quote(message)))

I'm kind of suspicious of the empty string as an id, but assuming that 
makes the id equal to file.filename, then how about::

if container.Files[file.filename]:
  container.Files.manage_delObjects([file.filename])
container.Files.manage_addProduct['OFSP'].manage_addFile(id='', 
title='', file=file)

or, a little more elegantly::

obj = container.Files[file.filename]
if obj is not None:
  obj.update_data(file)
else:
  container.Files.manage_addProduct['OFSP'].manage_addFile(id='', 
title='', file=file)

Where did I find those methods, you ask? Zope Help System > API > File, 
ObjectManager is all I needed.

Also, I'm copying this to the list, so that others can see. It's 
probably best to send things to the list, too. I might have been on 
vacation or hit by a bus or something.

             --jcc

fyi, the second example can probably be shortened to::

if obj = container.Files[file.filename]:
  obj.update_data(file)
else:
  container.Files.manage_addProduct['OFSP'].manage_addFile(id='', 
title='', file=file)

-- 
"My point and period will be throughly wrought,
Or well or ill, as this day's battle's fought."





More information about the Zope mailing list