1 # -*- coding: utf-8 -*-
4 # Copyright (c) 2009-2010, Bolloré telecom
7 # See AUTHORS file for a full list of contributors.
9 # Redistribution and use in source and binary forms, with or without modification,
10 # are permitted provided that the following conditions are met:
12 # 1. Redistributions of source code must retain the above copyright notice,
13 # this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in the
17 # documentation and/or other materials provided with the distribution.
19 # 3. Neither the name of Bolloré telecom nor the names of its contributors
20 # may be used to endorse or promote products derived from this software
21 # without specific prior written permission.
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
27 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 from django.db.backends import BaseDatabaseFeatures, BaseDatabaseOperations, BaseDatabaseWrapper
38 from django.db.backends.creation import BaseDatabaseCreation
40 class DatabaseCreation(BaseDatabaseCreation):
41 def create_test_db(self, verbosity=1, autoclobber=False):
43 Creates a test database, prompting the user for confirmation if the
44 database already exists. Returns the name of the test database created.
48 def destroy_test_db(self, old_database_name, verbosity=1):
50 Destroy a test database, prompting the user for confirmation if the
51 database already exists. Returns the name of the test database created.
55 class DatabaseCursor(object):
56 def __init__(self, ldap_connection):
57 self.connection = ldap_connection
59 class DatabaseFeatures(BaseDatabaseFeatures):
60 def __init__(self, connection):
61 self.connection = connection
63 class DatabaseOperations(BaseDatabaseOperations):
64 compiler_module = "ldapdb.backends.ldap.compiler"
66 def quote_name(self, name):
69 class DatabaseWrapper(BaseDatabaseWrapper):
70 def __init__(self, *args, **kwargs):
71 super(DatabaseWrapper, self).__init__(*args, **kwargs)
73 self.charset = "utf-8"
74 self.creation = DatabaseCreation(self)
75 self.features = DatabaseFeatures(self)
76 self.ops = DatabaseOperations()
85 if self.connection is None:
86 self.connection = ldap.initialize(self.settings_dict['NAME'])
87 self.connection.simple_bind_s(
88 self.settings_dict['USER'],
89 self.settings_dict['PASSWORD'])
90 return DatabaseCursor(self.connection)
95 def add_s(self, dn, modlist):
96 cursor = self._cursor()
97 return cursor.connection.add_s(dn.encode(self.charset), modlist)
99 def delete_s(self, dn):
100 cursor = self._cursor()
101 return cursor.connection.delete_s(dn.encode(self.charset))
103 def modify_s(self, dn, modlist):
104 cursor = self._cursor()
105 return cursor.connection.modify_s(dn.encode(self.charset), modlist)
107 def rename_s(self, dn, newrdn):
108 cursor = self._cursor()
109 return cursor.connection.rename_s(dn.encode(self.charset), newrdn.encode(self.charset))
111 def search_s(self, base, scope, filterstr, attrlist):
112 cursor = self._cursor()
113 results = cursor.connection.search_s(base, scope, filterstr.encode(self.charset), attrlist)
115 for dn, attrs in results:
116 output.append((dn.decode(self.charset), attrs))