[Zope] Upload files, set properties, search props

Troy Farrell troy@entheossoft.com
Mon, 19 May 2003 23:35:53 -0500


A few notes for you:
1) put this in a python script - it's much cleaner.  The code below 
would become

context.objects.manage_addFile(id='', file=file, title='test')
context.objects.manage_addProperty('keywords', keywords, 'text')

2) You are exactly right about your idea.  Here's the trick.  Look at 
$ZOPEDIR/lib/python/OFS/Image.py and find the function "manage_addFile". 
  You see that in there it calls

id, title = cookId(id, title, file)

so, we need to see what cookId() does.  Searching at the bottom of 
Image.py reveals this code:

def cookId(id, title, file):
     if not id and hasattr(file,'filename'):
         filename=file.filename
         title=title or filename
         id=filename[max(filename.rfind('/'),
                         filename.rfind('\\'),
                         filename.rfind(':'),
                         )+1:]
     return id, title

So, we see that cookId reads the HTTP POST file object, and marshalls 
it's filename into a workable Id.  You need to duplicate this 
functionality if you want to use manage_addFile with a blank id.  I 
would setup an External Method like:

from OFS.Image import cookId

def findFileId(title, file):
   "All good methods have docstrings :)"
   return cookId('', title, file)[0]

I think that will work (it might need some tweaking...)  Then your 
python script becomes

context.objects.manage_addFile(id='', file=file, title='test')
fileId = context.externalMethodFromAboveCalledFindFileId('test', file)
context.objects[fileId].manage_addProperty('keywords', keywords, 'text')

I hope I didn't just do your homework for you ;-)

Troy

John Toews wrote:
> Thank you, pointing out I could do this without a Zclass is going to
> save me time... The one thing I don't understand right now. The code
> below adds a property to the containing folder, not the object I just
> uploaded. How do I obtain the id so I can deal with that? (objects is
> the name of my folder, it is a level under the script itself)
> 
> <dtml-call expr="objects.manage_addFile(id='', file=file,
> title='test')">
> <dtml-call expr="objects.manage_addProperty('keywords', keywords,
> 'text')">
> 
> I think the second one should be something like
> objects.UPLOADED_FILE_ID.manage_addProperty...
> 
> Thanks!
> 
> -----Original Message-----
> From: Paul Winkler [mailto:pw_lists@slinkp.com] 
> Sent: Monday, May 19, 2003 11:46 AM
> To: zope@zope.org
> Subject: Re: [Zope] Upload files, set properties, search props
> 
> 
> On Mon, May 19, 2003 at 02:57:46PM -0500, John Toews wrote:
> 
>>Hello All,
>>
>>I am (very) new to Zope and am trying to figure out how exactly to
>>implement what I want. I have read most of the Zope book and it
> 
> doesn't
> 
>>seem to go into the depths I want, but please feel free to point me to
>>better manual(s). What I want to be able to do is:
>>
>>- Present the user a form with to upload a file and, for now, a text
> 
> box
> 
>>for keywords (I've done this already)
>>- Save the file to a folder and set a property for the keywords (I can
>>save to a folder with the generic File object [using
>>foldername.manage_addFile], but how do I create & set the keyword
>>property)
> 
> 
> the details vary depending on whether you want 1 property per keyword,
> or 1 property for all keywords. but the basics can be gleaned from
> the online zope book:
> http://www.zope.org/Documentation/Books/ZopeBook/2_6Edition/ScriptingZop
> e.stx
> 
> read the section on "The Zope API". There are examples of adding and
> changing properties.
> 
> e.g. in the script that's the target of your form, do something like
> this
> to add a single property:
> 
> request = context.REQUEST
> my_folder.manage_addProperty('keywords', request.get('keywords'),
> 'text')
> 
> 
> 
>>- Catalog the keywords property and be able to search it (Haven't even
>>attempted yet)
> 
> 
> no problem, just give youtr catalog a KeywordIndex with the name of your
> property.
> 
> 
> 
>>I believe the "correct" way to do this would be a Zclass using the
> 
> File
> 
>>base class and a property sheet.
> 
> 
> not necessary, the above will work.
> 
> the only advantage of a custom Product (whether zclass or
> filesystem-based
> product) is that it can be made to support automatic cataloging
> so you don't have to periodically update the catalog.
>