From b1cc6993cafbf24e8a394d8b9bea1048efa8adc9 Mon Sep 17 00:00:00 2001 From: jlaine Date: Mon, 24 May 2010 19:38:56 +0000 Subject: [PATCH] delay LDAP connection establishment git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@878 e071eeec-0327-468d-9b6a-08194a12b294 --- ldapdb/__init__.py | 36 +++++++++++++++++++++++++----------- ldapdb/models/query.py | 6 +++--- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/ldapdb/__init__.py b/ldapdb/__init__.py index f466bea..4b0a6d5 100644 --- a/ldapdb/__init__.py +++ b/ldapdb/__init__.py @@ -41,6 +41,10 @@ def escape_ldap_filter(value): .replace(')', '\\29') \ .replace('\0', '\\00') +class DatabaseCursor(object): + def __init__(self, ldap_connection): + self.connection = ldap_connection + class DatabaseFeatures(BaseDatabaseFeatures): pass @@ -49,13 +53,20 @@ class DatabaseOperations(BaseDatabaseOperations): return name class LdapConnection(object): - def __init__(self, server, bind_dn, bind_password): - self.connection = ldap.initialize(server) - self.connection.simple_bind_s(bind_dn, bind_password) + def __init__(self): + self.connection = None self.charset = "utf-8" self.features = DatabaseFeatures() self.ops = DatabaseOperations() + def _cursor(self): + if self.connection is None: + self.connection = ldap.initialize(settings.LDAPDB_SERVER_URI) + self.connection.simple_bind_s( + settings.LDAPDB_BIND_DN, + settings.LDAPDB_BIND_PASSWORD) + return DatabaseCursor(self.connection) + def add_s(self, dn, modlist): mods = [] for field, value in modlist: @@ -64,22 +75,27 @@ class LdapConnection(object): mods.append((field, converted)) else: mods.append((field, [converted])) - return self.connection.add_s(dn.encode(self.charset), mods) + cursor = self._cursor() + return cursor.connection.add_s(dn.encode(self.charset), mods) def delete_s(self, dn): - return self.connection.delete_s(dn.encode(self.charset)) + cursor = self._cursor() + return cursor.connection.delete_s(dn.encode(self.charset)) def modify_s(self, dn, modlist): mods = [] for op, field, value in modlist: mods.append((op, field, convert(field, value, lambda x: x.encode(self.charset)))) - return self.connection.modify_s(dn.encode(self.charset), mods) + cursor = self._cursor() + return cursor.connection.modify_s(dn.encode(self.charset), mods) def rename_s(self, dn, newrdn): - return self.connection.rename_s(dn.encode(self.charset), newrdn.encode(self.charset)) + cursor = self._cursor() + return cursor.connection.rename_s(dn.encode(self.charset), newrdn.encode(self.charset)) def search_s(self, base, scope, filterstr, attrlist): - results = self.connection.search_s(base, scope, filterstr.encode(self.charset), attrlist) + cursor = self._cursor() + results = cursor.connection.search_s(base, scope, filterstr.encode(self.charset), attrlist) output = [] for dn, attrs in results: for field in attrs: @@ -91,7 +107,5 @@ class LdapConnection(object): return output # FIXME: is this the right place to initialize the LDAP connection? -connection = LdapConnection(settings.LDAPDB_SERVER_URI, - settings.LDAPDB_BIND_DN, - settings.LDAPDB_BIND_PASSWORD) +connection = LdapConnection() diff --git a/ldapdb/models/query.py b/ldapdb/models/query.py index f5c4020..069c237 100644 --- a/ldapdb/models/query.py +++ b/ldapdb/models/query.py @@ -75,11 +75,11 @@ class WhereNode(BaseWhereNode): obj = Constraint(obj.alias, obj.col, obj.field) super(WhereNode, self).add((obj, lookup_type, value), connector) - def as_sql(self, qn=None): + def as_sql(self, qn=None, connection=None): bits = [] for item in self.children: - if isinstance(item, WhereNode): - sql, params = item.as_sql() + if hasattr(item, 'as_sql'): + sql, params = item.as_sql(qn=qn, connection=connection) bits.append(sql) continue -- 2.30.2