[Zope] python script executing twice?
Karen Yang
chefky77@yahoo.com
Sun, 29 Sep 2002 23:07:24 -0700 (PDT)
--0-1005360708-1033366044=:97173
Content-Type: text/plain; charset=us-ascii
I have an 'Add Item' form which allows the user to add item information, as well as upload a file. I have a python script that processes the form, but I'm getting some weird behavior because it seems to be executing twice. I know this because it gives me an integrity error; meaning, it has executed my insert query twice. I feel there is something wrong with my script because when I comment out the block that processes the image upload (in boldface red below), I don't get the integrity error. In addition, when I test the script in the 'Test' section, it works. I would appreciate any ideas you may have. The code is pasted below. Thank you very much.
####Python Script -- processAddItem ####
#parameters: code, description,cost_code,transfer_id=None, origin, location, remarks,itemImage
#process form with item details and image upload
from Products.PythonScripts.standard import url_quote
REQUEST=context.REQUEST
#insert try here
#call insert zsql method
context.InventoryDB.insertItem(code=code, description=description,
cost_code=cost_code,
transfer_id=transfer_id, origin=origin,
location=location, remarks=remarks)
#query for id so that you can name image that
results = context.InventoryDB.getByCode(code=code)
imageId = -1
for item in results:
imageId = item.id
break
if itemImage != None:
content_type=itemImage.headers['Content-Type']
if content_type.find('image')!=-1:
context.images.inventory.manage_addProduct['OFSP'].manage_addImage(id=str(imageId), file=itemImage, title='')
else:
context.images.inventory.manage_addProduct['OFSP'].manage_addFile(id=str(imageId), file=itemImage, title='')
# create a success message
message="Item '%s' added successfully." % code
# 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)))
#### Page Template code:####
<form action="processAddItem" method="POST" enctype="multipart/form-data">
<span class="errorMessage" tal:condition="options/error_message | nothing" tal:content="options/error_message">
Error Message
</span>
<table class="text" style="font-weight:bold" cellspacing="2" cellpadding="2" border="0">
<tr>
<td><font size="1" font-family="tahoma" color="red">*required fields</font></td>
</tr>
<tr>
<td>Code:<font color="red">*</font></td>
<td><input type="text" name="code:required"></td>
</tr>
<tr>
<td>Description:</td>
<td><input type="Text" name="description"></td>
</tr>
<tr>
<td>Cost Code:</td>
<td><input type="text" name="cost_code"></td>
</tr>
<tr>
<td>Transfer Id:</td>
<td><input type="text" name="transfer_id:int:ignore_empty"></td>
</tr>
<tr>
<td>Origin:</td>
<td><select name="origin" class="text">
<option value="">None</option>
<option tal:repeat="branch container/getBranchList" tal:content="branch"
tal:attributes="value branch">
branch
</option>
</select>
</td>
</tr>
<tr>
<td>Location:</td>
<td><select name="location" class="text">
<option tal:repeat="branch container/getBranchList" tal:content="branch"
tal:attributes="value branch">
branch
</option>
</select>
</td>
</tr>
<tr>
<td>Remarks:</td>
<td><textarea cols="30" rows="10" name="remarks"></textarea></td>
</tr>
<tr>
<td>Image:</td>
<td><input type="file" name="itemImage"></td>
<tr>
<td> </td>
<td> <input type="submit" name="submit" value="Add Item"></td>
</tr>
</table>
</form>
---------------------------------
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
--0-1005360708-1033366044=:97173
Content-Type: text/html; charset=us-ascii
<P>I have an 'Add Item' form which allows the user to add item information, as well as upload a file. I have a python script that processes the form, but I'm getting some weird behavior because it seems to be executing twice. I know this because it gives me an integrity error; meaning, it has executed my insert query twice. I feel there is something wrong with my script because when I comment out the block that processes the image upload (in boldface red below), I don't get the integrity error. In addition, when I test the script in the 'Test' section, it works. I would appreciate any ideas you may have. The code is pasted below. Thank you very much.</P>
<P>####Python Script -- processAddItem ####</P>
<P>#parameters: code, description,cost_code,transfer_id=None, origin, location, remarks,itemImage</P>
<P>#process form with item details and image upload<BR>from Products.PythonScripts.standard import url_quote</P>
<P>REQUEST=context.REQUEST</P>
<P>#insert try here<BR>#call insert zsql method</P>
<P>context.InventoryDB.insertItem(code=code, description=description,<BR> cost_code=cost_code,<BR> transfer_id=transfer_id, origin=origin,<BR> location=location, remarks=remarks)</P>
<P>#query for id so that you can name image that<BR>results = context.InventoryDB.getByCode(code=code)<BR>imageId = -1</P>
<P>for item in results:<BR> imageId = item.id<BR> break</P>
<P><FONT color=#c00000><STRONG>if itemImage != None:<BR> content_type=itemImage.headers['Content-Type']<BR> if content_type.find('image')!=-1: <BR> context.images.inventory.manage_addProduct['OFSP'].manage_addImage(id=str(imageId), file=itemImage, title='')<BR> else:<BR> context.images.inventory.manage_addProduct['OFSP'].manage_addFile(id=str(imageId), file=itemImage, title='')</STRONG></FONT></P>
<P><BR># create a success message<BR>message="Item '%s' added successfully." % code</P>
<P># redirect to main page - this is necessary to make all the URLs<BR># on the main page work correctly.<BR>return context.REQUEST.RESPONSE.redirect("%s?message=%s" % (container.absolute_url(), url_quote(message)))<BR></P>
<P>#### Page Template code:####</P>
<P><form action="processAddItem" method="POST" enctype="multipart/form-data"></P>
<P><span class="errorMessage" tal:condition="options/error_message | nothing" tal:content="options/error_message"><BR>Error Message<BR></span><BR><table class="text" style="font-weight:bold" cellspacing="2" cellpadding="2" border="0"><BR><tr><BR> <td><font size="1" font-family="tahoma" color="red">*required fields</font></td><BR></tr><BR><tr><BR> <td>Code:<font color="red">*</font></td><BR> <td><input type="text" name="code:required"></td><BR></tr><BR><tr><BR> <td>Description:</td><BR> <td><input type="Text" name="description"></td><BR></tr><BR><tr><BR> <td>Cost Code:</td><BR> <td><input type="text" name="cost_code"></td><BR></tr><BR><tr><BR> <td>Transfer Id:</td><BR> <td><input type="text" name="transfer_id:int:ignore_empty"></td><BR></tr><BR><tr><BR> <td>Origin:</td><BR> <td><select name="origin" class="text"><BR> <option value="">None</option><BR> <option tal:repeat="branch container/getBranchList" tal:content="branch" <BR> tal:attributes="value branch"><BR> branch<BR> </option><BR> </select><BR> </td><BR></tr><BR><tr><BR> <td>Location:</td><BR> <td><select name="location" class="text"><BR> <option tal:repeat="branch container/getBranchList" tal:content="branch"<BR> tal:attributes="value branch"> <BR> branch<BR> </option><BR> </select><BR> </td><BR></tr><BR><tr><BR> <td>Remarks:</td><BR> <td><textarea cols="30" rows="10" name="remarks"></textarea></td><BR></tr><BR><tr><BR> <td>Image:</td><BR> <td><input type="file" name="itemImage"></td><BR><tr><BR> <td>&nbsp;</td><BR> <td> <input type="submit" name="submit" value="Add Item"></td><BR></tr><BR></table><BR> <BR></form></P>
<P> </P><p><br><hr size=1>Do you Yahoo!?<br>
New <a href="http://rd.yahoo.com/evt=1207/*http://sbc.yahoo.com/">DSL Internet Access</a> from SBC & Yahoo!</a>
--0-1005360708-1033366044=:97173--