1
+ import contextlib
2
+
1
3
from django .db .backends .base .base import BaseDatabaseWrapper
2
- from django .db .backends .signals import connection_created
3
4
from pymongo .collection import Collection
4
5
from pymongo .mongo_client import MongoClient
5
6
@@ -128,11 +129,6 @@ def _isnull_operator(a, b):
128
129
introspection_class = DatabaseIntrospection
129
130
ops_class = DatabaseOperations
130
131
131
- def __init__ (self , * args , ** kwargs ):
132
- super ().__init__ (* args , ** kwargs )
133
- self .connected = False
134
- del self .connection
135
-
136
132
def get_collection (self , name , ** kwargs ):
137
133
collection = Collection (self .database , name , ** kwargs )
138
134
if self .queries_logged :
@@ -145,44 +141,45 @@ def get_database(self):
145
141
return self .database
146
142
147
143
def __getattr__ (self , attr ):
148
- """
149
- Connect to the database the first time `connection` or `database` are
150
- accessed.
151
- """
152
- if attr in ["connection" , "database" ]:
153
- assert not self .connected
154
- self ._connect ()
144
+ """Connect to the database the first time `database` is accessed."""
145
+ if attr == "database" :
146
+ if self .connection is None :
147
+ self .connect ()
155
148
return getattr (self , attr )
156
149
raise AttributeError (attr )
157
150
158
- def _connect (self ):
159
- settings_dict = self .settings_dict
160
- self .connection = MongoClient (
161
- host = settings_dict ["HOST" ] or None ,
162
- port = int (settings_dict ["PORT" ] or 27017 ),
163
- username = settings_dict .get ("USER" ),
164
- password = settings_dict .get ("PASSWORD" ),
165
- ** settings_dict ["OPTIONS" ],
166
- )
167
- db_name = settings_dict ["NAME" ]
151
+ def init_connection_state (self ):
152
+ db_name = self .settings_dict ["NAME" ]
168
153
if db_name :
169
154
self .database = self .connection [db_name ]
155
+ super ().init_connection_state ()
156
+
157
+ def get_connection_params (self ):
158
+ settings_dict = self .settings_dict
159
+ return {
160
+ "host" : settings_dict ["HOST" ] or None ,
161
+ "port" : int (settings_dict ["PORT" ] or 27017 ),
162
+ "username" : settings_dict .get ("USER" ),
163
+ "password" : settings_dict .get ("PASSWORD" ),
164
+ ** settings_dict ["OPTIONS" ],
165
+ }
170
166
171
- self . connected = True
172
- connection_created . send ( sender = self . __class__ , connection = self )
167
+ def get_new_connection ( self , conn_params ):
168
+ return MongoClient ( ** conn_params )
173
169
174
170
def _commit (self ):
175
171
pass
176
172
177
173
def _rollback (self ):
178
174
pass
179
175
176
+ def set_autocommit (self , autocommit , force_begin_transaction_with_broken_autocommit = False ):
177
+ pass
178
+
180
179
def close (self ):
181
- if self .connected :
182
- self .connection .close ()
183
- del self .connection
180
+ super ().close ()
181
+ with contextlib .suppress (AttributeError ):
184
182
del self .database
185
- self .connected = False
186
183
187
184
def cursor (self ):
188
185
return Cursor ()
0 commit comments