Hi;<br>I think I finally have an interesting problem for y'all. I need to import a script from a lower dir, forcing me to change dirs:<br><br>cwd = os.getcwd()<br>
os.chdir('%s/..' % cwd)<br>
sys.path.append(os.getcwd())<br>
from templateFrame import top, bottom<br>
os.chdir(cwd)<br>
<br>Because I've found I must do my form evaluations *before* changing dir as above, I am forced to call these values as globals:<br><br>form = cgi.FieldStorage()<br>
store = form.getfirst('store')<br>
cat = form.getfirst('cat')<br>
id = form.getfirst('id')<br>
pkg = form.getfirst('pkg')<br>
patientID = form.getfirst('patientID')<br>
<br>Now, apparently because of python's problem with globals, when I call "id" as follows:<br><br>cursor.execute('select ProductID from productsPackages where PackageID=%s' % id)<br><br>I get the following error:<br>
<br>/var/www/html/<a href="http://angrynates.com/cart/Store_frame2.py">angrynates.com/cart/Store_frame2.py</a><br> 135 cursor.close()<br> 136 bottom()<br> 137 <br> 138 Store_frame2()<br> 139 <br>Store_frame2 = <function Store_frame2><br>
/var/www/html/<a href="http://angrynates.com/cart/Store_frame2.py">angrynates.com/cart/Store_frame2.py</a> in Store_frame2()<br> 119 printAProduct()<br> 120 else:<br> 121 cursor.execute('select ProductID from productsPackages where PackageID=%s' % id)<br>
122 for id in [itm[0] for itm in cursor]:<br> 123 printAProduct(id)<br>global cursor = <MySQLdb.cursors.Cursor object>, cursor.execute = <bound method Cursor.execute of <MySQLdb.cursors.Cursor object>>, global id = '1'<br>
<br>UnboundLocalError: local variable 'id' referenced before assignment<br> args = ("local variable 'id' referenced before assignment",) <br><br>Full code below. Please advise. <br>TIA,<br>beno<br>
<br>#! /usr/bin/python<br><br>import string<br>import cgitb; cgitb.enable()<br>import cgi<br>import MySQLdb<br>import sys,os<br>from sets import Set<br>import fpformat<br>cwd = os.getcwd()<br>sys.path.append(cwd)<br>from login import login<br>
from particulars import truncate<br><br>form = cgi.FieldStorage()<br>store = form.getfirst('store')<br>cat = form.getfirst('cat')<br>id = form.getfirst('id')<br>pkg = form.getfirst('pkg')<br>
patientID = form.getfirst('patientID')<br><br>try:<br> browser = form.getfirst('browser', 'all')<br>except:<br> browser = headers()<br><br>os.chdir('%s/..' % cwd)<br>sys.path.append(os.getcwd())<br>
from templateFrame import top, bottom<br>os.chdir(cwd)<br><br>ourFile = string.split(__file__, "/")<br>p = ourFile[len(ourFile) - 1]<br>p = p[: - 9]<br>site = ourFile[4][:-10]<br>if site != '':<br> site = site[:-1]<br>
<br>user, passwd, db, host = login()<br>db = MySQLdb.connect(host, user, passwd, db)<br>cursor= db.cursor()<br><br>cursor.execute('describe %s;' % store)<br>descr = cursor.fetchall()<br>cols = []<br>for elt in descr:<br>
cols.append(elt[0])<br><br>def printAProduct(id=id):<br> lastID = ''<br> cursor.execute('select * from %s where ID="%s"' % (store, id))<br> vals = cursor.fetchone()<br> prodDict = dict(zip(cols, vals))<br>
for col, item in prodDict.iteritems():<br> if col == 'ID':<br> print '<input type="hidden" name="prodid" value="%s" />' % item<br> try: # Stores such as products (but not prescriptions) will pass through<br>
from particulars import ourOptions<br> if col in ourOptions():<br> print '<input type="hidden" name="%s" value="%s" />' % (col, item)<br> except:<br> pass<br>
if col[:3] != 'pic':<br> notSet = 1<br> if isinstance(item, (str, int, long, float, long, complex, unicode, list, buffer, xrange, tuple)):<br> print '<b>%s: </b>%s<br />\n' % (col, item)<br>
else:<br> try:<br> html = "<b>%s</b>: <select name='%s'>" % (col, col)<br> notSet = 0<br> for itm in item:<br> try:<br> color, number = itm.split('_')<br>
html += "<option value='%s'>%s</option>" % (itm, color)<br> except:<br> html += "<option value='%s'>%s</option>" % (itm, itm)<br>
html += "</select><br />"<br> print html<br> except:<br> pass<br># if notSet == 1:<br># if len(col) > 49:<br># colX = col[:50] + '...'<br>
# else:<br># colX = col<br># print '<b>%s: </b>%s<br />\n' % (colX, item)<br> elif col == 'pic1':<br> try:<br> if (item != None):<br># if (item != None) and (len(item > 0)):<br>
print '<a href="getpic.py?store=%s&pic=%s&id=%s" class="highslide" href="getpic.py?store=%s&pic=%s&id=%s" onclick="return hs.expand(this)"><img src="getpic.py?store=%s&pic=%s&id=%s" width="100" height="80" alt="" align="left" border="0" title="Click to enlarge" style="border: 0px"></a><br clear="all" />\n' % (store, col[3:], id, store, col[3:], id, store, col[3:], id)<br>
except TypeError:<br> raise<br> except:<br> raise<br># i += 1<br># try:<br># content = item[x].tostring()<br># pic = "tmp" + str(i) + ".jpg"<br># try:<br>
# os.remove(pic)<br># except:<br># pass<br># img = open(pic, "w")<br># img.write(content)<br># print '<img src="%s"><br /><br />' % pic<br>
# img.close()<br># except:<br># pass<br><br>def Store_frame2():<br> top(browser, p)<br> i = 0<br> print '<tr>\n'<br> print '<form method="post" action="cart.py">'<br>
print "<input type='hidden' name='store' value='%s' />" % store<br> print "<input type='hidden' name='whatDo' value='insert' />"<br> if pkg == 'no':<br>
printAProduct()<br> else:<br> cursor.execute('select ProductID from productsPackages where PackageID=%s' % id)<br> for id in [itm[0] for itm in cursor]:<br> printAProduct(id)<br> if store == 'prescriptions':<br>
print "<input type='hidden' name='quantity' value='1' />"<br> print "<input type='submit' value=' Refill ' />"<br> elif truncate()[store] != '':<br>
pass<br> else:<br> print "<b>Quantity</b>: <input type='text' name='quantity' width='3' maxlength='3' /><br />"<br> print "<input type='submit' value=' Add To Cart ' />"<br>
print '</form>'<br> print '</td></tr></table>\n'<br> print '</table>\n'<br> cursor.close()<br> bottom()<br><br>Store_frame2()<br><br clear="all"><br>-- <br>The Logos has come to bear<br>
<a href="http://logos.13gems.com/">http://logos.13gems.com/</a><br>