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 |
print
|
|
11 |
|
|
12 |
# import cgitb; cgitb.enable()
|
|
13 |
import cgi
|
|
14 |
from htmltmpl import TemplateManager, TemplateProcessor
|
|
15 |
import my_db
|
|
16 |
|
3
|
17 |
def get_selection(form):
|
|
18 |
kw = form.getvalue("kw")
|
|
19 |
if not isinstance(kw, type([])):
|
|
20 |
if kw:
|
|
21 |
kw = [kw]
|
|
22 |
else:
|
|
23 |
kw = [0]
|
|
24 |
return map(int, kw)
|
|
25 |
|
|
26 |
def set_selection(kw, sel):
|
|
27 |
res = []
|
|
28 |
for key in kw:
|
|
29 |
chk = key[0] in sel
|
|
30 |
res.append({'id':key[0],
|
|
31 |
'keyword': key[1],
|
|
32 |
'checked' : chk})
|
|
33 |
return res
|
|
34 |
|
|
35 |
def main(bk, kw):
|
0
|
36 |
tmpl = TemplateManager().prepare("index.tmpl")
|
|
37 |
tproc = TemplateProcessor()
|
3
|
38 |
tproc.set("Bookmarks", bk)
|
|
39 |
tproc.set("Keywords", kw)
|
0
|
40 |
print tproc.process(tmpl)
|
|
41 |
|
|
42 |
if (__name__ == "__main__"):
|
|
43 |
form = cgi.FieldStorage()
|
3
|
44 |
db = my_db.connect()
|
|
45 |
keywords = db.get_all_keywords()[1:]
|
|
46 |
selection = get_selection(form)
|
|
47 |
if selection[0] == 0:
|
|
48 |
exc = map(lambda e: int(e[0]), keywords)
|
|
49 |
bookmarks = db.select_bookmarks([0], exc)
|
|
50 |
else:
|
|
51 |
bookmarks = db.select_bookmarks(selection)
|
|
52 |
keywords = set_selection(keywords, selection)
|
|
53 |
bookmarks = db.get_bookmarks(map(lambda x: x[0], bookmarks))
|
|
54 |
bookmarks = map(lambda bk: bk.dict(), bookmarks)
|
|
55 |
main(bookmarks, keywords)
|