[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>&nbsp;</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&nbsp;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.&nbsp; Thank you very much.</P>
<P>####Python Script -- processAddItem ####</P>
<P>#parameters: code, description,cost_code,transfer_id=None, origin, location,&nbsp;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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cost_code=cost_code,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transfer_id=transfer_id, origin=origin,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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>&nbsp;&nbsp; imageId = item.id<BR>&nbsp;&nbsp; break</P>
<P><FONT color=#c00000><STRONG>if itemImage != None:<BR>&nbsp;&nbsp; content_type=itemImage.headers['Content-Type']<BR>&nbsp;&nbsp; if content_type.find('image')!=-1: <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; context.images.inventory.manage_addProduct['OFSP'].manage_addImage(id=str(imageId), file=itemImage, title='')<BR>&nbsp;&nbsp; else:<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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>&lt;form action="processAddItem" method="POST" enctype="multipart/form-data"&gt;</P>
<P>&lt;span class="errorMessage" tal:condition="options/error_message | nothing" tal:content="options/error_message"&gt;<BR>Error Message<BR>&lt;/span&gt;<BR>&lt;table class="text" style="font-weight:bold" cellspacing="2" cellpadding="2" border="0"&gt;<BR>&lt;tr&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;font size="1" font-family="tahoma" color="red"&gt;*required fields&lt;/font&gt;&lt;/td&gt;<BR>&lt;/tr&gt;<BR>&lt;tr&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;Code:&lt;font color="red"&gt;*&lt;/font&gt;&lt;/td&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;input type="text" name="code:required"&gt;&lt;/td&gt;<BR>&lt;/tr&gt;<BR>&lt;tr&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;Description:&lt;/td&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;input type="Text" name="description"&gt;&lt;/td&gt;<BR>&lt;/tr&gt;<BR>&lt;tr&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;Cost Code:&lt;/td&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;input type="text" name="cost_code"&gt;&lt;/td&gt;<BR>&lt;/tr&gt;<BR>&lt;tr&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;Transfer Id:&lt;/td&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;input type="text" name="transfer_id:int:ignore_empty"&gt;&lt;/td&gt;<BR>&lt;/tr&gt;<BR>&lt;tr&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;Origin:&lt;/td&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;select name="origin" class="text"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;option value=""&gt;None&lt;/option&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;option tal:repeat="branch container/getBranchList" tal:content="branch" <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tal:attributes="value branch"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; branch<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/option&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/select&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;/td&gt;<BR>&lt;/tr&gt;<BR>&lt;tr&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;Location:&lt;/td&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;select name="location" class="text"&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;option tal:repeat="branch container/getBranchList" tal:content="branch"<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tal:attributes="value branch"&gt; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; branch<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/option&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/select&gt;<BR>&nbsp;&nbsp;&nbsp;&nbsp; &lt;/td&gt;<BR>&lt;/tr&gt;<BR>&lt;tr&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;Remarks:&lt;/td&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;textarea cols="30" rows="10" name="remarks"&gt;&lt;/textarea&gt;&lt;/td&gt;<BR>&lt;/tr&gt;<BR>&lt;tr&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;Image:&lt;/td&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;&lt;input type="file" name="itemImage"&gt;&lt;/td&gt;<BR>&lt;tr&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt;&amp;nbsp;&lt;/td&gt;<BR>&nbsp;&nbsp;&nbsp; &lt;td&gt; &lt;input type="submit" name="submit" value="Add Item"&gt;&lt;/td&gt;<BR>&lt;/tr&gt;<BR>&lt;/table&gt;<BR>&nbsp;<BR>&lt;/form&gt;</P>
<P>&nbsp;</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--