[Zope] Image Uploading and Adding Properties?

Ross J. Reedstrom reedstrm@wallace.ece.rice.edu
Mon, 14 Jun 1999 10:29:25 -0500


On Sun, Jun 13, 1999 at 01:27:02PM -0100, Tom Schwaller wrote:
> Hi,
> can someone give me a simple example how to do the following:
> 
> I`d like to have an enhanced Image-Upload-Form, which takes the image
> (on the local file system) you choose, adds some properties to this
> image
> which you can fill in the Form (a few text strings like URL e.g.) and 
> returns the reply I wish (not this ok-Buttons..).

Tom - 
I've done something similar with a generic file upload/exchange site. I
wanted to tag each File with the (authenticated) user who uploaded
it. There's three tricks involved. The first is the standard Zopeism of
having a user interface Form method/page, which then calls a non-interface
Action method/page. The second is to discover that the various manage_*
methods will return to your method if called without a REQUEST variable
being set. The third to another Zopeism: using the :method tag on a FORM
input NAME to call a method, but not have it show up in the URL. That way,
all these file/object management pages bookmark as my main interface page.

A couple notes about my (ugly, but functional) methods below:
It still implements a click through 'ok', but it returns to my page, not
the standard management interface.

I've still got some functionality to implement, (i.e. the comment in
the code about name collisions), but I think I'm bumping up against what
should be done in DTML and what should be moved into external methods.

Anyone have any suggestions on improving this code, feel free to comment.

Here's my addFileForm (mostly stolen from the imageUpload.dtml source):

<!--#var standard_html_header-->

<FORM ACTION="<!--#var URL1-->" METHOD="POST"
      ENCTYPE="multipart/form-data">
<TABLE CELLSPACING="2">
<TR>
  <TD ALIGN="LEFT" VALIGN="TOP">
  <STRONG>File to upload:</STRONG>
  </TD>
  <TD ALIGN="LEFT" VALIGN="TOP">
  <INPUT TYPE="file" NAME="file" SIZE="25" VALUE="">
  </TD>
</TR>
<TR>
<TD></TD>
<TD>
<BR><INPUT TYPE="SUBMIT" NAME="addFileAction:method" VALUE=" Add ">
</TD>
</TR>
</TABLE>
</FORM>

<!--#var standard_html_footer-->

And here's addFileAction:

<!--#var standard_html_header-->
<!--#comment--> 
We need to set the 'id' to the filename if it's not set. The second line
strips the last component from the filename passed in the REQUEST.file
object, in a platform neutral hack. It fails if someone uses the
path seperator component from one platforn in the filename on another
platform. E.g. slashed dates in filenames on Macs. I stole it from cookId
in Image.py

<!--#/comment-->
<!--#unless REQUEST['id']-->
   <!--#call "REQUEST.set('filename',REQUEST['file'].filename)"-->
   <!--#call  "REQUEST.set('id',filename[_.max(
                        _.string.rfind(filename, '/'),
                        _.string.rfind(filename, '\\'),
                        _.string.rfind(filename, ':'),
                        )+1:])"-->
<!--#/unless-->

<!--#if "REQUEST['id'] in objectIds()"-->
<H4>Sorry! That name exists, try again (use your browser's back button)</H4>
<!--#comment--> fix this up
Would you like to:
New Name:
rename existing file
replace existing file
Upload with new name
<!--#/comment-->
<!--#else-->
<!--#call "manage_addFile(id=REQUEST['id'],file=REQUEST['file'])"-->
<!--#call "_.getitem(REQUEST['id']).manage_addProperty(
                   'addedby',REQUEST.get('AUTHENTICATED_USER'),
                   'string')"-->
<FORM ACTION=<!--#var "URL1"--> METHOD="GET">
<H3>File Added</H3>
<INPUT type=submit value="OK">
</FORM>

<!--#/if-->

<!--#var standard_html_footer-->

-- 
Ross J. Reedstrom, Ph.D., <reedstrm@rice.edu> 
NSBRI Research Scientist/Programmer
Computer and Information Technology Institute
Rice University, 6100 S. Main St.,  Houston, TX 77005