Partial submission, to include tests.
--- a/INSTALL Sun Dec 28 15:21:49 2008 -0500
+++ b/INSTALL Tue Dec 30 22:32:10 2008 -0500
@@ -32,7 +32,7 @@
the CGI.
2. Tell the web server to serve only CGI .py from the root directory
- of the application. The lib and inc directory content should not
+ of the application. The lib and templates directory content should not
be used. Add any authorization you need.
3. Create a database for xbelweb (e.g., "xbelweb"), then create the
--- a/index.py Sun Dec 28 15:21:49 2008 -0500
+++ b/index.py Tue Dec 30 22:32:10 2008 -0500
@@ -13,15 +13,17 @@
# import cgitb; cgitb.enable()
import cgi
import my_db
+
+from local_config import CONFIG
from os import environ
-from actions import actions;
+from actions import actions
if (__name__ == "__main__"):
form = cgi.FieldStorage()
if form.has_key('debug'):
sys.stderr = sys.stdout
print "<pre>"
- db = my_db.connect(environ["REMOTE_USER"])
+ db = my_db.connect(CONFIG, environ["REMOTE_USER"])
prefs = db.get_preferences()
if form.has_key('action'):
action = form['action'].value
--- a/lib/config.py Sun Dec 28 15:21:49 2008 -0500
+++ b/lib/config.py Tue Dec 30 22:32:10 2008 -0500
@@ -1,8 +1,12 @@
#!/usr/bin/python
-host = 'www.myhost.com'
-user = 'mydbuser'
-passwd = 'mypass'
-database = 'bookmarker3'
-port = 8021
-template_dir = 'templates'
+class DefaultConfig:
+ def __init__(self):
+ self.db_host = 'www.myhost.com'
+ self.db_user = 'mydbuser'
+ self.db_passwd = 'mypass'
+ self.db_name = 'bookmarker3'
+ self.db_port = 5432
+ self.template_dir = 'templates'
+
+CONFIG = DefaultConfig()
--- a/lib/my_db.py Sun Dec 28 15:21:49 2008 -0500
+++ b/lib/my_db.py Tue Dec 30 22:32:10 2008 -0500
@@ -1,29 +1,33 @@
import pgdb
-import config
+import os
+
+# BADSMELL: There must be an interface between both
from bkmark import Bookmark
-import os
def sql_quote(str):
return pgdb._quote(str)
class MyDbConnexion:
- def __init__(self, name):
- self.cnx = pgdb.connect(database = config.database,
- host = config.host+':%d'%config.port,
- user = config.user, password = config.passwd)
+ def __init__(self, config):
+ self.cnx = pgdb.connect(database = config.db_name,
+ host = config.db_host+':%d'%config.db_port,
+ user = config.db_user, password = config.db_passwd)
self.crs = self.cnx.cursor();
- self.userid = self.get_user_id(name)
- if self.userid == None:
- self.userid = self.create_user(name)
- else:
- self.userid = self.userid[0]
- def get_user_id(self, name):
+ def connect_user(self, name):
+ if (self.userid == None):
+ self.userid = self._get_user_id(username)
+ if self.userid == None:
+ self.userid = self._create_user(username)
+ else:
+ self.userid = self.userid[0]
+ return self.userid
+ def _get_user_id(self, name):
self.crs.execute("""
SELECT id FROM users
WHERE name = %s;
""" % (sql_quote(name),))
return self.crs.fetchone()
- def create_user(self, name):
+ def _create_user(self, name):
id = self.get_next_id('users')
self.crs.execute("""
INSERT INTO users(id, name, full_name)
@@ -63,7 +67,7 @@
DELETE FROM preferences WHERE userid = %d;
DELETE FROM keywords WHERE userid = %d;
DELETE FROM db_sequence WHERE userid = %d;
- """ % (userid,userid,userid,userid,userid))
+ """ % ((userid,)*5))
self.cnx.commit()
def get_preferences(self):
self.crs.execute("""
@@ -392,5 +396,6 @@
""" % (id+1, seq_name, userid))
return id
-def connect(username):
- return MyDbConnexion(username)
+def connect(config, username):
+ db = MyDbConnexion(config)
+ db.connect_user(username)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/lib/test_my_db.py Tue Dec 30 22:32:10 2008 -0500
@@ -0,0 +1,40 @@
+if __name__ == '__main__':
+ import sys
+ sys.path.insert(0, "../../lib")
+
+import unittest
+import my_db
+import testconfig
+import pgdb
+
+class TestMyDbConnexion(unittest.TestCase):
+ def setUp(self):
+ " Setup a test-db environment."
+ self.db = my_db.connect(testconfig.CONFIG, "test")
+ self.prefs = { 'keywords_box' : 10,
+ 'keywords_sort' : 'count',
+ 'keywords_reverse': True,
+ 'fullname': 'Test User',
+ 'default_view': 1 }
+
+ def tearDown(self):
+ " Tear down the test-db environment"
+ self.db.delete_user('test')
+ pass
+
+ def testConnect(self):
+ self.assertNotEqual(db, None)
+ self.assertNotEqual(db.userid, -1)
+
+ def testGetPreferences(self):
+ prefs = self.db.get_preferences()
+ self.assertEqual(prefs, self.prefs)
+
+ def testSetPreferences(self):
+ self.db.set_preferences(self.prefs)
+ prefs = self.db.get_preferences()
+ self.assertEqual(prefs, self.prefs)
+
+
+if __name__ == '__main__':
+ unittest.main()