diff -r dbf4027d82fc -r cee64de4e7e0 lib/webutils.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/webutils.py Tue Oct 07 00:05:59 2003 -0400 @@ -0,0 +1,72 @@ +#!/usr/bin/python + + +import cgi +import my_db +from htmltmpl import TemplateManager, TemplateProcessor +from os import environ +from urlparse import urljoin + +def get_keywords(form, name): + kws = form.getvalue(name) + if not isinstance(kws, type([])): + if kws: + kws = [kws] + else: + kws = [] + return map(int, kws) + +def set_selection(db, bk, sel, exc = []): + if len(bk) > 0: + ids, names = map(list,apply(zip,db.get_keywords(bk))) + for key in exc: + if key not in ids: + ids.append(key) + names.append(db.get_keyword(key)) + allkw = map(lambda x,y: [x, y], ids, names) + else: + allkw = db.get_all_keywords() + (kw, cnt) = map(list,apply(zip, db.get_keywords_count())) + res = [] + for key in allkw: + is_selected = key[0] in sel + is_excluded = key[0] in exc + is_unselected = not (is_selected or is_excluded) + if key[0] in kw: + kcnt = cnt[kw.index(key[0])] + else: + kcnt = 0 + res.append({'id':key[0], + 'keyword': key[1], + 'count' : kcnt, + 'selected' : is_selected, + 'excluded' : is_excluded, + 'unselected' : is_unselected}) + return res + +def get_curl(): + return urljoin( 'http://' + environ["HTTP_HOST"] + environ["REQUEST_URI"], 'add.py') + +def load_index(db): + keywords = set_selection(db, [], [], []) + exc = map(lambda e: int(e['id']), keywords[1:]) + bookmarks = db.select_bookmarks([0], exc) + total = keywords[0]['count'] + if len(bookmarks)>0: + bookmarks = db.get_bookmarks(map(lambda x: x[0], bookmarks)) + bookmarks = map(lambda bk: bk.dict(), bookmarks) + process_index(bookmarks, keywords) + +def process_index(bk, kw): + tmpl = TemplateManager().prepare("index.tmpl") + tproc = TemplateProcessor() + tproc.set("pagetitle", environ["REMOTE_USER"]+"'s XBELWeb") + bk.sort(lambda x,y: cmp(x['name'],y['name'])) + tproc.set("Bookmarks", bk) + kw.sort(lambda x,y: -cmp(x['count'],y['count'])) + tproc.set("Keywords", kw) + tproc.set("curl", get_curl()) + tproc.set("total", len(bk)) + print tproc.process(tmpl) + +