2016-09-18 02:29:00 +03:00
|
|
|
from django.db import connections
|
|
|
|
|
2016-09-18 03:09:56 +03:00
|
|
|
from promise import Promise
|
|
|
|
|
2016-09-18 02:29:00 +03:00
|
|
|
from .sql.tracking import unwrap_cursor, wrap_cursor
|
|
|
|
from .types import DjangoDebug
|
|
|
|
|
|
|
|
|
|
|
|
class DjangoDebugContext(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.debug_promise = None
|
|
|
|
self.promises = []
|
|
|
|
self.enable_instrumentation()
|
|
|
|
self.object = DjangoDebug(sql=[])
|
|
|
|
|
|
|
|
def get_debug_promise(self):
|
|
|
|
if not self.debug_promise:
|
|
|
|
self.debug_promise = Promise.all(self.promises)
|
|
|
|
return self.debug_promise.then(self.on_resolve_all_promises)
|
|
|
|
|
|
|
|
def on_resolve_all_promises(self, values):
|
|
|
|
self.disable_instrumentation()
|
|
|
|
return self.object
|
|
|
|
|
|
|
|
def add_promise(self, promise):
|
|
|
|
if self.debug_promise and not self.debug_promise.is_fulfilled:
|
|
|
|
self.promises.append(promise)
|
|
|
|
|
|
|
|
def enable_instrumentation(self):
|
|
|
|
# This is thread-safe because database connections are thread-local.
|
|
|
|
for connection in connections.all():
|
|
|
|
wrap_cursor(connection, self)
|
|
|
|
|
|
|
|
def disable_instrumentation(self):
|
|
|
|
for connection in connections.all():
|
|
|
|
unwrap_cursor(connection)
|
|
|
|
|
|
|
|
|
|
|
|
class DjangoDebugMiddleware(object):
|
2017-07-28 19:43:27 +03:00
|
|
|
def resolve(self, next, root, info, **args):
|
|
|
|
context = info.context
|
2018-07-20 02:51:33 +03:00
|
|
|
django_debug = getattr(context, "django_debug", None)
|
2016-09-18 02:29:00 +03:00
|
|
|
if not django_debug:
|
|
|
|
if context is None:
|
2018-07-20 02:51:33 +03:00
|
|
|
raise Exception("DjangoDebug cannot be executed in None contexts")
|
2016-09-18 02:29:00 +03:00
|
|
|
try:
|
|
|
|
context.django_debug = DjangoDebugContext()
|
|
|
|
except Exception:
|
2018-07-20 02:51:33 +03:00
|
|
|
raise Exception(
|
|
|
|
"DjangoDebug need the context to be writable, context received: {}.".format(
|
|
|
|
context.__class__.__name__
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if info.schema.get_type("DjangoDebug") == info.return_type:
|
2016-09-18 02:29:00 +03:00
|
|
|
return context.django_debug.get_debug_promise()
|
2017-07-28 19:43:27 +03:00
|
|
|
promise = next(root, info, **args)
|
2016-09-18 02:29:00 +03:00
|
|
|
context.django_debug.add_promise(promise)
|
|
|
|
return promise
|