index.py
branchxbelweb
changeset 21 345ee7421989
parent 17 14bec94bbe89
child 27 212f1dc25b67
equal deleted inserted replaced
20:8c201fa3bdc7 21:345ee7421989
    24 			kw = [kw]
    24 			kw = [kw]
    25 		else:
    25 		else:
    26 			kw = [0]
    26 			kw = [0]
    27 	return map(int, kw)
    27 	return map(int, kw)
    28 
    28 
    29 def set_selection(kw, sel):
    29 def set_selection(db, sel):
       
    30 	allkw = db.get_all_keywords()
       
    31 	(kw, cnt) = map(list,apply(zip, db.get_keywords_count()))
    30 	res = []
    32 	res = []
    31 	for key in kw:
    33 	for key in allkw:
    32 		chk = key[0] in sel
    34 		chk = key[0] in sel
       
    35 		if key[0] in kw:
       
    36 			kcnt = cnt[kw.index(key[0])]
       
    37 		else:
       
    38 			kcnt = 0
    33 		res.append({'id':key[0],
    39 		res.append({'id':key[0],
    34 			'keyword': key[1],
    40 			'keyword': key[1],
    35 			'count' : key[2],
    41 			'count' : kcnt,
    36 			'checked' : chk})
    42 			'checked' : chk})
    37 	res.sort(lambda l,r: cmp(l["keyword"],r["keyword"]))
       
    38 	return res
    43 	return res
    39 
    44 
    40 def get_curl():
    45 def get_curl():
    41 	return urljoin( 'http://' + environ["HTTP_HOST"] + environ["REQUEST_URI"], 'add.py')
    46 	return urljoin( 'http://' + environ["HTTP_HOST"] + environ["REQUEST_URI"], 'add.py')
    42 
    47 
    43 def load_index(db):
    48 def load_index(db):
    44     	kw = db.get_all_keywords()
    49 	kw = set_selection(db, [])
    45 	kw = set_selection(kw, [])
       
    46     	total = kw[0]['count']
    50     	total = kw[0]['count']
    47     	kw = kw[1:]
    51     	kw = kw[1:]
    48     	exc = map(lambda e: int(e['id']), kw)
    52     	exc = map(lambda e: int(e['id']), kw)
    49     	bookmarks = db.select_bookmarks([0], exc)
    53     	bookmarks = db.select_bookmarks([0], exc)
    50 	if len(bookmarks)>0:
    54 	if len(bookmarks)>0:
    54     	main(bookmarks, kw, curl, total)
    58     	main(bookmarks, kw, curl, total)
    55 
    59 
    56 def main(bk, kw, curl, total):
    60 def main(bk, kw, curl, total):
    57     tmpl = TemplateManager().prepare("index.tmpl")
    61     tmpl = TemplateManager().prepare("index.tmpl")
    58     tproc = TemplateProcessor()
    62     tproc = TemplateProcessor()
       
    63     tproc.set("pagetitle", environ["REMOTE_USER"]+"'s XBELWeb")
       
    64     bk.sort(lambda x,y: cmp(x['name'],y['name']))
    59     tproc.set("Bookmarks", bk)
    65     tproc.set("Bookmarks", bk)
       
    66     kw.sort(lambda x,y: cmp(x['keyword'],y['keyword']))
    60     tproc.set("Keywords", kw)
    67     tproc.set("Keywords", kw)
    61     tproc.set("curl", curl)
    68     tproc.set("curl", curl)
    62     tproc.set("total", total)
    69     tproc.set("total", total)
    63     print tproc.process(tmpl)
    70     print tproc.process(tmpl)
    64 
    71 
    65 if (__name__ == "__main__"):
    72 if (__name__ == "__main__"):
    66     form = cgi.FieldStorage()
    73     form = cgi.FieldStorage()
    67     db = my_db.connect()
    74     db = my_db.connect(environ["REMOTE_USER"])
    68     keywords = db.get_all_keywords()
    75     selection = get_selection(form)
    69     total = keywords[0][2]
    76     keywords = set_selection(db, selection)
       
    77     total = keywords[0]['count']
    70     keywords = keywords[1:]
    78     keywords = keywords[1:]
    71     selection = get_selection(form)
       
    72     if selection[0] == 0:
    79     if selection[0] == 0:
    73     	exc = map(lambda e: int(e[0]), keywords)
    80     	exc = map(lambda e: int(e['id']), keywords)
    74    	bookmarks = db.select_bookmarks([0], exc)
    81    	bookmarks = db.select_bookmarks([0], exc)
    75     else:
    82     else:
    76     	bookmarks = db.select_bookmarks(selection)
    83     	bookmarks = db.select_bookmarks(selection)
    77     keywords = set_selection(keywords, selection)
       
    78     if len(bookmarks) > 0:
    84     if len(bookmarks) > 0:
    79     	bookmarks = db.get_bookmarks(map(lambda x: x[0], bookmarks))
    85     	bookmarks = db.get_bookmarks(map(lambda x: x[0], bookmarks))
    80     bookmarks = map(lambda bk: bk.dict(), bookmarks)
    86     bookmarks = map(lambda bk: bk.dict(), bookmarks)
    81     curl = get_curl();
    87     curl = get_curl();
    82     main(bookmarks, keywords, curl, total)
    88     main(bookmarks, keywords, curl, total)