lib/webutils.py
branchxbelweb
changeset 31 cee64de4e7e0
child 38 c30e2402deca
equal deleted inserted replaced
30:dbf4027d82fc 31:cee64de4e7e0
       
     1 #!/usr/bin/python
       
     2 
       
     3 
       
     4 import cgi
       
     5 import my_db
       
     6 from htmltmpl import TemplateManager, TemplateProcessor
       
     7 from os import environ
       
     8 from urlparse import urljoin
       
     9 
       
    10 def get_keywords(form, name):
       
    11 	kws = form.getvalue(name)
       
    12 	if not isinstance(kws, type([])):
       
    13 		if kws:
       
    14 			kws = [kws]
       
    15 		else:
       
    16 			kws = []
       
    17 	return map(int, kws)
       
    18 
       
    19 def set_selection(db, bk, sel, exc = []):
       
    20 	if len(bk) > 0:
       
    21 		ids, names = map(list,apply(zip,db.get_keywords(bk)))
       
    22 		for key in exc:
       
    23 			if key not in ids:
       
    24 				ids.append(key)
       
    25 				names.append(db.get_keyword(key))
       
    26 		allkw = map(lambda x,y: [x, y], ids, names)
       
    27 	else:
       
    28 		allkw = db.get_all_keywords()
       
    29 	(kw, cnt) = map(list,apply(zip, db.get_keywords_count()))
       
    30 	res = []
       
    31 	for key in allkw:
       
    32 		is_selected = key[0] in sel
       
    33 		is_excluded = key[0] in exc
       
    34 		is_unselected = not (is_selected or is_excluded)
       
    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 			'selected' : is_selected,
       
    43 			'excluded' : is_excluded,
       
    44 			'unselected' : is_unselected})
       
    45 	return res
       
    46 
       
    47 def get_curl():
       
    48 	return urljoin( 'http://' + environ["HTTP_HOST"] + environ["REQUEST_URI"], 'add.py')
       
    49 
       
    50 def load_index(db):
       
    51     	keywords = set_selection(db, [], [], [])
       
    52     	exc = map(lambda e: int(e['id']), keywords[1:])
       
    53    	bookmarks = db.select_bookmarks([0], exc)
       
    54     	total = keywords[0]['count']
       
    55 	if len(bookmarks)>0:
       
    56     		bookmarks = db.get_bookmarks(map(lambda x: x[0], bookmarks))
       
    57     	bookmarks = map(lambda bk: bk.dict(), bookmarks)
       
    58     	process_index(bookmarks, keywords)
       
    59 
       
    60 def process_index(bk, kw):
       
    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", get_curl())
       
    69     tproc.set("total", len(bk))
       
    70     print tproc.process(tmpl)
       
    71 
       
    72