add_result.py
author fabien
Wed, 24 Sep 2003 14:28:33 -0400
branchxbelweb
changeset 6 da757ef67c69
parent 3 9e7e8b678819
child 7 7cc3ab1c160b
permissions -rwxr-xr-x
[svn r1540] Correction for zero-lenght results in bookmark search.

#!/usr/bin/python

import sys
import traceback
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
import time
from bkmark import Bookmark

def get_bk_from_form(form):
	bk = Bookmark()
	bk.url = form['url'].value
	bk.name = form['name'].value
	bk.desc = form['desc'].value
	return bk

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

def main(bk, kw):
    tmpl = TemplateManager().prepare("add_result.tmpl")
    tproc = TemplateProcessor()
    tproc.set("url", bk.url)
    tproc.set("name", bk.name)
    tproc.set("added", bk.added)
    tproc.set("desc", bk.desc)
    tproc.set("Keywords", kw)
    print tproc.process(tmpl)

if (__name__ == "__main__"):
    form = cgi.FieldStorage()
    db = my_db.connect()
    bk = get_bk_from_form(form)
    kw = get_kw_from_form(form)
    id = db.add_bookmark(bk)
    db.update_keywords(id, kw)
    kw = map(lambda e: { 'keyword': e[1] }, db.get_keywords(id)[1:])
    main(bk, kw)