[svn r1549] Add update/merge/delete keywords. xbelweb
authorfabien
Thu, 25 Sep 2003 11:02:05 -0400
branchxbelweb
changeset 15 574631f841c3
parent 14 cdb4ed8660c5
child 16 07adce9ccccb
[svn r1549] Add update/merge/delete keywords.
TODO
add_bk.tmpl
do_edit_kw.py
edit_kw.py
edit_kw.tmpl
index.py
index.tmpl
kw_confirm.tmpl
kw_result.py
lib/my_db.py
--- a/TODO	Wed Sep 24 17:51:18 2003 -0400
+++ b/TODO	Thu Sep 25 11:02:05 2003 -0400
@@ -1,10 +1,12 @@
 * Used SQL sequence instead of the db_sequence table.
 * Add delete bookmark.
-* Add edit/delete/merge keyword.
 * Add CSS.
 * Add better navigational means.
-* Add limit query page or move keywords up.
+* Add limit query page
 * Add Search capabilities.
 * Add default (dynamic?) tree expansion.
 * Add multiuser support.
 * Add XBel Import/Export.
+* Add support for icons.
+* Add modified time.
+* Add access/check time.
--- a/add_bk.tmpl	Wed Sep 24 17:51:18 2003 -0400
+++ b/add_bk.tmpl	Thu Sep 25 11:02:05 2003 -0400
@@ -17,14 +17,17 @@
       </fieldset>
       <fieldset class="keywords">
         <legend>Mots clés:</legend>
-	<ul>
+	<p>
 	<TMPL_LOOP Keywords>
-	  <li><input type="checkbox" name="kw"
+	  <input type="checkbox" name="kw"
 	  <TMPL_IF checked>checked</TMPL_IF>
-	  value="<TMPL_VAR id>"/><TMPL_VAR keyword></li>
+	  value="<TMPL_VAR id>"/><TMPL_VAR keyword>;
 	</TMPL_LOOP>
-	</ul>
+	</p>
 	<label>New keywords: <input type="text" name="newkw"> (comma separated list)</label>
     </form>
   </body>
 </html>
+<!--
+vi: syntax=html
+-->
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/do_edit_kw.py	Thu Sep 25 11:02:05 2003 -0400
@@ -0,0 +1,67 @@
+#!/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, TemplateProcessor
+import my_db
+import time
+from bkmark import Bookmark
+
+def get_bk_from_form(form):
+	bk = Bookmark()
+	bk.id = int(form['id'].value)
+	bk.url = form['url'].value
+	if form.has_key('name'):
+		bk.name = form['name'].value
+	if form.has_key('desc'):
+		bk.desc = form['desc'].value
+	return bk
+
+def get_kw_from_form(form):
+	kw = form.getvalue("kw")
+	if not isinstance(kw, type([])):
+		if kw:
+			kw = [kw]
+		else:
+			kw = []
+	kw = map(int, kw)
+	return kw
+
+def get_new_kw_from_form(form):
+	if form.has_key('newkw'):
+		return map(lambda e: e.strip(),
+			form['newkw'].value.split(','))
+	else:
+		return []
+
+if (__name__ == "__main__"):
+    form = cgi.FieldStorage()
+    db = my_db.connect()
+    id = int(form['id'].value)
+    action = form['action'].value
+    kwname = db.get_keyword(id)
+    name = form['name'].value
+    merge = int(form['keywords'].value)
+    mergename = db.get_keyword(merge)
+    tmpl = TemplateManager().prepare("kw_confirm.tmpl")
+    tproc = TemplateProcessor()
+    tproc.set("confirm_delete", action == 'delete')
+    tproc.set("confirm_merge", action == 'merge')
+    tproc.set("confirm_update", action == 'update')
+    tproc.set("action", action)
+    tproc.set("id", id)
+    tproc.set("name", kwname)
+    tproc.set("newname", name)
+    tproc.set("mergeid", merge)
+    tproc.set("mergename", mergename)
+    print tproc.process(tmpl)
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/edit_kw.py	Thu Sep 25 11:02:05 2003 -0400
@@ -0,0 +1,36 @@
+#!/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, TemplateProcessor
+import my_db
+import time
+
+def main(keyword, id, keywords):
+    tmpl = TemplateManager().prepare("edit_kw.tmpl")
+    tproc = TemplateProcessor()
+    tproc.set('keyword', keyword)
+    tproc.set('id', id)
+    tproc.set('Keywords', keywords)
+    print tproc.process(tmpl)
+
+if (__name__ == "__main__"):
+    form = cgi.FieldStorage()
+    id = int(form["id"].value)
+    db = my_db.connect()
+    name = db.get_keyword(id)
+    kw = db.get_all_keywords()[1:]
+    kw.sort(lambda l,r: cmp(l[1],r[1]))
+    kw = map(lambda elem: {
+   	 'id' : elem[0],
+	 'keyword' : elem[1] }, kw)
+    main(name, id, kw)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/edit_kw.tmpl	Thu Sep 25 11:02:05 2003 -0400
@@ -0,0 +1,24 @@
+<html>
+  <head>
+    <title>Edit a keyword</title>
+  </head>
+  <body>
+    <h1>Edit a keyword</h1>
+    <form action="do_edit_kw.py" method="post" name="edit">
+      <input type="hidden" name="id" value="<TMPL_VAR id>"/>
+      <label>Name: <input type="text" name="name"
+                          value="<TMPL_VAR keyword>"/></label>
+      <button name="action" type="submit" value="update">Update</button><br/>
+      <label>Keyword: <select name="keywords">
+	<TMPL_LOOP Keywords>
+	  <option value="<TMPL_VAR id>"/><TMPL_VAR keyword></option>
+	</TMPL_LOOP>
+      </select></label>
+      <button name="action" type="submit" value="merge">Merge</button><br/>
+      <button name="action" type="submit" value="delete">Delete</button>
+    </form>
+  </body>
+</html>
+<!--
+vi: syntax=html
+-->
--- a/index.py	Wed Sep 24 17:51:18 2003 -0400
+++ b/index.py	Thu Sep 25 11:02:05 2003 -0400
@@ -2,12 +2,13 @@
 
 import sys
 import traceback
-sys.path.insert(0, "/home/fabien/lib/python")
-sys.path.insert(0, "./lib")
-sys.stderr = sys.stdout
+if (__name__ == '__main__'):
+	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
+	print "Content-type: text/html; charset=iso-8859-1;"
+	print
 
 # import cgitb; cgitb.enable()
 import cgi
--- a/index.tmpl	Wed Sep 24 17:51:18 2003 -0400
+++ b/index.tmpl	Thu Sep 25 11:02:05 2003 -0400
@@ -7,7 +7,7 @@
   <body>
     <h1>Fabien's XBelWeb</h1>
     <p>Total bookmarks: <TMPL_VAR total></p>
-    <form action="index.py" method="post" name="keywords">
+    <form action="index.py" method="get" name="keywords">
       <fieldset class="keywords">
         <legend>Keywords:</legend>
 	<p>
@@ -15,11 +15,11 @@
 	  <input type="checkbox" name="kw"
 	  <TMPL_IF checked>checked</TMPL_IF>
 	  value="<TMPL_VAR id>"
-	  /><TMPL_VAR keyword>(<TMPL_VAR count>);
+	  /><a href="edit_kw.py?id=<TMPL_VAR id>"><TMPL_VAR keyword></a>(<TMPL_VAR count>);
 	</TMPL_LOOP>
-	</ul>
+	</p>
+      <button type="submit">Refresh</button>
       </fieldset>
-      <button type="submit">Refresh</button>
     </form>
     <dl>
     <TMPL_LOOP Bookmarks>
@@ -53,4 +53,5 @@
 sgml-local-catalogs:nil
 sgml-local-ecat-files:nil
 End:
+vi: syntax=html
 -->
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kw_confirm.tmpl	Thu Sep 25 11:02:05 2003 -0400
@@ -0,0 +1,48 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+  <head>
+    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-15">
+    <title>Fabien's XBelWeb</title>
+  </head>
+  <body>
+    <h1>Fabien's XBelWeb</h1>
+    <h2>Confirmation</h2>
+    <form name="confirmation" method="post" action="kw_result.py">
+      <input type="hidden" name="id" value="<TMPL_VAR id>"/>
+      <input type="hidden" name="merge" value="<TMPL_VAR mergeid>"/>
+      <input type="hidden" name="name" value="<TMPL_VAR newname>"/>
+      <TMPL_IF confirm_delete>
+        <p>Do you really want to remove <em><TMPL_VAR name></em>?</p>
+      </TMPL_IF>
+      <TMPL_IF confirm_merge>
+        <p>Do you really want to merge <em><TMPL_VAR name></em> into
+          <em><TMPL_VAR mergename></em>?</p>
+      </TMPL_IF>
+      <TMPL_IF confirm_update>
+        <p>Do you really want to rename <em><TMPL_VAR name></em> into
+          <em><TMPL_VAR newname></em>?</p>
+      </TMPL_IF>
+      <button name="action" type="submit"
+              value="<TMPL_VAR action>">Yes, I want</button>
+      <button name="action" type="submit" value="cancel">Cancel</button>
+    </form>
+  </body>
+</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
+-->
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kw_result.py	Thu Sep 25 11:02:05 2003 -0400
@@ -0,0 +1,54 @@
+#!/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, TemplateProcessor
+import my_db
+from os import environ
+from urlparse import urljoin
+from index import set_selection, get_curl, main
+
+def do_merge(form, db):
+	fromid = int(form['id'].value)	
+	toid = int(form['merge'].value)	
+	db.merge_keywords(fromid, toid)
+
+def do_update(form, db):
+	id = int(form['id'].value)
+	newname = form['name'].value
+	db.update_keyword(id, newname)
+ 
+def do_delete(form, db):
+	id = int(form['id'].value)
+	db.remove_keyword(id)
+
+if (__name__ == "__main__"):
+    form = cgi.FieldStorage()
+    db = my_db.connect()
+    action = form['action'].value
+    if action == 'merge':
+	do_merge(form, db)
+    elif action == 'update':
+	do_update(form, db)
+    elif action == 'delete':
+    	do_delete(form, db)
+    keywords = db.get_all_keywords()
+    total = keywords[0][2]
+    keywords = keywords[1:]
+    exc = map(lambda e: int(e[0]), keywords)
+    bookmarks = db.select_bookmarks([0], exc)
+    keywords = set_selection(keywords, [])
+    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)
--- a/lib/my_db.py	Wed Sep 24 17:51:18 2003 -0400
+++ b/lib/my_db.py	Thu Sep 25 11:02:05 2003 -0400
@@ -52,7 +52,7 @@
 			SET url = %s, name = %s,
 			ldesc = %s, added = %s
 			WHERE id = %d;
-			""" % (bk.url, sql_quote(bk.name),
+			""" % (sql_quote(bk.url), sql_quote(bk.name),
 			sql_quote(bk.desc),
 			sql_quote(bk.added), bk.id))
 		self.cnx.commit()
@@ -87,7 +87,7 @@
 	def get_keyword(self, id):
 		self.crs.execute("""
 			SELECT name FROM keywords
-			WHERE keyword.id = %d;
+			WHERE id = %d;
 			""" % (id,))
 		return self.crs.fetchone()[0]
 	def get_keywords(self, bk_id):
@@ -119,8 +119,12 @@
 		self.crs.execute("""
 			UPDATE bookmarks_keywords
 			SET keyword = %d
-			WHERE keyword = %d;
-			""" % (into_id, merge_id))
+			WHERE keyword = %d
+			AND NOT ( bookmark IN 
+			  (SELECT bookmark FROM bookmarks_keywords
+			  WHERE keyword = %d ))
+			;
+			""" % (into_id, merge_id, merge_id))
 		self.remove_keyword(merge_id)
 	def get_all_keywords(self):
 		"""Return a list of triplets [id, keyword, count]."""