Advertisement
Guest User

editor

a guest
Jan 25th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.35 KB | None | 0 0
  1.  
  2. the calling html:
  3.  
  4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  5. <HTML>
  6. <HEAD>
  7. <style type="text/css">
  8.  
  9. td {height: 15; border:1; cellspacing:0; cellpadding:0}
  10. tr {border:1; cellspacing:0; cellpadding:0}
  11. body{background-color:white;}
  12.  
  13. input{border:0; cellspacing:0; cellpadding:0}
  14. input.enter {background-color:PaleTurquoise; border:1; cellspacing:0; cellpadding:0}
  15. input.serieA {background-color:PaleTurquoise}
  16. input.serieB {background-color:white}
  17. table{border:1;}
  18.  
  19. </style>
  20.  
  21. <BODY>
  22.  
  23.  
  24. <FORM method="POST" action="http://localhost/cgi-bin/quanteditor.py">
  25. <input class="enter" size="15" type="text" name="Referencia">
  26. <input size="15" type="submit" value="Consulta" name="submit1">
  27. <input size="15" type="submit" value="Grabar los cambios" name="submit2">
  28.  
  29. </BODY>
  30.  
  31. </HTML>
  32.  
  33.  
  34.  
  35.  
  36.  
  37. the python script, quanteditor.py:
  38.  
  39. #!c:\Python26\python.exe
  40. import cgi
  41. from sys import exit
  42. import MySQLdb
  43.  
  44. import urllib
  45.  
  46.  
  47.  
  48. # HTML SKELETON SECTION
  49.  
  50.  
  51. def header(title):
  52. print "Content-type: text/html\n"
  53. print """<HTML><HEAD><style type="text/css">
  54.  
  55. td {height: 15; border:1; cellspacing:0; cellpadding:0}
  56. tr {border:1; cellspacing:0; cellpadding:0}
  57. body{background-color:white;}
  58.  
  59. input{border:0; cellspacing:0; cellpadding:0}
  60. input.enter {background-color:PaleTurquoise; border:1; cellspacing:0; cellpadding:0}
  61. input.serieA {background-color:PaleTurquoise}
  62. input.serieB {background-color:white}
  63. table{border:1;}
  64.  
  65. </style><TITLE>"%s"</TITLE>\n</HEAD>\n<BODY>\n""" % (title)
  66.  
  67. def bodyform():
  68.  
  69. print """<FORM method="POST" action="http://localhost/cgi-bin/quanteditor.py">"""
  70. print """<input class="enter" size="15" type="text" name="Referencia">"""
  71. print """<input size="15" type="submit" value="Consulta" name="submit1">"""
  72. print """<input size="15" type="submit" value="Grabar los cambios" name="submit2">"""
  73.  
  74.  
  75. def footer():
  76.  
  77. print """</form>"""
  78. print "</BODY></HTML>"
  79.  
  80.  
  81. #DATABASE AND EDIT FUNCTIONS
  82.  
  83. def gettable(ref):
  84.  
  85.  
  86. #connect
  87. try:
  88. conn = MySQLdb.connect(
  89. host = 'localhost',
  90. user = 'root',
  91. passwd = '',
  92. db = 'pruebagales')
  93. except MySQLdb.Error, e:
  94. print "<pre>%s</pre></body></html>" % e
  95. exit(1)
  96.  
  97. #get values, sql
  98. cursor = conn.cursor()
  99. sql="""SELECT color,
  100. group_concat(if(numero='16 ',cast(quantity as char), null)) as '16',
  101. group_concat(if(numero='17 ',cast(quantity as char), null)) as '17',
  102. group_concat(if(numero='18 ',cast(quantity as char), null)) as '18',
  103. group_concat(if(numero='19 ',cast(quantity as char), null)) as '19',
  104. group_concat(if(numero='20 ',cast(quantity as char), null)) as '20',
  105. group_concat(if(numero='21 ',cast(quantity as char), null)) as '21',
  106. group_concat(if(numero='22 ',cast(quantity as char), null)) as '22',
  107. group_concat(if(numero='23 ',cast(quantity as char), null)) as '23',
  108. group_concat(if(numero='24 ',cast(quantity as char), null)) as '24',
  109. group_concat(if(numero='25 ',cast(quantity as char), null)) as '25',
  110. group_concat(if(numero='26 ',cast(quantity as char), null)) as '26',
  111. group_concat(if(numero='27 ',cast(quantity as char), null)) as '27',
  112. group_concat(if(numero='28 ',cast(quantity as char), null)) as '28',
  113. group_concat(if(numero='29 ',cast(quantity as char), null)) as '29',
  114. group_concat(if(numero='30 ',cast(quantity as char), null)) as '30',
  115. group_concat(if(numero='31 ',cast(quantity as char), null)) as '31',
  116. group_concat(if(numero='32 ',cast(quantity as char), null)) as '32',
  117. group_concat(if(numero='33 ',cast(quantity as char), null)) as '33',
  118. group_concat(if(numero='34 ',cast(quantity as char), null)) as '34',
  119. group_concat(if(numero='35 ',cast(quantity as char), null)) as '35',
  120. group_concat(if(numero='36 ',cast(quantity as char), null)) as '36',
  121. group_concat(if(numero='37 ',cast(quantity as char), null)) as '37',
  122. group_concat(if(numero='38 ',cast(quantity as char), null)) as '38',
  123. group_concat(if(numero='39 ',cast(quantity as char), null)) as '39'
  124.  
  125. from
  126. (
  127.  
  128. SELECT substring(group_concat(PAL.name order by A.id_attribute_group desc separator ' '),3) as color,
  129. substring(group_concat(PAL.name order by A.id_attribute_group desc separator ' '),1,3) as numero, quantity
  130.  
  131. FROM ps_attribute_lang as PAL, ps_product_attribute_combination as PAC, ps_product_attribute as PA, ps_attribute as A
  132.  
  133. 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)
  134.  
  135. group by PAC.id_product_attribute
  136.  
  137. ) as tbl
  138.  
  139. group by color;"""
  140.  
  141. count = cursor.execute(sql)
  142.  
  143.  
  144.  
  145. resultado = cursor.fetchall()
  146. num_fields = len(cursor.description)
  147. field_names = [i[0] for i in cursor.description]
  148.  
  149.  
  150. seriesA=['16', '17', '18', '19', '20', '25', '26', '27', '31', '32', '33', '34']
  151.  
  152.  
  153. #TABLE
  154. #print column names
  155.  
  156. print """<table border=1 cellspacing=0 cellpadding=0>"""
  157.  
  158. print "<tr>"
  159.  
  160. for n in range(len(field_names)):
  161. a=field_names[n]
  162. if (a in seriesA):
  163. print """<td width=10>%s</td>""" % a
  164. else:
  165. print """<td bgcolor=PaleTurquoise width=10>%s</td>""" % a
  166. print "</tr>"
  167.  
  168. #print editable rows, assign values from query
  169. for index,fila in enumerate(resultado):
  170. print "<tr>"
  171.  
  172. for n in range(len(fila)):
  173. text=resultado[index][n]
  174. namevar="result"+" "+str(index)+" "+str(n)
  175. if n==0:
  176. text=text[5:]
  177. print """<td><input size="15" type="text" name="%s" value="%s"></td>""" % (namevar,text)
  178. elif text is None:
  179. text=""
  180. print """<td width=10><input size="2" type="text" name="%s" disabled="disabled" value="%s"></td>""" % (namevar,text)
  181. else:
  182. color=str(n+15)
  183. if (color in seriesA):
  184. print """<td width=10><input class="serieB" size="2" type="text" name="%s" value="%s"></td>""" % (namevar,text)
  185. else:
  186. print """<td width=10><input class="serieA" size="2" type="text" name="%s" value="%s"></td>""" % (namevar,text)
  187.  
  188. print "</tr>"
  189.  
  190.  
  191. print "</table>"
  192. #exit db
  193. cursor.close()
  194. conn.close()
  195.  
  196.  
  197. print """<input type="hidden" name="Refe" value="%s">""" % ref
  198. print """<input type="hidden" name="Len" value="%d">""" % len(resultado)
  199.  
  200.  
  201.  
  202.  
  203. def edittable(ref):
  204.  
  205. #show again table with new, edited values
  206.  
  207. accQuant=[]
  208.  
  209. print """<table border=1 cellspacing=0 cellpadding=0>"""
  210.  
  211. #constant header of table
  212.  
  213. print "<tr>"
  214. print """<td width=110>color</td>"""
  215. for n in range(24):
  216. print """<td width=35>%s</td>""" % str(n+16)
  217. print "</tr>"
  218.  
  219. #fetch edited data from form variables
  220.  
  221. for n in range(int(form["Len"].value)):
  222. print "<tr>"
  223. for m in range(25):
  224. testvar="result"+" "+str(n)+" "+str(m)
  225. if (testvar in form):
  226. color=str(n+15)
  227. print """<td width=35>%s</td>""" % str(form[testvar].value)
  228. if (m):
  229. accQuant.append( int(form[testvar].value) )
  230. else:
  231. print """<td width=35 bgcolor=LightGray>&nbsp</td>"""
  232.  
  233. print "</tr>"
  234.  
  235.  
  236. print "</table>"
  237.  
  238.  
  239.  
  240. #GET ID_PRODUCT_ATTRIBUTE NUMBERS FROM AN EQUIVALENT SQL QUERY:
  241. #connect
  242. try:
  243. conn = MySQLdb.connect(
  244. host = 'localhost',
  245. user = 'root',
  246. passwd = '',
  247. db = 'pruebagales')
  248. except MySQLdb.Error, e:
  249. print "<pre>%s</pre></body></html>" % e
  250. exit(1)
  251.  
  252. #get values, sql
  253. cursor = conn.cursor()
  254. 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
  255.  
  256.  
  257. FROM ps_attribute_lang as PAL, ps_product_attribute_combination as PAC, ps_product_attribute as PA, ps_attribute as A
  258.  
  259. 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)
  260.  
  261.  
  262. group by PAC.id_product_attribute
  263. order by numero_color"""
  264.  
  265.  
  266.  
  267. count = cursor.execute(sql)
  268. resultDB = cursor.fetchall()
  269.  
  270. resultIndex=[]
  271. for each in resultDB:
  272. resultIndex.append(int(each[1]))
  273.  
  274.  
  275.  
  276. #build list of updates
  277.  
  278. sqllist=[]
  279. for n,m in enumerate(accQuant):
  280. a=accQuant[n]
  281. b=resultIndex[n]
  282. consulta="UPDATE ps_product_attribute SET quantity='%s' WHERE id_product_attribute='%s'" % (a,b)
  283. sqllist.append(consulta)
  284.  
  285. for n in sqllist:
  286. #cursor.execute(n)
  287. print n
  288. print "<br>"
  289.  
  290.  
  291. #exit db
  292.  
  293. cursor.close()
  294. conn.close()
  295.  
  296. print "<H4>Cambios enviados a la base de datos</H4>"
  297.  
  298.  
  299.  
  300.  
  301. #PROGRAM CORE
  302.  
  303. form = cgi.FieldStorage()
  304. password = "cuscus0987"
  305. 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']
  306.  
  307.  
  308. if not form:
  309.  
  310. header("Login Response")
  311.  
  312.  
  313. elif ("submit1" in form) and (form["Referencia"].value !="") and (form["Referencia"].value in valid):
  314.  
  315. header("Resultado")
  316. bodyform()
  317.  
  318. print """<H3>Referencia %s</H3>""" % form["Referencia"].value
  319. gettable(form["Referencia"].value)
  320.  
  321. elif ("submit2" in form) and ("Refe" in form):
  322.  
  323. header("Enviado")
  324. bodyform()
  325. print """<H3>Referencia %s</H3>""" % form["Refe"].value
  326. edittable(form["Refe"].value)
  327.  
  328. else:
  329. header("Error")
  330. print "<H3>Referencia de producto equivocada. Pulsa 'Retroceso' o Pagina anterior en el navegador</H3>"
  331.  
  332. footer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement