rename LdapConnection to DatabaseWrapper and accept configuration as a dict
[matthijs/upstream/django-ldapdb.git] / ldapdb / __init__.py
index c6f0974476129a1e1d910d536ddc446f0027ef62..7fec75dc41c733a6cebdddff51656ce4563c2700 100644 (file)
@@ -38,7 +38,7 @@ from django.conf import settings
 from django.db.backends import BaseDatabaseFeatures, BaseDatabaseOperations
 
 def escape_ldap_filter(value):
-    value = str(value)
+    value = unicode(value)
     return value.replace('\\', '\\5c') \
                 .replace('*', '\\2a') \
                 .replace('(', '\\28') \
@@ -50,25 +50,30 @@ class DatabaseCursor(object):
         self.connection = ldap_connection
 
 class DatabaseFeatures(BaseDatabaseFeatures):
-    pass
+    def __init__(self, connection):
+        self.connection = connection
 
 class DatabaseOperations(BaseDatabaseOperations):
     def quote_name(self, name):
         return name
 
-class LdapConnection(object):
-    def __init__(self):
+class DatabaseWrapper(object):
+    def __init__(self, settings_dict={}, alias='ldap'):
+        self.settings_dict = settings_dict
         self.connection = None
         self.charset = "utf-8"
-        self.features = DatabaseFeatures()
+        self.features = DatabaseFeatures(self)
         self.ops = DatabaseOperations()
 
+    def close(self):
+        pass
+
     def _cursor(self):
         if self.connection is None:
-            self.connection = ldap.initialize(settings.LDAPDB_SERVER_URI)
+            self.connection = ldap.initialize(self.settings_dict['NAME'])
             self.connection.simple_bind_s(
-                settings.LDAPDB_BIND_DN,
-                settings.LDAPDB_BIND_PASSWORD)
+                self.settings_dict['USER'],
+                self.settings_dict['PASSWORD'])
         return DatabaseCursor(self.connection)
 
     def add_s(self, dn, modlist):
@@ -96,5 +101,8 @@ class LdapConnection(object):
         return output
 
 # FIXME: is this the right place to initialize the LDAP connection?
-connection = LdapConnection()
+connection = DatabaseWrapper({
+    'NAME': settings.LDAPDB_SERVER_URI,
+    'USER': settings.LDAPDB_BIND_DN,
+    'PASSWORD': settings.LDAPDB_BIND_PASSWORD})