index.py
author fabien
Fri, 26 Sep 2003 18:51:48 -0400
branchxbelweb
changeset 22 41c87e1332eb
parent 21 345ee7421989
child 27 212f1dc25b67
permissions -rwxr-xr-x
[svn r1556] Add lookup button and add bookmark link.

#!/usr/bin/python

import sys
import traceback
if (__name__ == '__main__'):
	sys.path.insert(0, "/home/fabien/lib/python")
	sys.path.insert(0, "./lib")
	sys.stderr = sys.stdout

	print "Content-type: text/html; charset=iso-8859-1;"
	print

# import cgitb; cgitb.enable()
import cgi
from htmltmpl import TemplateManager, TemplateProcessor
import my_db
from os import environ
from urlparse import urljoin

def get_selection(form):
	kw = form.getvalue("kw")
	if not isinstance(kw, type([])):
		if kw:
			kw = [kw]
		else:
			kw = [0]
	return map(int, kw)

def set_selection(db, sel):
	allkw = db.get_all_keywords()
	(kw, cnt) = map(list,apply(zip, db.get_keywords_count()))
	res = []
	for key in allkw:
		chk = key[0] in sel
		if key[0] in kw:
			kcnt = cnt[kw.index(key[0])]
		else:
			kcnt = 0
		res.append({'id':key[0],
			'keyword': key[1],
			'count' : kcnt,
			'checked' : chk})
	return res

def get_curl():
	return urljoin( 'http://' + environ["HTTP_HOST"] + environ["REQUEST_URI"], 'add.py')

def load_index(db):
	kw = set_selection(db, [])
    	total = kw[0]['count']
    	kw = kw[1:]
    	exc = map(lambda e: int(e['id']), kw)
    	bookmarks = db.select_bookmarks([0], exc)
	if len(bookmarks)>0:
    		bookmarks = db.get_bookmarks(map(lambda x: x[0], bookmarks))
    	bookmarks = map(lambda bk: bk.dict(), bookmarks)
    	curl = get_curl();
    	main(bookmarks, kw, curl, total)

def main(bk, kw, curl, total):
    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['keyword'],y['keyword']))
    tproc.set("Keywords", kw)
    tproc.set("curl", curl)
    tproc.set("total", total)
    print tproc.process(tmpl)

if (__name__ == "__main__"):
    form = cgi.FieldStorage()
    db = my_db.connect(environ["REMOTE_USER"])
    selection = get_selection(form)
    keywords = set_selection(db, selection)
    total = keywords[0]['count']
    keywords = keywords[1:]
    if selection[0] == 0:
    	exc = map(lambda e: int(e['id']), keywords)
   	bookmarks = db.select_bookmarks([0], exc)
    else:
    	bookmarks = db.select_bookmarks(selection)
    if len(bookmarks) > 0:
    	bookmarks = db.get_bookmarks(map(lambda x: x[0], bookmarks))
    bookmarks = map(lambda bk: bk.dict(), bookmarks)
    curl = get_curl();
    main(bookmarks, keywords, curl, total)