Kasutliidesed lab 2 otsingu cgi

Allikas: Lambda
#!/usr/bin/python
import cgi
import sqlite
#from pysqlite2 import dbapi2 as sqlite

# ---- abifunktsioonid ----

def formatdata(data,format):
  if format=='xml':
    return xmlstr(data)
  else:
    return jsonstr(data)    

def jsonstr(data):
  if data:
    return "'"+str(data)+"'"
  else:
    return "''"
    
def xmlstr(data):  
  if data and data!='':
    data=str(data)
    data=data.replace('<','<')
    data=data.replace('>','>')
    data=data.replace('&','&')    
    return "<data>"+data+"</data>"
  else:
    return "<data/>"
    
# --- formaat ja content-type ---

form = cgi.FieldStorage()
format='json'
#format='xml'
if form.has_key('format'):
  format=form["format"].value
if format=='xml':
  print 'Content-type: text/xml'
else:
  print 'Content-type: text/plain'  
print  

# -- sql select lause moodustamine ---

query="""select id,laadija,ylikool,teaduskond,ainekood,
                ainenimetus,oppejounimi,materjalikeel,
                materjalityyp,materjalinimi,lyhikirjeldus
         from materjalid where 1=1  """
         
# --- otsivaljad ----        
if form.has_key('laadija'):
  query=query+" and laadija='"+form["laadija"].value+"' "
if form.has_key('ylikool'):
  query=query+" and ylikool='"+form["ylikool"].value+"' "
if form.has_key('teaduskond'):
  query=query+" and teaduskond='"+form["teaduskond"].value+"%' "
if form.has_key('oppejounimi'):
  query=query+" and oppejounimi='%"+form["oppejounimi"].value+"%' "
if form.has_key('ainekood'):
  query=query+" and ainekood='"+form["ainekood"].value+"%' "
if form.has_key('ainenimetus'):
  query=query+" and ainenimetus='"+form["ainenimetus"].value+"%' "
if form.has_key('oppejounimi'):
  query=query+" and oppejounimi='"+form["oppejounimi"].value+"%' "
if form.has_key('materjalikeel'):
  query=query+" and materjalikeel='"+form["materjalikeel"].value+"%' "
if form.has_key('materjalityyp'):
  query=query+" and materjalityyp='"+form["materjalityyp"].value+"%' "
if form.has_key('materjalinimi'):
  query=query+" and materjalinimi='"+form["materjalinimi"].value+"%' "
if form.has_key('lyhikirjeldus'):
  query=query+" and =lyhikirjeldus'"+form["lyhikirjeldus"].value+"%' "

# -- sorteerimine ---

order="id"
direction="desc"
if form.has_key('order'): 
  order=form["order"].value
if form.has_key('direction'): 
  direction=form["direction"].value      
query=query+" order by "+order+" "+direction

# -- otsingu tegemine --

#print query
#con = sqlite.connect('/var/www/cgi-bin/tammet/materjalid.db')
con = sqlite.connect('/tmp/tammet/materjalid.db')
#con = sqlite.connect('materjalid.db')
cur = con.cursor()
cur.execute(query)

# --- otsingutulemuste votmine ---

fromrow=0
getrows=100
if form.has_key('fromrow'):
  fromrow=int(form["fromrow"].value)
if form.has_key('getrows'):
  getrows=int(form["getrows"].value)  

res=[]
i=0
j=0
cnt=0
while 1:
  row=cur.fetchone()
  if not row:
    break
  i=i+1
  if i>fromrow:
    cnt=cnt+1
    if cnt>getrows:
      break
    res=res+[row]  

# --- otsingutulemuste vormistamine ----

l=len(res)
i=0
if format=='xml':
  print "<materjalid>" 
else:
  print "["  
for row in res:
  if format=='xml':
    print "<row>" 
  else:
    print "[",
  for data in row[0:-1]:
      print formatdata(data,format),
      if format!='xml':
        print ",",
  print formatdata(row[-1],format),                
  if format=='xml':
    print
    print "</row>" 
  else:
    print "]",  
  if format!='xml' and i<l-1:
      print ","
  i=i+1
if format=='xml':
  print "</materjalid>" 
else:
  print
  print "]"