[svn r1582] Add some user preferences support (kw sorting and kw select box size).
--- a/TODO Tue Oct 07 11:10:52 2003 -0400
+++ b/TODO Tue Oct 07 12:50:30 2003 -0400
@@ -10,4 +10,4 @@
* Add user session support.
* Better unicode handling.
* Selection based bookmark manipulation.
-* sort options.
+* More user preferences.
--- a/add_bk.tmpl Tue Oct 07 11:10:52 2003 -0400
+++ b/add_bk.tmpl Tue Oct 07 12:50:30 2003 -0400
@@ -14,7 +14,8 @@
</fieldset>
<fieldset class="keywords">
<legend>Keywords:</legend>
- <label>Selected: <select multiple name="kw">
+ <label>Selected:
+ <select multiple name="kw" size="<TMPL_VAR kw_size>">
<TMPL_LOOP Keywords>
<option value="<TMPL_VAR id>"
<TMPL_IF selected>selected</TMPL_IF> />
--- a/edit_kw.tmpl Tue Oct 07 11:10:52 2003 -0400
+++ b/edit_kw.tmpl Tue Oct 07 12:50:30 2003 -0400
@@ -3,7 +3,8 @@
<body>
<h1><TMPL_VAR pagetitle></h1>
<form action="do_edit_kw.py" method="get" name="edit">
- <label>Keywords: <select multiple name="kw">
+ <label>Keywords:
+ <select multiple name="kw" size="<TMPL_VAR kw_size>">
<TMPL_LOOP Keywords>
<option value="<TMPL_VAR id>"><TMPL_VAR keyword></option>
</TMPL_LOOP></select><br/>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/edit_prefs.py Tue Oct 07 12:50:30 2003 -0400
@@ -0,0 +1,29 @@
+#!/usr/bin/python
+
+import sys
+import traceback
+sys.path.insert(0, "/home/fabien/lib/python")
+sys.path.insert(0, "./lib")
+sys.stderr = sys.stdout
+
+print "Content-type: text/html; charset=iso-8859-1;"
+print
+
+# import cgitb; cgitb.enable()
+import cgi
+import my_db
+from os import environ
+from webutils import load_index
+
+if (__name__ == "__main__"):
+ form = cgi.FieldStorage()
+ db = my_db.connect(environ["REMOTE_USER"])
+ prefs = db.get_preferences()
+ if form.has_key('kw_size'):
+ prefs['keywords_box'] = int(form['kw_size'].value)
+ if form.has_key('kw_sort'):
+ prefs['keywords_sort'] = form['kw_sort'].value
+ if form.has_key('kw_reverse'):
+ prefs['keywords_reverse'] = form['kw_reverse'].value <> 0
+ db.set_preferences(prefs)
+ load_index(db)
--- a/inc/headers.tmpl Tue Oct 07 11:10:52 2003 -0400
+++ b/inc/headers.tmpl Tue Oct 07 12:50:30 2003 -0400
@@ -1,5 +1,5 @@
<head>
- <title><TMPL_VAR pagetitle></title>
+ <title><TMPL_VAR remote_user>'s XBelWeb</title>
<meta name="generator" content="XBelWeb"/>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"
<link rel="home" href="index.py" title="XBelWeb's Main Page"/>
--- a/index.py Tue Oct 07 11:10:52 2003 -0400
+++ b/index.py Tue Oct 07 12:50:30 2003 -0400
@@ -33,4 +33,4 @@
if len(bookmarks) > 0:
bookmarks = db.get_bookmarks(map(lambda x: x[0], bookmarks))
bookmarks = map(lambda bk: bk.dict(), bookmarks)
- process_index(bookmarks, keywords)
+ process_index(bookmarks, keywords, db.get_preferences())
--- a/index.tmpl Tue Oct 07 11:10:52 2003 -0400
+++ b/index.tmpl Tue Oct 07 12:50:30 2003 -0400
@@ -8,13 +8,17 @@
<fieldset class="keywords">
<legend>Keywords:</legend>
<p>
- <label>Include: <select multiple name="sel" onchange="submit()">
+ <label>Include:
+ <select multiple name="sel" onchange="submit()"
+ size="<TMPL_VAR kw_size>">
<TMPL_LOOP Keywords>
<option value="<TMPL_VAR id>"
<TMPL_IF selected>selected</TMPL_IF>
><TMPL_VAR keyword> (<TMPL_VAR count>)</option>
</TMPL_LOOP></select>
- <label>Exclude: <select multiple name="exc" onchange="submit()">
+ <label>Exclude:
+ <select multiple name="exc" onchange="submit()"
+ size="<TMPL_VAR kw_size>">
<TMPL_LOOP Keywords>
<option value="<TMPL_VAR id>"
<TMPL_IF excluded>selected</TMPL_IF>
@@ -36,7 +40,8 @@
<hr/>
<p><a href="add.py">Add new bookmark</a>.<br/>
<a href="import.py">Import a XBEL file</a>.<br/>
- <a href="edit_kw.py">Edit keywords</a>.</p>
+ <a href="edit_kw.py">Edit keywords</a>.<br/>
+ <a href="prefs.py">Edit User Preferences</a>.</p>
<p>Here the <a
href="javascript:bk1='<TMPL_VAR curl>?curl='+escape(location.href)+'&ctitle='+escape(document.title);bkwin=window.open(bk1,'bkqm','width=620,height=500,scrollbars=1,resizable=1');bkwin.focus();">Quick
Mark Link</a>. Put it on your bookmarks (using <tt><Right-Mouse
--- a/kw_confirm.tmpl Tue Oct 07 11:10:52 2003 -0400
+++ b/kw_confirm.tmpl Tue Oct 07 12:50:30 2003 -0400
@@ -23,7 +23,8 @@
<TMPL_VAR keyword>;
</TMPL_LOOP>
</p>
- <label>Merge them with: <select name="kw">
+ <label>Merge them with:
+ <select name="kw" size="<TMPL_VAR kw_size>">
<TMPL_LOOP Keywords>
<option value="<TMPL_VAR id>"><TMPL_VAR keyword></option>
</TMPL_LOOP></select><br/>
--- a/lib/create_db.sql Tue Oct 07 11:10:52 2003 -0400
+++ b/lib/create_db.sql Tue Oct 07 12:50:30 2003 -0400
@@ -8,6 +8,14 @@
CREATE INDEX index_users_name ON users(name);
+CREATE TABLE preferences (
+ userid int NOT NULL,
+ keywords_box int DEFAULT 5 not NULL,
+ keywords_sort varchar(31) DEFAULT 'keyword' NOT NULL,
+ keywords_reverse boolean DEFAULT FALSE NOT NULL,
+ PRIMARY KEY (userid)
+);
+
--
-- Table structure for table 'bookmark'
--
--- a/lib/my_db.py Tue Oct 07 11:10:52 2003 -0400
+++ b/lib/my_db.py Tue Oct 07 12:50:30 2003 -0400
@@ -43,16 +43,48 @@
INSERT INTO db_sequence(seq_name, userid, nextid)
VALUES ('bookmarks', %d, 0);
""" %(id,))
+ self.crs.execute("""
+ INSERT INTO preferences(userid)
+ VALUES (%d);
+ """ %(id,))
self.cnx.commit()
return id
def delete_user(self, userid):
- self.crs_execute("""
+ self.crs.execute("""
DELETE FROM users WHERE id = %d;
DELETE FROM bookmarks_keywords WHERE userid = %d;
DELETE FROM bookmarks WHERE userid = %d;
DELETE FROM keywords WHERE userid = %d;
DELETE FROM db_sequence WHERE userid = %d;
""" % (userid,userid,userid,userid,userid))
+ self.cnx.commit()
+ def get_preferences(self):
+ self.crs.execute("""
+ SELECT keywords_box,
+ keywords_sort, keywords_reverse
+ FROM preferences WHERE userid = %d;
+ """ % (self.userid,))
+ res = self.crs.fetchone()
+ return {
+ 'keywords_box' : res[0],
+ 'keywords_sort' : res[1],
+ 'keywords_reverse': res[2]
+ }
+ def set_preferences(self, prefs):
+ if prefs['keywords_reverse']:
+ rev = 'TRUE'
+ else:
+ rev = 'FALSE'
+ self.crs.execute("""
+ UPDATE preferences
+ SET keywords_box = %d,
+ keywords_sort = %s,
+ keywords_reverse = %s
+ WHERE userid = %d;
+ """ % (prefs['keywords_box'],
+ sql_quote(prefs['keywords_sort']),
+ rev, self.userid))
+ self.cnx.commit()
def add_bookmark(self, bk):
id = self.get_next_id('bookmarks')
self.crs.execute("""
--- a/lib/webutils.py Tue Oct 07 11:10:52 2003 -0400
+++ b/lib/webutils.py Tue Oct 07 12:50:30 2003 -0400
@@ -44,6 +44,17 @@
'unselected' : is_unselected})
return res
+def sort_keywords(kw, pref):
+ global _sk_rev_fact
+ global _sk_sort
+ if pref['keywords_reverse']:
+ _sk_rev_fact = -1
+ else:
+ _sk_rev_fact = 1
+ _sk_sort = pref['keywords_sort']
+ kw.sort(lambda l, r: _sk_rev_fact*cmp(l[_sk_sort], r[_sk_sort]))
+ return kw
+
def get_curl():
return urljoin( 'http://' + environ["HTTP_HOST"] + environ["REQUEST_URI"], 'add.py')
@@ -55,15 +66,23 @@
if len(bookmarks)>0:
bookmarks = db.get_bookmarks(map(lambda x: x[0], bookmarks))
bookmarks = map(lambda bk: bk.dict(), bookmarks)
- process_index(bookmarks, keywords)
+ pref = db.get_preferences()
+ process_index(bookmarks, keywords[1:], pref)
-def process_index(bk, kw):
+def get_template_processor(prefs):
+ tproc = TemplateProcessor()
+ tproc.set("remote_user", environ["REMOTE_USER"])
+ tproc.set("kw_size", prefs['keywords_box'])
+ tproc.set("kw_sort", prefs['keywords_sort'])
+ tproc.set("kw_reverse", prefs['keywords_reverse'])
+ return tproc
+
+def process_index(bk, kw, pref):
tmpl = TemplateManager().prepare("index.tmpl")
- tproc = TemplateProcessor()
- tproc.set("pagetitle", environ["REMOTE_USER"]+"'s XBELWeb")
+ tproc = get_template_processor(pref)
bk.sort(lambda x,y: cmp(x['name'],y['name']))
tproc.set("Bookmarks", bk)
- kw.sort(lambda x,y: -cmp(x['count'],y['count']))
+ kw = sort_keywords(kw[1:], pref)
tproc.set("Keywords", kw)
tproc.set("curl", get_curl())
tproc.set("total", len(bk))
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/prefs.py Tue Oct 07 12:50:30 2003 -0400
@@ -0,0 +1,31 @@
+#!/usr/bin/python
+
+import sys
+import traceback
+sys.path.insert(0, "/home/fabien/lib/python")
+sys.path.insert(0, "./lib")
+sys.stderr = sys.stdout
+
+print "Content-type: text/html; charset=iso-8859-1;"
+print
+
+# import cgitb; cgitb.enable()
+import cgi
+from htmltmpl import TemplateManager
+from webutils import get_template_processor
+import my_db
+import os
+
+def main(prefs):
+ tmpl = TemplateManager().prepare("prefs.tmpl")
+ tproc = get_template_processor(prefs)
+ fields = []
+ for field in ['keyword', 'count']:
+ fields.append({'field': field,
+ 'selected' : prefs['keywords_sort'] == field })
+ tproc.set('Sortfields', fields)
+ print tproc.process(tmpl)
+
+if (__name__ == "__main__"):
+ db = my_db.connect(os.environ["REMOTE_USER"])
+ main(db.get_preferences())
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/prefs.tmpl Tue Oct 07 12:50:30 2003 -0400
@@ -0,0 +1,43 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+ <TMPL_INCLUDE NAME="headers.tmpl">
+ <body>
+ <h1><TMPL_VAR remote_user>'s Preferences</h1>
+ <form action="edit_prefs.py" method="get" name="keywords">
+ <p><label>Size of keywords selection box:
+ <input type="text" name="kw_size"
+ value="<TMPL_VAR kw_size>"/></label></p>
+ <p><label>Sort keywords by:
+ <select name="kw_sort" size="1">
+ <TMPL_LOOP Sortfields>
+ <option value="<TMPL_VAR field>"
+ <TMPL_IF selected>selected</TMPL_IF>
+ /><TMPL_VAR field></option>
+ </TMPL_LOOP>
+ </select>
+ <input type="checkbox" name="kw_reverse"
+ value="<TMPL_VAR kw_reverse>"/>
+ reversed</p>
+ <button>Submit</button>
+ </form>
+ <hr/>
+ <a href="index.py">Back to main page</a>
+</html>
+<!-- Keep this comment at the end of the file
+Local variables:
+mode: sgml
+sgml-omittag:t
+sgml-shorttag:t
+sgml-namecase-general:t
+sgml-general-insert-case:lower
+sgml-minimize-attributes:nil
+sgml-always-quote-attributes:t
+sgml-indent-step:2
+sgml-indent-data:nil
+sgml-parent-document:nil
+sgml-exposed-tags:nil
+sgml-local-catalogs:nil
+sgml-local-ecat-files:nil
+End:
+vi: syntax=html
+-->