Missing Images
Victor Subervi
victorsubervi at gmail.com
Sat Dec 26 16:31:12 EST 2009
On Sat, Dec 26, 2009 at 11:28 AM, Carsten Haese <carsten.haese at gmail.com>wrote:
> It's hard to say what you have missed, since you're not giving us nearly
> enough information to determine what you haven't missed. So, we can now
> either beg for more information, make random guesses, or point you at a
> code example that shows how file uploads are done with the cgi module.
> I'll do the latter:
>
> http://webpython.codepoint.net/cgi_file_upload
>
Right. Thank you again. I'd forgotten to put in
enctype="multipart/form-data". Now I have the following snipped:
for pic in ourPics:
sql = 'update %s set pic%d=%s where ID="%s";' % (store, i,
(MySQLdb.Binary(pic),), id)
print sql
# cursor.execute(sql)
Which prints to screen the following:
insert into products (SKU, Category, Name, Title, Description, Price,
SortFactor, Availability, OutOfStock, ShipFlatFee, ShipPercentPrice,
ShipPercentWeight, Associations, TempPrice, LastDatePrice, Weight, Metal,
PercentMetal, pic0, pic1, sizes, colorsShadesNumbersShort)
values("prodSKU1", "prodCat1", "name1", "title1", "descr", "12.34", "500",
"1", "0", "10.00", "5", "2", "", "1", "2000-01-01", "2.5", "", "20",
"�����JFIF��H�H����
and a bunch more binary data. This fails on insert. If I recall correctly, I
need to convert that binary data into something like (c, array(... How do I
do that? I'll include all code below in case it's necessary.
TIA,
beno
#! /usr/bin/python
import cgitb; cgitb.enable()
import MySQLdb
import cgi
import string
import sys,os
sys.path.append(os.getcwd())
from login import login
from particulars import addStore, pics, ourOptions
def enterProducts4():
form = cgi.FieldStorage()
store = form.getfirst('store')
print '''Content-Type: text/html\r\n
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>'''
user, passwd, db, host = login()
db = MySQLdb.connect(host, user, passwd, db)
cursor= db.cursor()
both = ''
count = 0
cursor.execute('describe %s;' % store)
temp = cursor.fetchall()
tmp = [itm[0] for itm in cursor]
storeColNames = []
for descrip in tmp:
storeColNames.append(descrip)
exception = ''
colNames = []
colNamesOther = []
ourPics = []
sqlUpdate = []
sqlUpdatePics = []
i = 0
values = []
valuesPics = []
colNamesPics = []
y = 0
whatDo = form.getfirst('whatDo')
sql = 'select last_insert_id() from %s;' % store
cursor.execute(sql)
try:
id = cursor.fetchone()[0]
except:
id = 0 # This is obviously the first insert; since insert, will be
incremented below
if whatDo == 'insert':
id += 1
i = 0
for picsStore, num in pics().iteritems():
if picsStore == store:
numPics = int(num)
while i < numPics:
y += 1
pic = form.getfirst('pic%d' % i)
try:
if len(pic) > 0:
ourPics.append(pic)
else:
try:
sql = 'select %s from %s where ID=%s;' % (storeColNames[i][:4],
store, id)
cursor.execute(sql)
try:
pic = cursor.fetchone()[0].tostring()
ourPics.append(pic)
except:
pass
except: # This means it's an insert, not an update
pass
colNamesPics.append('pic%s' % i)
sqlUpdatePics.append('%s="%s"' % ('pic%s' % i, pic))
except TypeError:
pass # This indicates there is a pic of 0 length
except:
raise
i += 1
i = 0
while i < len(storeColNames):
try:
trueVal = form.getlist(storeColNames[i])
if (len(trueVal) == 0) and (storeColNames[i] != 'ID'):
trueVal = ['']
test = ','.join(trueVal)
if len(test) == 0:
trueVal == ''
colNames.append(storeColNames[i])
if len(trueVal) > 1:
trueVal = string.join(trueVal, ',')
values.append(trueVal)
elif len(trueVal) == 1:
trueVal = '%s' % trueVal[0]
values.append(trueVal)
if len(trueVal) > 0:
sql = '%s="%s"' % (storeColNames[i], trueVal)
sqlUpdate.append(sql)
except:
raise
i += 1
colNames = string.join(colNames[1:], ', ') # We don't want to include the
ID field
values = string.join(values, '", "')
sqlUpdate = string.join(sqlUpdate, ', ')
changePricesOptions = []
for optionsStore, optionsNames in ourOptions().iteritems():
if store == optionsStore:
for option in optionsNames:
check = form.getfirst('%sChangePrice' % option)
if check != '':
changePricesOptions.append('%s' % option)
try:
if whatDo == 'update':
sql = 'update %s set %s where ID="%s";' % (store, sqlUpdate,
form.getfirst('id'))
cursor.execute(sql)
i = 0
for pic in ourPics:
sql = 'update %s set pic%d=%s where ID=%s;' % (store, i, '%s', id)
cursor.execute(sql, (MySQLdb.Binary(pic[int(i)]),), )
db.commit()
i += 1
elif whatDo == 'insert':
cursor.execute('show tables like "%PersonalData";')
personalDataTables = [itm[0] for itm in cursor]
sql = 'create table if not exists relationships%s (%sID tinyint(5) not
null primary key, ' % (store[0].upper() + store[1:], store[0].upper() +
store[1:])
sql2 = 'insert into relationships%s values ("%s", ' %
(store[0].upper() + store[1:], id)
for dataTable in personalDataTables:
sql += '%sID tinyint(5) unsigned not null, ' % (dataTable[0].upper()
+ dataTable[1:-12])
sql2 += '"%s", ' % form.getfirst(dataTable[:-12])
sql = '%s);' % sql[:-2]
# cursor.execute(sql)
sql2 = '%s);' % sql2[:-2]
# cursor.execute(sql2)
sql = 'insert into %s (%s) values("%s");' % (store, colNames, values)
print sql
# cursor.execute(sql)
i = 0
print ourPics
for pic in ourPics:
sql = 'update %s set pic%d=%s where ID="%s";' % (store, i,
(MySQLdb.Binary(pic),), id)
print sql
# cursor.execute(sql)
i += 1
except MySQLdb.IntegrityError:
exception = 'I\'m sorry, but the SKU you entered is already in use in
another entry. Please click the \'back\' button and choose another number.'
raise
except MySQLdb.OperationalError:
exception = 'I\'m sorry, but you have added an illegal character to a
number (or in "SKU", "price", "weight", "% Metal", "flat shipping fee",
"shipping fee based on % of price" or "shipping fee based on weight").
Please click the \'back\' button and enter a correct number.'
raise
except:
exception = 'I\'m sorry, but there has been an error. Please click the
\'back\' button and try and figure out what went wrong.'
print 'Insert/update successful!<br />'
if len(changePricesOptions) == 0:
if exception == '':
head = '<META HTTP-EQUIV="refresh"
content="5;URL=enterProducts.py?store=%s">\n' % store
head += '</head>\n<body>\n'
else:
head = '</head>\n<body>\n'
html = '<i>This page is going to refresh to the principle page of the
shopping cart in 5 seconds.</i>'
else:
head = "<meta http-equiv='refresh' content='0;
url=enterOptionsPrices.py?store=%s&options=%s&whatDo=%s'>\n" % (store,
','.join(changePricesOptions), whatDo)
head += '</head>\n<body>\n'
html = ''
html += '<body>\n</html>\n'
print head, exception, html
db.commit()
cursor.close()
enterProducts4()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091226/c5bd7f09/attachment-0001.html>
More information about the Python-list
mailing list