6
6
from django .core .exceptions import ImproperlyConfigured
7
7
from django .db .backends .utils import logger
8
8
from django .utils .version import get_version_tuple
9
- from pymongo .cursor import Cursor
10
9
11
10
12
11
def check_django_compatability ():
@@ -41,12 +40,11 @@ def profile_call(self, func, args=(), kwargs=None):
41
40
return duration , retval
42
41
43
42
def log (self , op , duration , args , kwargs = None ):
43
+ # If kwargs are used by any operations in the future, they must be
44
+ # added to this logging.
44
45
msg = "(%.3f) %s"
45
46
args = ", " .join (str (arg ) for arg in args )
46
47
operation = f"{ self .collection .name } .{ op } ({ args } )"
47
- kwargs = {k : v for k , v in kwargs .items () if v }
48
- if kwargs :
49
- operation += f"; kwargs={ kwargs } "
50
48
if len (settings .DATABASES ) > 1 :
51
49
msg += f"; alias={ self .db .alias } "
52
50
self .db .queries_log .append (
@@ -62,19 +60,15 @@ def log(self, op, duration, args, kwargs=None):
62
60
extra = {
63
61
"duration" : duration ,
64
62
"sql" : operation ,
65
- "kwargs" : kwargs ,
66
63
"alias" : self .db .alias ,
67
64
},
68
65
)
69
66
70
- def find (self , * args , ** kwargs ):
71
- return DebugCursor (self , self .collection , * args , ** kwargs )
72
-
73
67
def logging_wrapper (method ):
74
68
def wrapper (self , * args , ** kwargs ):
75
69
func = getattr (self .collection , method )
76
- # Collection.insert_one () mutates args[0] (the document ) by adding
77
- # the _id. deepcopy() to avoid logging that version.
70
+ # Collection.insert_many () mutates args (the documents ) by adding
71
+ # _id. deepcopy() to avoid logging that version.
78
72
original_args = copy .deepcopy (args )
79
73
duration , retval = self .profile_call (func , args , kwargs )
80
74
self .log (method , duration , original_args , kwargs )
@@ -84,30 +78,8 @@ def wrapper(self, *args, **kwargs):
84
78
85
79
# These are the operations that this backend uses.
86
80
aggregate = logging_wrapper ("aggregate" )
87
- count_documents = logging_wrapper ("count_documents" )
88
81
insert_many = logging_wrapper ("insert_many" )
89
82
delete_many = logging_wrapper ("delete_many" )
90
83
update_many = logging_wrapper ("update_many" )
91
84
92
85
del logging_wrapper
93
-
94
-
95
- class DebugCursor (Cursor ):
96
- def __init__ (self , collection_wrapper , * args , ** kwargs ):
97
- self .collection_wrapper = collection_wrapper
98
- super ().__init__ (* args , ** kwargs )
99
-
100
- def _refresh (self ):
101
- super_method = super ()._refresh
102
- if self ._Cursor__id is not None :
103
- return super_method ()
104
- # self.__id is None: first time the .find() iterator is
105
- # entered. find() profiling happens here.
106
- duration , retval = self .collection_wrapper .profile_call (super_method )
107
- kwargs = {
108
- "limit" : self ._Cursor__limit ,
109
- "skip" : self ._Cursor__skip ,
110
- "sort" : self ._Cursor__ordering ,
111
- }
112
- self .collection_wrapper .log ("find" , duration , [self ._Cursor__spec ], kwargs )
113
- return retval
0 commit comments