add_result.py
author fabien
Wed, 24 Sep 2003 14:29:18 -0400
branchxbelweb
changeset 8 c763c420cbfc
parent 7 7cc3ab1c160b
child 9 896b7c6de627
permissions -rwxr-xr-x
[svn r1542] Invalid quoted in add_keyword query.

#!/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 get_new_kw_from_form(form):
	if form.has_key('newkw'):
		return map(lambda e: e.strip(),
			form['newkw'].value.split(','))
	else:
		return []

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)
    newkw = get_new_kw_from_form(form)
    for elem in newkw:
#	try:
	    id = db.add_keyword(elem)
	    kw.append(id)
#	except:
#	    print '<p class="error">Error inserting keyword "'+elem+'".</p>'
    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)