Home - Python - CGI scripting and MySQL


#! /usr/bin/python

import db
import html
import cgi

def strInsert(lstFields):

  strIns = ""
  for field in lstFields:
    strIns = strIns  + field + ","
  strIns = "(" + strIns[:-1] + ") values "
  strInsVal = ""
  
  for field in lstFields:
    if cgiData.has_key(field):
      strInsVal = strInsVal  + "'" + cgiData[field ].value + "',"
    else:
      strInsVal = strInsVal + "'',"
      
  strInsVal = "(" + strInsVal[:-1] + ")"
  return strIns + strInsVal
    
def doInsert():
  strSQL = strInsert(('valdyan'
  ,'label','glosse','comment'))
  if strSQL == "() values ()":
    pass
  else:
    dbc.ins("insert into lex_valdyan " + strSQL)
  
def strUpdate(lstFields):
  strUpd = ""
  for field in lstFields:
    if cgiData.has_key(field):
      strUpd = strUpd  + " "  + field + " = '" + cgiData[field].value + "',"
  strUpd = strUpd[:-1]
  return strUpd
  
def doUpdate():
  if cgiData.has_key("lexnr"):
    strSQL = " update lex_valdyan set " + strUpdate(('valdyan'
    ,'label','glosse','comment')
    ) + " where lexnr = " + str(cgiData['lexnr'].value)
    dbc.upd(strSQL)
    
def doDelete():
  if cgiData.has_key("lexnr"):
    strSQL = " delete from lex_valdyan where lexnr = " + str(cgiData['lexnr'].value)
    dbc.upd(strSQL)

def doNew():
  html.form('lex_valdyan.py')
  html.textbox('valdyan','valdyan','',70)
  html.textbox('label','label','',70)
  html.textbox('glosse','glosse','',70)
  html.textarea('comment','comment','',10,70)
  html.par()
  html.button('action','insert')
  html.button('action','search')
  html.button('action','new')  
  html.button('action','index')
  html.coda_form()
  
def doQueryDelete():
  html.form('lex_valdyan.py')
  if cgiData.has_key("lexnr"):
    strSQL = " select lexnr, valdyan, label, glosse, comment from lex_valdyan where lexnr = " + str(cgiData['lexnr'].value)
    rs = dbc.qry (strSQL)
    row = rs[0]
    html.hidden("lexnr",cgiData['lexnr'].value)
    html.txt("Do you really want to delete " + row[1] + "?")
    html.par()
    html.button('action','Rather not')
    html.button('action','Really delete')
  else:
    doNew()
  html.coda_form()

def doEdit():
  if cgiData.has_key("lexnr"):
    strSQL = " select lexnr, valdyan, label, glosse, comment from lex_valdyan where lexnr = " + str(cgiData['lexnr'].value)
    rs = dbc.qry (strSQL)
    row = rs[0]
    html.form('lex_valdyan.py')
    html.hidden('lexnr',row[0])
    html.textbox('valdyan','valdyan',row[1],70)
    html.textbox('label','label',row[2],70)
    html.textbox('glosse','glosse',row[3],70)
    if row[4] is None:
      html.textarea('comment','comment',"",10,70)
    else:
      html.textarea('comment','comment',row[4],10,70)
    html.par()
    html.button('action','update')
    html.button('action','search')
    html.button('action','new')
    html.button('action','index')
    html.coda_form()
  else:
    doNew()


def doIndex(strSQL):
  print('<hr>')
  html.form("lex_valdyan.py")

  html.button("action","new")
  html.button("action","edit")
  html.button("action","delete")
  html.par()
  rs = dbc.qry(strSQL + " order by valdyan ")
  html.radio ('lexnr','Entry',rs,'')
  html.par()
  html.coda_form()
    
  
def doSearch():
  strSQL = " select lexnr, concat( lexnr,', ', valdyan,', ',label,', ', glosse) from lex_valdyan where "
  if cgiData.has_key("valdyan"):
    strSQL = strSQL + " valdyan like '"
    strSQL = strSQL + cgiData["valdyan"].value + "'"
  else:
    strSQL = strSQL + " valdyan like '%' "
  if cgiData.has_key("label"):
    strSQL = strSQL + " and label like '"
    strSQL = strSQL + cgiData["label"].value + "'"
  
  if cgiData.has_key("glosse"):
    strSQL = strSQL + " and glosse like '"  
    strSQL = strSQL + cgiData["glosse"].value + "'"
  
  if cgiData.has_key("comment"):
    strSQL = strSQL + " and comment like '"
    strSQL = strSQL + cgiData["comment"].value + "'"
    
  doIndex(strSQL)
    
def doActions():
  if cgiData.has_key("action"):
    if cgiData["action"].value == 'insert':
      doInsert()
      doNew()
    elif cgiData["action"].value == 'update':
      doUpdate()
      doEdit()
    elif cgiData["action"].value == 'delete':
      doQueryDelete()
    elif cgiData["action"].value == 'search':
      doSearch()    
    elif cgiData["action"].value == 'edit':
      doEdit()
    elif cgiData["action"].value == 'new':
      doNew()
    elif cgiData["action"].value == 'Really delete':
      doDelete()
      doNew()
    else:
      doIndex(" select lexnr, concat(lexnr, ', ', valdyan,', ',label,', ', glosse) from lex_valdyan")
  else:
    doNew()
    

#
# main
#

dbc = db.Connection('essle','irina','valdyas')
cgiData = cgi.FieldStorage()

html.header('Valdyan Dictionary')
html.body()
html.head('1','Valdyan Dictionary')
doActions()
html.coda_body()

Changes


© 2000 Boudewijn Rempt