Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- the calling html:
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <HTML>
- <HEAD>
- <style type="text/css">
- td {height: 15; border:1; cellspacing:0; cellpadding:0}
- tr {border:1; cellspacing:0; cellpadding:0}
- body{background-color:white;}
- input{border:0; cellspacing:0; cellpadding:0}
- input.enter {background-color:PaleTurquoise; border:1; cellspacing:0; cellpadding:0}
- input.serieA {background-color:PaleTurquoise}
- input.serieB {background-color:white}
- table{border:1;}
- </style>
- <BODY>
- <FORM method="POST" action="http://localhost/cgi-bin/quanteditor.py">
- <input class="enter" size="15" type="text" name="Referencia">
- <input size="15" type="submit" value="Consulta" name="submit1">
- <input size="15" type="submit" value="Grabar los cambios" name="submit2">
- </BODY>
- </HTML>
- the python script, quanteditor.py:
- #!c:\Python26\python.exe
- import cgi
- from sys import exit
- import MySQLdb
- import urllib
- # HTML SKELETON SECTION
- def header(title):
- print "Content-type: text/html\n"
- print """<HTML><HEAD><style type="text/css">
- td {height: 15; border:1; cellspacing:0; cellpadding:0}
- tr {border:1; cellspacing:0; cellpadding:0}
- body{background-color:white;}
- input{border:0; cellspacing:0; cellpadding:0}
- input.enter {background-color:PaleTurquoise; border:1; cellspacing:0; cellpadding:0}
- input.serieA {background-color:PaleTurquoise}
- input.serieB {background-color:white}
- table{border:1;}
- </style><TITLE>"%s"</TITLE>\n</HEAD>\n<BODY>\n""" % (title)
- def bodyform():
- print """<FORM method="POST" action="http://localhost/cgi-bin/quanteditor.py">"""
- print """<input class="enter" size="15" type="text" name="Referencia">"""
- print """<input size="15" type="submit" value="Consulta" name="submit1">"""
- print """<input size="15" type="submit" value="Grabar los cambios" name="submit2">"""
- def footer():
- print """</form>"""
- print "</BODY></HTML>"
- #DATABASE AND EDIT FUNCTIONS
- def gettable(ref):
- #connect
- try:
- conn = MySQLdb.connect(
- host = 'localhost',
- user = 'root',
- passwd = '',
- db = 'pruebagales')
- except MySQLdb.Error, e:
- print "<pre>%s</pre></body></html>" % e
- exit(1)
- #get values, sql
- cursor = conn.cursor()
- sql="""SELECT color,
- group_concat(if(numero='16 ',cast(quantity as char), null)) as '16',
- group_concat(if(numero='17 ',cast(quantity as char), null)) as '17',
- group_concat(if(numero='18 ',cast(quantity as char), null)) as '18',
- group_concat(if(numero='19 ',cast(quantity as char), null)) as '19',
- group_concat(if(numero='20 ',cast(quantity as char), null)) as '20',
- group_concat(if(numero='21 ',cast(quantity as char), null)) as '21',
- group_concat(if(numero='22 ',cast(quantity as char), null)) as '22',
- group_concat(if(numero='23 ',cast(quantity as char), null)) as '23',
- group_concat(if(numero='24 ',cast(quantity as char), null)) as '24',
- group_concat(if(numero='25 ',cast(quantity as char), null)) as '25',
- group_concat(if(numero='26 ',cast(quantity as char), null)) as '26',
- group_concat(if(numero='27 ',cast(quantity as char), null)) as '27',
- group_concat(if(numero='28 ',cast(quantity as char), null)) as '28',
- group_concat(if(numero='29 ',cast(quantity as char), null)) as '29',
- group_concat(if(numero='30 ',cast(quantity as char), null)) as '30',
- group_concat(if(numero='31 ',cast(quantity as char), null)) as '31',
- group_concat(if(numero='32 ',cast(quantity as char), null)) as '32',
- group_concat(if(numero='33 ',cast(quantity as char), null)) as '33',
- group_concat(if(numero='34 ',cast(quantity as char), null)) as '34',
- group_concat(if(numero='35 ',cast(quantity as char), null)) as '35',
- group_concat(if(numero='36 ',cast(quantity as char), null)) as '36',
- group_concat(if(numero='37 ',cast(quantity as char), null)) as '37',
- group_concat(if(numero='38 ',cast(quantity as char), null)) as '38',
- group_concat(if(numero='39 ',cast(quantity as char), null)) as '39'
- from
- (
- SELECT substring(group_concat(PAL.name order by A.id_attribute_group desc separator ' '),3) as color,
- substring(group_concat(PAL.name order by A.id_attribute_group desc separator ' '),1,3) as numero, quantity
- FROM ps_attribute_lang as PAL, ps_product_attribute_combination as PAC, ps_product_attribute as PA, ps_attribute as A
- WHERE (PAL.id_lang=3 AND PAC.id_attribute=PAL.id_attribute AND PAC.id_product_attribute=PA.id_product_attribute AND PA.reference='"""+ ref + """' AND PAL.id_attribute=A.id_attribute)
- group by PAC.id_product_attribute
- ) as tbl
- group by color;"""
- count = cursor.execute(sql)
- resultado = cursor.fetchall()
- num_fields = len(cursor.description)
- field_names = [i[0] for i in cursor.description]
- seriesA=['16', '17', '18', '19', '20', '25', '26', '27', '31', '32', '33', '34']
- #TABLE
- #print column names
- print """<table border=1 cellspacing=0 cellpadding=0>"""
- print "<tr>"
- for n in range(len(field_names)):
- a=field_names[n]
- if (a in seriesA):
- print """<td width=10>%s</td>""" % a
- else:
- print """<td bgcolor=PaleTurquoise width=10>%s</td>""" % a
- print "</tr>"
- #print editable rows, assign values from query
- for index,fila in enumerate(resultado):
- print "<tr>"
- for n in range(len(fila)):
- text=resultado[index][n]
- namevar="result"+" "+str(index)+" "+str(n)
- if n==0:
- text=text[5:]
- print """<td><input size="15" type="text" name="%s" value="%s"></td>""" % (namevar,text)
- elif text is None:
- text=""
- print """<td width=10><input size="2" type="text" name="%s" disabled="disabled" value="%s"></td>""" % (namevar,text)
- else:
- color=str(n+15)
- if (color in seriesA):
- print """<td width=10><input class="serieB" size="2" type="text" name="%s" value="%s"></td>""" % (namevar,text)
- else:
- print """<td width=10><input class="serieA" size="2" type="text" name="%s" value="%s"></td>""" % (namevar,text)
- print "</tr>"
- print "</table>"
- #exit db
- cursor.close()
- conn.close()
- print """<input type="hidden" name="Refe" value="%s">""" % ref
- print """<input type="hidden" name="Len" value="%d">""" % len(resultado)
- def edittable(ref):
- #show again table with new, edited values
- accQuant=[]
- print """<table border=1 cellspacing=0 cellpadding=0>"""
- #constant header of table
- print "<tr>"
- print """<td width=110>color</td>"""
- for n in range(24):
- print """<td width=35>%s</td>""" % str(n+16)
- print "</tr>"
- #fetch edited data from form variables
- for n in range(int(form["Len"].value)):
- print "<tr>"
- for m in range(25):
- testvar="result"+" "+str(n)+" "+str(m)
- if (testvar in form):
- color=str(n+15)
- print """<td width=35>%s</td>""" % str(form[testvar].value)
- if (m):
- accQuant.append( int(form[testvar].value) )
- else:
- print """<td width=35 bgcolor=LightGray> </td>"""
- print "</tr>"
- print "</table>"
- #GET ID_PRODUCT_ATTRIBUTE NUMBERS FROM AN EQUIVALENT SQL QUERY:
- #connect
- try:
- conn = MySQLdb.connect(
- host = 'localhost',
- user = 'root',
- passwd = '',
- db = 'pruebagales')
- except MySQLdb.Error, e:
- print "<pre>%s</pre></body></html>" % e
- exit(1)
- #get values, sql
- cursor = conn.cursor()
- sql="""SELECT group_concat(PAL.name order by A.id_attribute_group asc separator ' ') as numero_color, PAC.id_product_attribute, PA.reference, PA.quantity
- FROM ps_attribute_lang as PAL, ps_product_attribute_combination as PAC, ps_product_attribute as PA, ps_attribute as A
- WHERE (PAL.id_lang=3 AND PAC.id_attribute=PAL.id_attribute AND PAC.id_product_attribute=PA.id_product_attribute AND PA.reference='"""+ ref +"""' AND PAL.id_attribute=A.id_attribute)
- group by PAC.id_product_attribute
- order by numero_color"""
- count = cursor.execute(sql)
- resultDB = cursor.fetchall()
- resultIndex=[]
- for each in resultDB:
- resultIndex.append(int(each[1]))
- #build list of updates
- sqllist=[]
- for n,m in enumerate(accQuant):
- a=accQuant[n]
- b=resultIndex[n]
- consulta="UPDATE ps_product_attribute SET quantity='%s' WHERE id_product_attribute='%s'" % (a,b)
- sqllist.append(consulta)
- for n in sqllist:
- #cursor.execute(n)
- print n
- print "<br>"
- #exit db
- cursor.close()
- conn.close()
- print "<H4>Cambios enviados a la base de datos</H4>"
- #PROGRAM CORE
- form = cgi.FieldStorage()
- password = "cuscus0987"
- valid=['10', '07', '09', '06', '01', '03', '7293', '6318', '5285', '7345', '7253', '2516', '1235', '3556', '3819', '3825', '1101', '2335', '3850', '3852', '2334 ga', '2334 ag','3850 rg', '3359 rg', '3359 m', '3359 ga', '2335 rg', '1029 m', '3311 m']
- if not form:
- header("Login Response")
- elif ("submit1" in form) and (form["Referencia"].value !="") and (form["Referencia"].value in valid):
- header("Resultado")
- bodyform()
- print """<H3>Referencia %s</H3>""" % form["Referencia"].value
- gettable(form["Referencia"].value)
- elif ("submit2" in form) and ("Refe" in form):
- header("Enviado")
- bodyform()
- print """<H3>Referencia %s</H3>""" % form["Refe"].value
- edittable(form["Refe"].value)
- else:
- header("Error")
- print "<H3>Referencia de producto equivocada. Pulsa 'Retroceso' o Pagina anterior en el navegador</H3>"
- footer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement