123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- import ldap
- import django
- from django.db.backends import BaseDatabaseFeatures, BaseDatabaseOperations, BaseDatabaseWrapper
- from django.db.backends.creation import BaseDatabaseCreation
- class DatabaseCreation(BaseDatabaseCreation):
- def create_test_db(self, verbosity=1, autoclobber=False):
- """
- Creates a test database, prompting the user for confirmation if the
- database already exists. Returns the name of the test database created.
- """
- pass
- def destroy_test_db(self, old_database_name, verbosity=1):
- """
- Destroy a test database, prompting the user for confirmation if the
- database already exists. Returns the name of the test database created.
- """
- pass
- class DatabaseCursor(object):
- def __init__(self, ldap_connection):
- self.connection = ldap_connection
- class DatabaseFeatures(BaseDatabaseFeatures):
- def __init__(self, connection):
- self.connection = connection
- class DatabaseOperations(BaseDatabaseOperations):
- compiler_module = "ldapdb.backends.ldap.compiler"
- def quote_name(self, name):
- return name
- class DatabaseWrapper(BaseDatabaseWrapper):
- def __init__(self, *args, **kwargs):
- super(DatabaseWrapper, self).__init__(*args, **kwargs)
- self.charset = "utf-8"
- self.creation = DatabaseCreation(self)
- self.features = DatabaseFeatures(self)
- if django.VERSION > (1, 4):
- self.ops = DatabaseOperations(self)
- else:
- self.ops = DatabaseOperations()
- self.settings_dict['SUPPORTS_TRANSACTIONS'] = False
- def close(self):
- pass
- def _commit(self):
- pass
- def _cursor(self):
- if self.connection is None:
- self.connection = ldap.initialize(self.settings_dict['NAME'])
- self.connection.simple_bind_s(
- self.settings_dict['USER'],
- self.settings_dict['PASSWORD'])
- return DatabaseCursor(self.connection)
- def _rollback(self):
- pass
- def add_s(self, dn, modlist):
- cursor = self._cursor()
- return cursor.connection.add_s(dn.encode(self.charset), modlist)
- def delete_s(self, dn):
- cursor = self._cursor()
- return cursor.connection.delete_s(dn.encode(self.charset))
- def modify_s(self, dn, modlist):
- cursor = self._cursor()
- return cursor.connection.modify_s(dn.encode(self.charset), modlist)
- def rename_s(self, dn, newrdn):
- cursor = self._cursor()
- return cursor.connection.rename_s(dn.encode(self.charset), newrdn.encode(self.charset))
- def search_s(self, base, scope, filterstr='(objectClass=*)',attrlist=None):
- cursor = self._cursor()
- results = cursor.connection.search_s(base, scope, filterstr.encode(self.charset), attrlist)
- output = []
- for dn, attrs in results:
- output.append((dn.decode(self.charset), attrs))
- print filterstr
- return output
|