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