Skip to content

Fix missing query-actions buttons for queries with date/datetime params. #1019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions debug_toolbar/panels/sql/tracking.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import, unicode_literals

import datetime
import json
from threading import local
from time import time
Expand Down Expand Up @@ -95,8 +96,10 @@ def _quote_params(self, params):
return [self._quote_expr(p) for p in params]

def _decode(self, param):
# make sure datetime, date and time are converted to string by force_text
CONVERT_TYPES = (datetime.datetime, datetime.date, datetime.time)
try:
return force_text(param, strings_only=True)
return force_text(param, strings_only=not isinstance(param, CONVERT_TYPES))
except UnicodeDecodeError:
return '(encoded string)'

Expand All @@ -114,7 +117,7 @@ def _record(self, method, sql, params):
_params = ''
try:
_params = json.dumps([self._decode(p) for p in params])
except Exception:
except TypeError:
pass # object not JSON serializable

template_info = get_template_info()
Expand Down
31 changes: 31 additions & 0 deletions tests/panels/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

from __future__ import absolute_import, unicode_literals

import datetime
import unittest

from django.contrib.auth.models import User
from django.db import connection
from django.db.models import Count
from django.db.utils import DatabaseError
from django.shortcuts import render
from django.test.utils import override_settings
Expand Down Expand Up @@ -69,6 +71,35 @@ def test_non_ascii_query(self):
# ensure the panel renders correctly
self.assertIn('café', self.panel.content)

def test_param_conversion(self):
self.assertEqual(len(self.panel._queries), 0)

list(
User.objects
.filter(first_name='Foo')
.filter(is_staff=True)
.filter(is_superuser=False)
)
list(
User.objects
.annotate(group_count=Count('groups__id'))
.filter(group_count__lt=10)
.filter(group_count__gt=1)
)
list(User.objects.filter(date_joined=datetime.datetime(2017, 12, 22, 16, 7, 1)))

self.panel.process_response(self.request, self.response)
self.panel.generate_stats(self.request, self.response)

# ensure query was logged
self.assertEqual(len(self.panel._queries), 3)

self.assertEqual(tuple([q[1]['params'] for q in self.panel._queries]), (
'["Foo", true, false]',
'[10, 1]',
'["2017-12-22 16:07:01"]'
))

def test_insert_content(self):
"""
Test that the panel only inserts content after generate_stats and
Expand Down