author | fabien |
Wed, 24 Sep 2003 17:19:52 -0400 | |
branch | xbelweb |
changeset 13 | 7357230539d2 |
parent 9 | 896b7c6de627 |
child 15 | 574631f841c3 |
permissions | -rwxr-xr-x |
0 | 1 |
#!/usr/bin/python |
2 |
||
3 |
import sys |
|
4 |
import traceback |
|
5 |
sys.path.insert(0, "/home/fabien/lib/python") |
|
6 |
sys.path.insert(0, "./lib") |
|
7 |
sys.stderr = sys.stdout |
|
8 |
||
9 |
print "Content-type: text/html; charset=iso-8859-1;" |
|
10 |
||
11 |
||
12 |
# import cgitb; cgitb.enable() |
|
13 |
import cgi |
|
14 |
from htmltmpl import TemplateManager, TemplateProcessor |
|
15 |
import my_db |
|
4 | 16 |
from os import environ |
17 |
from urlparse import urljoin |
|
0 | 18 |
|
3 | 19 |
def get_selection(form): |
20 |
kw = form.getvalue("kw") |
|
21 |
if not isinstance(kw, type([])): |
|
22 |
if kw: |
|
23 |
kw = [kw] |
|
24 |
else: |
|
25 |
kw = [0] |
|
26 |
return map(int, kw) |
|
27 |
||
28 |
def set_selection(kw, sel): |
|
29 |
res = [] |
|
30 |
for key in kw: |
|
31 |
chk = key[0] in sel |
|
32 |
res.append({'id':key[0], |
|
33 |
'keyword': key[1], |
|
13 | 34 |
'count' : key[2], |
3 | 35 |
'checked' : chk}) |
9 | 36 |
res.sort(lambda l,r: cmp(l["keyword"],r["keyword"])) |
3 | 37 |
return res |
38 |
||
4 | 39 |
def get_curl(): |
40 |
return urljoin( 'http://' + environ["HTTP_HOST"] + environ["REQUEST_URI"], 'add.py') |
|
41 |
||
13 | 42 |
def main(bk, kw, curl, total): |
0 | 43 |
tmpl = TemplateManager().prepare("index.tmpl") |
44 |
tproc = TemplateProcessor() |
|
3 | 45 |
tproc.set("Bookmarks", bk) |
46 |
tproc.set("Keywords", kw) |
|
4 | 47 |
tproc.set("curl", curl) |
13 | 48 |
tproc.set("total", total) |
0 | 49 |
print tproc.process(tmpl) |
50 |
||
51 |
if (__name__ == "__main__"): |
|
52 |
form = cgi.FieldStorage() |
|
3 | 53 |
db = my_db.connect() |
13 | 54 |
keywords = db.get_all_keywords() |
55 |
total = keywords[0][2] |
|
56 |
keywords = keywords[1:] |
|
3 | 57 |
selection = get_selection(form) |
58 |
if selection[0] == 0: |
|
59 |
exc = map(lambda e: int(e[0]), keywords) |
|
60 |
bookmarks = db.select_bookmarks([0], exc) |
|
61 |
else: |
|
62 |
bookmarks = db.select_bookmarks(selection) |
|
63 |
keywords = set_selection(keywords, selection) |
|
6
da757ef67c69
[svn r1540] Correction for zero-lenght results in bookmark search.
fabien
parents:
4
diff
changeset
|
64 |
if len(bookmarks) > 0: |
da757ef67c69
[svn r1540] Correction for zero-lenght results in bookmark search.
fabien
parents:
4
diff
changeset
|
65 |
bookmarks = db.get_bookmarks(map(lambda x: x[0], bookmarks)) |
3 | 66 |
bookmarks = map(lambda bk: bk.dict(), bookmarks) |
4 | 67 |
curl = get_curl(); |
13 | 68 |
main(bookmarks, keywords, curl, total) |