index.py
branchxbelweb
changeset 3 9e7e8b678819
parent 0 a4c49a0c313b
child 4 20c995f13f63
equal deleted inserted replaced
2:4cb6d9f3d3eb 3:9e7e8b678819
    12 # import cgitb; cgitb.enable()
    12 # import cgitb; cgitb.enable()
    13 import cgi
    13 import cgi
    14 from htmltmpl import TemplateManager, TemplateProcessor
    14 from htmltmpl import TemplateManager, TemplateProcessor
    15 import my_db
    15 import my_db
    16 
    16 
    17 def main(msg):
    17 def get_selection(form):
       
    18 	kw = form.getvalue("kw")
       
    19 	if not isinstance(kw, type([])):
       
    20 		if kw:
       
    21 			kw = [kw]
       
    22 		else:
       
    23 			kw = [0]
       
    24 	return map(int, kw)
       
    25 
       
    26 def set_selection(kw, sel):
       
    27 	res = []
       
    28 	for key in kw:
       
    29 		chk = key[0] in sel
       
    30 		res.append({'id':key[0],
       
    31 			'keyword': key[1],
       
    32 			'checked' : chk})
       
    33 	return res
       
    34 
       
    35 def main(bk, kw):
    18     tmpl = TemplateManager().prepare("index.tmpl")
    36     tmpl = TemplateManager().prepare("index.tmpl")
    19     tproc = TemplateProcessor()
    37     tproc = TemplateProcessor()
    20     tproc.set("msg", msg)
    38     tproc.set("Bookmarks", bk)
    21 
    39     tproc.set("Keywords", kw)
    22 #    tproc.set("syntax", user.syntax)
       
    23 #    for syntax in Tester.syntax_name:
       
    24 #        tproc.set("syntax_" + syntax,
       
    25 #                  (Tester.syntax_name[user.syntax] == syntax))
       
    26 #    tproc.set("id", user.id)
       
    27 #    tproc.set("gender", user.gender);
       
    28 #    tproc.set("age", user.age);
       
    29 #    exp = []
       
    30 #    for key in Tester.keys:
       
    31 #        exp.append({ 'name' : key,
       
    32 #                     'value' : user.experience[key] });
       
    33 #    tproc.set('Exp', exp);
       
    34     print tproc.process(tmpl)
    40     print tproc.process(tmpl)
    35 
    41 
    36 if (__name__ == "__main__"):
    42 if (__name__ == "__main__"):
    37     form = cgi.FieldStorage()
    43     form = cgi.FieldStorage()
    38     cnx = my_db.connect()
    44     db = my_db.connect()
    39     qry = cnx.query('SELECT name FROM keywords;')
    45     keywords = db.get_all_keywords()[1:]
    40     main(str(qry.getresult()))
    46     selection = get_selection(form)
       
    47     if selection[0] == 0:
       
    48     	exc = map(lambda e: int(e[0]), keywords)
       
    49    	bookmarks = db.select_bookmarks([0], exc)
       
    50     else:
       
    51     	bookmarks = db.select_bookmarks(selection)
       
    52     keywords = set_selection(keywords, selection)
       
    53     bookmarks = db.get_bookmarks(map(lambda x: x[0], bookmarks))
       
    54     bookmarks = map(lambda bk: bk.dict(), bookmarks)
       
    55     main(bookmarks, keywords)