quick search:

file uploader with extensions

Submitted by: edwardcass
Last Edited: 2001-10-03

Category: Python(Script)

Average rating is: 4.0 out of 5 (3 ratings)

Much more the black box now, Perl's been replaced with Python (per runyaga's request).
Now, all you have to do is change the input fields in formUploadFields and you'll
have those properties attached (as many as you'd like)... oh, and it submits the objects
to the catalog at the same time now (thanks to dawa, appearing daily on the Zope
irc channel).

Next step with this is to extend it with logic that directs the script for
where the files will end up since several different forms within Zope could be using
this functionality, it needs to know where to put files and possibly which catalog
to attach them to. But since that will be system specific, we won't bother with it here.

Source (Text):
first, make a DTML method "preUpload":

<dtml-var standard_html_header>
<h2><dtml-var title_or_id> <dtml-var document_title></h2>

<form method="post" action="uploadForm"

<b>How many images would you like to upload?</b><br><br>
<select name="many_files">

        <dtml-var "option_iterator(`1`, `100`)">


<input type=submit method=submit>

<dtml-var standard_html_footer>

Was Perl now Python of a different name: option_iterator... neat little thing that you might
find handy for saving time when building date like forms...
final = ''
for n in range(int(start), int(stop)):
        final = final + '<option>' + str(n) + '</option>'
return final

Okay, so now you have a simple form for choosing how many files you plan to upload. The 'uploadForm' DTML method calls a Python method 'fieldsLoop' which calls a DTML method called 'uploadFormFields', iterates over it (many_fields times) and replaces XML with ints...

import string
final = ''
work = context.fileUploadFields()
text = ''
for x in work:
        text = text + x

for n in range(int(stop)):
        w = n + 1 ## to change first option from being "0" and still have iteration right...
        final = final + string.replace(text, "<counter>", str(w))
return final

<dtml-var standard_html_header>
<h2><dtml-var title_or_id> <dtml-var document_title></h2>

<form method="post" action="upload_files"
<table width="100%" border=1>

<dtml-var "fieldsLoop(many_files)">
        <td align=center>
<input type=submit method=submit>

<dtml-var standard_html_footer>

DTML method uploadFormFields: you'll notice that I've changed the input scheme
from last time to make it easier to index later...

File <counter>- id:       <input type=text name="file.id.<counter>"> <br>
File <counter>- contents: <input type=file name="file.name.<counter>">  <br>
Show Type: <select name="file.property.show_type.<counter>">
        <option>Talk Show</option>
        <option>News Show</option>
        <option>Hippie Poetry</option>
Word1: <input type=text name="file.property.word_one.<counter>"><br>

Okay, I wanted to do the addFile and addProperty in the same loop to save time, but the files weren't always there in
time to add properties... go figure, 2 loops (for inputting to dict and for pulling out)

import string
insert = {}
ids = {}
props = {}
filenames = {}

for key in context.REQUEST.keys():
        if key[:8] == 'file.id.':
                if len(key) == 9:
                        this_key = key[8:9]
                        this_key = key[8:10]
                ids[this_key] = REQUEST[key] ## id# -> value
        if key[:10] == 'file.name.':
                if len(key) == 11:
                        this_key = key[10:11]
                        this_key = key[9:11]
                filenames[this_key] = REQUEST[key]
        if key[:14] == 'file.property.':
                if key[-2:-1] == '.':
                        work = len(key) - 2
                        work = len(key) - 3
                properties = {}
                properties[key[14:work]] = REQUEST[key]
                this_key = key[(work + 1):len(key)]

                if props.has_key(this_key):
                        worker = []
                        props[this_key] = worker
                        props[this_key] = properties
for k in ids.keys():
        context.manage_addFile(ids[k], filenames[k])
        print 'added file ', ids[k]
        thisObject = getattr(context,ids[k])

for k in props.keys():
        for n in props[k]:
                for w in n.keys():
                        getattr(context, ids[k]).manage_addProperty(w, n[w], 'string')
                        print 'added property ', w, ' -- ', n[w]

return printed

So, we have our black box now. However, you'll want to add a 'path' attribute to it so that
it knows where to put the file, yada yada yada.

Thanks to runyaga for his help.

Thanks to dawa for the catalog extension.


No Comments