--- a/index.py Tue Oct 07 00:05:59 2003 -0400
+++ b/index.py Tue Oct 07 00:06:23 2003 -0400
@@ -12,77 +12,25 @@
# import cgitb; cgitb.enable()
import cgi
-from htmltmpl import TemplateManager, TemplateProcessor
import my_db
-from os import environ
-from urlparse import urljoin
-
-def get_selection(form):
- kw = form.getvalue("kw")
- if not isinstance(kw, type([])):
- if kw:
- kw = [kw]
- else:
- kw = [0]
- return map(int, kw)
-
-def set_selection(db, sel):
- allkw = db.get_all_keywords()
- (kw, cnt) = map(list,apply(zip, db.get_keywords_count()))
- res = []
- for key in allkw:
- chk = key[0] in sel
- if key[0] in kw:
- kcnt = cnt[kw.index(key[0])]
- else:
- kcnt = 0
- res.append({'id':key[0],
- 'keyword': key[1],
- 'count' : kcnt,
- 'checked' : chk})
- return res
-
-def get_curl():
- return urljoin( 'http://' + environ["HTTP_HOST"] + environ["REQUEST_URI"], 'add.py')
-
-def load_index(db):
- kw = set_selection(db, [])
- total = kw[0]['count']
- kw = kw[1:]
- exc = map(lambda e: int(e['id']), kw)
- bookmarks = db.select_bookmarks([0], exc)
- if len(bookmarks)>0:
- bookmarks = db.get_bookmarks(map(lambda x: x[0], bookmarks))
- bookmarks = map(lambda bk: bk.dict(), bookmarks)
- curl = get_curl();
- main(bookmarks, kw, curl, total)
-
-def main(bk, kw, curl, total):
- tmpl = TemplateManager().prepare("index.tmpl")
- tproc = TemplateProcessor()
- tproc.set("pagetitle", environ["REMOTE_USER"]+"'s XBELWeb")
- bk.sort(lambda x,y: cmp(x['name'],y['name']))
- tproc.set("Bookmarks", bk)
- kw.sort(lambda x,y: -cmp(x['count'],y['count']))
- tproc.set("Keywords", kw)
- tproc.set("curl", curl)
- tproc.set("total", total)
- print tproc.process(tmpl)
+from webutils import *
if (__name__ == "__main__"):
form = cgi.FieldStorage()
db = my_db.connect(environ["REMOTE_USER"])
- selection = get_selection(form)
- keywords = set_selection(db, selection)
- total = keywords[0]['count']
- keywords = keywords[1:]
- if selection[0] == 0:
- exc = map(lambda e: int(e['id']), keywords)
+ selection = get_keywords(form, 'sel')
+ exclusion = get_keywords(form, 'exc')
+ if len(selection) == 0:
+ keywords = set_selection(db, [], selection, exclusion)
+ exc = map(lambda e: int(e['id']), keywords[1:])
bookmarks = db.select_bookmarks([0], exc)
else:
- bookmarks = db.select_bookmarks(selection)
+ bookmarks = db.select_bookmarks(selection, exclusion)
+ keywords = set_selection(db,
+ map(lambda e: e[0], bookmarks),
+ selection, exclusion)
+ total = len(bookmarks)
if len(bookmarks) > 0:
bookmarks = db.get_bookmarks(map(lambda x: x[0], bookmarks))
bookmarks = map(lambda bk: bk.dict(), bookmarks)
- curl = get_curl();
- main(bookmarks, keywords, curl, total)
+ process_index(bookmarks, keywords)