index.py
branchxbelweb
changeset 32 10f57beae364
parent 27 212f1dc25b67
child 38 c30e2402deca
equal deleted inserted replaced
31:cee64de4e7e0 32:10f57beae364
    10 	print "Content-type: text/html; charset=iso-8859-1;"
    10 	print "Content-type: text/html; charset=iso-8859-1;"
    11 	print
    11 	print
    12 
    12 
    13 # import cgitb; cgitb.enable()
    13 # import cgitb; cgitb.enable()
    14 import cgi
    14 import cgi
    15 from htmltmpl import TemplateManager, TemplateProcessor
       
    16 import my_db
    15 import my_db
    17 from os import environ
    16 from webutils import *
    18 from urlparse import urljoin
       
    19 
       
    20 def get_selection(form):
       
    21 	kw = form.getvalue("kw")
       
    22 	if not isinstance(kw, type([])):
       
    23 		if kw:
       
    24 			kw = [kw]
       
    25 		else:
       
    26 			kw = [0]
       
    27 	return map(int, kw)
       
    28 
       
    29 def set_selection(db, sel):
       
    30 	allkw = db.get_all_keywords()
       
    31 	(kw, cnt) = map(list,apply(zip, db.get_keywords_count()))
       
    32 	res = []
       
    33 	for key in allkw:
       
    34 		chk = key[0] in sel
       
    35 		if key[0] in kw:
       
    36 			kcnt = cnt[kw.index(key[0])]
       
    37 		else:
       
    38 			kcnt = 0
       
    39 		res.append({'id':key[0],
       
    40 			'keyword': key[1],
       
    41 			'count' : kcnt,
       
    42 			'checked' : chk})
       
    43 	return res
       
    44 
       
    45 def get_curl():
       
    46 	return urljoin( 'http://' + environ["HTTP_HOST"] + environ["REQUEST_URI"], 'add.py')
       
    47 
       
    48 def load_index(db):
       
    49 	kw = set_selection(db, [])
       
    50     	total = kw[0]['count']
       
    51     	kw = kw[1:]
       
    52     	exc = map(lambda e: int(e['id']), kw)
       
    53     	bookmarks = db.select_bookmarks([0], exc)
       
    54 	if len(bookmarks)>0:
       
    55     		bookmarks = db.get_bookmarks(map(lambda x: x[0], bookmarks))
       
    56     	bookmarks = map(lambda bk: bk.dict(), bookmarks)
       
    57     	curl = get_curl();
       
    58     	main(bookmarks, kw, curl, total)
       
    59 
       
    60 def main(bk, kw, curl, total):
       
    61     tmpl = TemplateManager().prepare("index.tmpl")
       
    62     tproc = TemplateProcessor()
       
    63     tproc.set("pagetitle", environ["REMOTE_USER"]+"'s XBELWeb")
       
    64     bk.sort(lambda x,y: cmp(x['name'],y['name']))
       
    65     tproc.set("Bookmarks", bk)
       
    66     kw.sort(lambda x,y: -cmp(x['count'],y['count']))
       
    67     tproc.set("Keywords", kw)
       
    68     tproc.set("curl", curl)
       
    69     tproc.set("total", total)
       
    70     print tproc.process(tmpl)
       
    71 
    17 
    72 if (__name__ == "__main__"):
    18 if (__name__ == "__main__"):
    73     form = cgi.FieldStorage()
    19     form = cgi.FieldStorage()
    74     db = my_db.connect(environ["REMOTE_USER"])
    20     db = my_db.connect(environ["REMOTE_USER"])
    75     selection = get_selection(form)
    21     selection = get_keywords(form, 'sel')
    76     keywords = set_selection(db, selection)
    22     exclusion = get_keywords(form, 'exc')
    77     total = keywords[0]['count']
    23     if len(selection) == 0:
    78     keywords = keywords[1:]
    24     	keywords = set_selection(db, [], selection, exclusion)
    79     if selection[0] == 0:
    25     	exc = map(lambda e: int(e['id']), keywords[1:])
    80     	exc = map(lambda e: int(e['id']), keywords)
       
    81    	bookmarks = db.select_bookmarks([0], exc)
    26    	bookmarks = db.select_bookmarks([0], exc)
    82     else:
    27     else:
    83     	bookmarks = db.select_bookmarks(selection)
    28     	bookmarks = db.select_bookmarks(selection, exclusion)
       
    29    	keywords = set_selection(db,
       
    30 		map(lambda e: e[0], bookmarks),
       
    31 		selection, exclusion)
       
    32     total = len(bookmarks)
    84     if len(bookmarks) > 0:
    33     if len(bookmarks) > 0:
    85     	bookmarks = db.get_bookmarks(map(lambda x: x[0], bookmarks))
    34     	bookmarks = db.get_bookmarks(map(lambda x: x[0], bookmarks))
    86     bookmarks = map(lambda bk: bk.dict(), bookmarks)
    35     bookmarks = map(lambda bk: bk.dict(), bookmarks)
    87     curl = get_curl();
    36     process_index(bookmarks, keywords)
    88     main(bookmarks, keywords, curl, total)