Description:
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>
<p>
<form method="post" action="uploadForm"
enctype="multipart/form-data">
<b>How many images would you like to upload?</b><br><br>
<select name="many_files">
<dtml-var "option_iterator(`1`, `100`)">
</select>
<input type=submit method=submit>
</form>
</p>
<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...
fieldsLoop:
import string
final = ''
work = context.fileUploadFields()
text = ''
for x in work:
text = text + x
for n in range(int(stop)):
w = n + 1
final = final + string.replace(text, "<counter>", str(w))
return final
uploadForm:
<dtml-var standard_html_header>
<h2><dtml-var title_or_id> <dtml-var document_title></h2>
<p>
<form method="post" action="upload_files"
enctype="multipart/form-data">
<table width="100%" border=1>
<dtml-var "fieldsLoop(many_files)">
<tr>
<td align=center>
<input type=submit method=submit>
</td>
</tr>
</table>
</form>
<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...
<tr>
<td>
<br><br>
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>Music</option>
<option>Hippie Poetry</option>
</select><br>
Meta-data:
Word1: <input type=text name="file.property.word_one.<counter>"><br>
<br>
</td>
</tr>
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
REQUEST=context.REQUEST
insert = {}
ids = {}
props = {}
filenames = {}
for key in context.REQUEST.keys():
if key[:8] == 'file.id.':
if len(key) == 9:
this_key = key[8:9]
else:
this_key = key[8:10]
ids[this_key] = REQUEST[key]
if key[:10] == 'file.name.':
if len(key) == 11:
this_key = key[10:11]
else:
this_key = key[9:11]
filenames[this_key] = REQUEST[key]
if key[:14] == 'file.property.':
if key[-2:-1] == '.':
work = len(key) - 2
else:
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 = []
worker.append(props[this_key])
worker.append(properties)
props[this_key] = worker
else:
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])
context.Catalog.catalog_object(thisObject,string.join(thisObject.getPhysicalPath(),'/'))
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
|