author | fabien |
Sat, 27 Sep 2003 03:17:30 -0400 | |
branch | xbelweb |
changeset 26 | 17b0cd274530 |
parent 21 | 345ee7421989 |
child 27 | 212f1dc25b67 |
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 |
||
21
345ee7421989
[svn r1555] Add multiuser support and more templating.
fabien
parents:
17
diff
changeset
|
29 |
def set_selection(db, sel): |
345ee7421989
[svn r1555] Add multiuser support and more templating.
fabien
parents:
17
diff
changeset
|
30 |
allkw = db.get_all_keywords() |
345ee7421989
[svn r1555] Add multiuser support and more templating.
fabien
parents:
17
diff
changeset
|
31 |
(kw, cnt) = map(list,apply(zip, db.get_keywords_count())) |
3 | 32 |
res = [] |
21
345ee7421989
[svn r1555] Add multiuser support and more templating.
fabien
parents:
17
diff
changeset
|
33 |
for key in allkw: |
3 | 34 |
chk = key[0] in sel |
21
345ee7421989
[svn r1555] Add multiuser support and more templating.
fabien
parents:
17
diff
changeset
|
35 |
if key[0] in kw: |
345ee7421989
[svn r1555] Add multiuser support and more templating.
fabien
parents:
17
diff
changeset
|
36 |
kcnt = cnt[kw.index(key[0])] |
345ee7421989
[svn r1555] Add multiuser support and more templating.
fabien
parents:
17
diff
changeset
|
37 |
else: |
345ee7421989
[svn r1555] Add multiuser support and more templating.
fabien
parents:
17
diff
changeset
|
38 |
kcnt = 0 |
3 | 39 |
res.append({'id':key[0], |
40 |
'keyword': key[1], |
|
21
345ee7421989
[svn r1555] Add multiuser support and more templating.
fabien
parents:
17
diff
changeset
|
41 |
'count' : kcnt, |
3 | 42 |
'checked' : chk}) |
43 |
return res |
|
44 |
||
4 | 45 |
def get_curl(): |
46 |
return urljoin( 'http://' + environ["HTTP_HOST"] + environ["REQUEST_URI"], 'add.py') |
|
47 |
||
17
14bec94bbe89
[svn r1551] Add delete bookmarks functionality as well as many reusability issues.
fabien
parents:
15
diff
changeset
|
48 |
def load_index(db): |
21
345ee7421989
[svn r1555] Add multiuser support and more templating.
fabien
parents:
17
diff
changeset
|
49 |
kw = set_selection(db, []) |
17
14bec94bbe89
[svn r1551] Add delete bookmarks functionality as well as many reusability issues.
fabien
parents:
15
diff
changeset
|
50 |
total = kw[0]['count'] |
14bec94bbe89
[svn r1551] Add delete bookmarks functionality as well as many reusability issues.
fabien
parents:
15
diff
changeset
|
51 |
kw = kw[1:] |
14bec94bbe89
[svn r1551] Add delete bookmarks functionality as well as many reusability issues.
fabien
parents:
15
diff
changeset
|
52 |
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
|
53 |
bookmarks = db.select_bookmarks([0], exc) |
14bec94bbe89
[svn r1551] Add delete bookmarks functionality as well as many reusability issues.
fabien
parents:
15
diff
changeset
|
54 |
if len(bookmarks)>0: |
14bec94bbe89
[svn r1551] Add delete bookmarks functionality as well as many reusability issues.
fabien
parents:
15
diff
changeset
|
55 |
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
|
56 |
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
|
57 |
curl = get_curl(); |
14bec94bbe89
[svn r1551] Add delete bookmarks functionality as well as many reusability issues.
fabien
parents:
15
diff
changeset
|
58 |
main(bookmarks, kw, curl, total) |
14bec94bbe89
[svn r1551] Add delete bookmarks functionality as well as many reusability issues.
fabien
parents:
15
diff
changeset
|
59 |
|
13 | 60 |
def main(bk, kw, curl, total): |
0 | 61 |
tmpl = TemplateManager().prepare("index.tmpl") |
62 |
tproc = TemplateProcessor() |
|
21
345ee7421989
[svn r1555] Add multiuser support and more templating.
fabien
parents:
17
diff
changeset
|
63 |
tproc.set("pagetitle", environ["REMOTE_USER"]+"'s XBELWeb") |
345ee7421989
[svn r1555] Add multiuser support and more templating.
fabien
parents:
17
diff
changeset
|
64 |
bk.sort(lambda x,y: cmp(x['name'],y['name'])) |
3 | 65 |
tproc.set("Bookmarks", bk) |
21
345ee7421989
[svn r1555] Add multiuser support and more templating.
fabien
parents:
17
diff
changeset
|
66 |
kw.sort(lambda x,y: cmp(x['keyword'],y['keyword'])) |
3 | 67 |
tproc.set("Keywords", kw) |
4 | 68 |
tproc.set("curl", curl) |
13 | 69 |
tproc.set("total", total) |
0 | 70 |
print tproc.process(tmpl) |
71 |
||
72 |
if (__name__ == "__main__"): |
|
73 |
form = cgi.FieldStorage() |
|
21
345ee7421989
[svn r1555] Add multiuser support and more templating.
fabien
parents:
17
diff
changeset
|
74 |
db = my_db.connect(environ["REMOTE_USER"]) |
345ee7421989
[svn r1555] Add multiuser support and more templating.
fabien
parents:
17
diff
changeset
|
75 |
selection = get_selection(form) |
345ee7421989
[svn r1555] Add multiuser support and more templating.
fabien
parents:
17
diff
changeset
|
76 |
keywords = set_selection(db, selection) |
345ee7421989
[svn r1555] Add multiuser support and more templating.
fabien
parents:
17
diff
changeset
|
77 |
total = keywords[0]['count'] |
13 | 78 |
keywords = keywords[1:] |
3 | 79 |
if selection[0] == 0: |
21
345ee7421989
[svn r1555] Add multiuser support and more templating.
fabien
parents:
17
diff
changeset
|
80 |
exc = map(lambda e: int(e['id']), keywords) |
3 | 81 |
bookmarks = db.select_bookmarks([0], exc) |
82 |
else: |
|
83 |
bookmarks = db.select_bookmarks(selection) |
|
6
da757ef67c69
[svn r1540] Correction for zero-lenght results in bookmark search.
fabien
parents:
4
diff
changeset
|
84 |
if len(bookmarks) > 0: |
da757ef67c69
[svn r1540] Correction for zero-lenght results in bookmark search.
fabien
parents:
4
diff
changeset
|
85 |
bookmarks = db.get_bookmarks(map(lambda x: x[0], bookmarks)) |
3 | 86 |
bookmarks = map(lambda bk: bk.dict(), bookmarks) |
4 | 87 |
curl = get_curl(); |
13 | 88 |
main(bookmarks, keywords, curl, total) |