Skip to content

Commit 64d928f

Browse files
authored
Merge branch 'main' into javascript-load-panel-event
2 parents e766af7 + 5a3ccd9 commit 64d928f

File tree

26 files changed

+183
-76
lines changed

26 files changed

+183
-76
lines changed

.github/workflows/test.yml

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ jobs:
1515
mariadb:
1616
image: mariadb:10.3
1717
env:
18-
MYSQL_ROOT_PASSWORD: mysql
19-
MYSQL_DATABASE: mysql
18+
MYSQL_ROOT_PASSWORD: debug_toolbar
2019
options: >-
2120
--health-cmd "mysqladmin ping"
2221
--health-interval 10s
@@ -61,11 +60,10 @@ jobs:
6160
run: tox
6261
env:
6362
DB_BACKEND: mysql
64-
DB_NAME: mysql
6563
DB_USER: root
66-
DB_PASSWORD: mysql
67-
DB_HOST: "127.0.0.1"
68-
DB_PORT: "3306"
64+
DB_PASSWORD: debug_toolbar
65+
DB_HOST: 127.0.0.1
66+
DB_PORT: 3306
6967

7068
- name: Upload coverage
7169
uses: codecov/codecov-action@v1
@@ -84,7 +82,9 @@ jobs:
8482
postgres:
8583
image: 'postgres:9.5'
8684
env:
87-
POSTGRES_PASSWORD: postgres
85+
POSTGRES_DB: debug_toolbar
86+
POSTGRES_USER: debug_toolbar
87+
POSTGRES_PASSWORD: debug_toolbar
8888
ports:
8989
- 5432:5432
9090
options: >-
@@ -129,9 +129,6 @@ jobs:
129129
run: tox
130130
env:
131131
DB_BACKEND: postgresql
132-
DB_NAME: postgres
133-
DB_USER: postgres
134-
DB_PASSWORD: postgres
135132
DB_HOST: localhost
136133
DB_PORT: 5432
137134

.tx/config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
host = https://www.transifex.com
33
lang_map = sr@latin:sr_Latn
44

5-
[django-debug-toolbar.master]
5+
[django-debug-toolbar.main]
66
file_filter = debug_toolbar/locale/<lang>/LC_MESSAGES/django.po
77
source_file = debug_toolbar/locale/en/LC_MESSAGES/django.po
88
source_lang = en

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ style: package-lock.json
88
flake8
99
npx eslint --ignore-path .gitignore --fix .
1010
npx prettier --ignore-path .gitignore --write $(PRETTIER_TARGETS)
11+
! grep -r '\(style=\|onclick=\|<script>\|<style\)' debug_toolbar/templates/
1112

1213
style_check: package-lock.json
1314
isort -c .
1415
black --target-version=py36 --check .
1516
flake8
1617
npx eslint --ignore-path .gitignore .
1718
npx prettier --ignore-path .gitignore --check $(PRETTIER_TARGETS)
19+
! grep -r '\(style=\|onclick=\|<script>\|<style\)' debug_toolbar/templates/
1820

1921
example:
2022
python example/manage.py migrate --noinput

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Django Debug Toolbar
1010
:target: https://github.com/jazzband/django-debug-toolbar/actions
1111
:alt: Build Status
1212

13-
.. image:: https://codecov.io/gh/jazzband/django-debug-toolbar/branch/master/graph/badge.svg
13+
.. image:: https://codecov.io/gh/jazzband/django-debug-toolbar/branch/main/graph/badge.svg
1414
:target: https://codecov.io/gh/jazzband/django-debug-toolbar
1515
:alt: Test coverage status
1616

@@ -28,7 +28,7 @@ more details about the panel's content.
2828

2929
Here's a screenshot of the toolbar in action:
3030

31-
.. image:: https://raw.github.com/jazzband/django-debug-toolbar/master/example/django-debug-toolbar.png
31+
.. image:: https://raw.github.com/jazzband/django-debug-toolbar/main/example/django-debug-toolbar.png
3232
:alt: Django Debug Toolbar screenshot
3333

3434
In addition to the built-in panels, a number of third-party panels are

debug_toolbar/__init__.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
__all__ = ["VERSION"]
22

33

4-
try:
5-
import pkg_resources
6-
7-
VERSION = pkg_resources.get_distribution("django-debug-toolbar").version
8-
except Exception:
9-
VERSION = "unknown"
10-
4+
# Do not use pkg_resources to find the version but set it here directly!
5+
# see issue #1446
6+
VERSION = "3.2"
117

128
# Code that discovers files or modules in INSTALLED_APPS imports this module.
139

debug_toolbar/middleware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def __init__(self, get_response):
4444
def __call__(self, request):
4545
# Decide whether the toolbar is active for this request.
4646
show_toolbar = get_show_toolbar()
47-
if not show_toolbar(request) or request.path.startswith("/__debug__/"):
47+
if not show_toolbar(request) or DebugToolbar.is_toolbar_request(request):
4848
return self.get_response(request)
4949

5050
toolbar = DebugToolbar(request, self.get_response)

debug_toolbar/panels/cache.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@
33
import time
44
from collections import OrderedDict
55

6+
try:
7+
from django.utils.connection import ConnectionProxy
8+
except ImportError:
9+
ConnectionProxy = None
10+
611
from django.conf import settings
712
from django.core import cache
8-
from django.core.cache import CacheHandler, caches as original_caches
13+
from django.core.cache import (
14+
DEFAULT_CACHE_ALIAS,
15+
CacheHandler,
16+
cache as original_cache,
17+
caches as original_caches,
18+
)
919
from django.core.cache.backends.base import BaseCache
1020
from django.dispatch import Signal
1121
from django.middleware import cache as middleware_cache
@@ -246,8 +256,13 @@ def enable_instrumentation(self):
246256
else:
247257
cache.caches = CacheHandlerPatch()
248258

259+
# Wrap the patched cache inside Django's ConnectionProxy
260+
if ConnectionProxy:
261+
cache.cache = ConnectionProxy(cache.caches, DEFAULT_CACHE_ALIAS)
262+
249263
def disable_instrumentation(self):
250264
cache.caches = original_caches
265+
cache.cache = original_cache
251266
# While it can be restored to the original, any views that were
252267
# wrapped with the cache_page decorator will continue to use a
253268
# monkey patched cache.

debug_toolbar/panels/request.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,16 @@ def generate_stats(self, request, response):
4444
view_info["view_func"] = get_name_from_obj(func)
4545
view_info["view_args"] = args
4646
view_info["view_kwargs"] = kwargs
47-
view_info["view_urlname"] = getattr(match, "url_name", _("<unavailable>"))
47+
48+
if getattr(match, "url_name", False):
49+
url_name = match.url_name
50+
if match.namespaces:
51+
url_name = ":".join([*match.namespaces, url_name])
52+
else:
53+
url_name = _("<unavailable>")
54+
55+
view_info["view_urlname"] = url_name
56+
4857
except Http404:
4958
pass
5059
self.record_stats(view_info)

debug_toolbar/static/debug_toolbar/css/toolbar.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,9 @@
601601
#djDebug .djdt-width-60 {
602602
width: 60%;
603603
}
604+
#djDebug .djdt-max-height-100 {
605+
max-height: 100%;
606+
}
604607
#djDebug .djdt-highlighted {
605608
background-color: lightgrey;
606609
}

debug_toolbar/static/debug_toolbar/js/toolbar.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const djdt = {
4545
inner.previousElementSibling.remove(); // Remove AJAX loader
4646
inner.innerHTML = data.content;
4747
$$.executeScripts(data.scripts);
48+
$$.applyStyles(inner);
4849
djDebug.dispatchEvent(
4950
new CustomEvent("djdt.panel.render", {
5051
detail: { panelId: panelId },

debug_toolbar/static/debug_toolbar/js/utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,26 @@ const $$ = {
4747
document.head.appendChild(el);
4848
});
4949
},
50+
applyStyles(container) {
51+
/*
52+
* Given a container element, apply styles set via data-djdt-styles attribute.
53+
* The format is data-djdt-styles="styleName1:value;styleName2:value2"
54+
* The style names should use the CSSStyleDeclaration camel cased names.
55+
*/
56+
container
57+
.querySelectorAll("[data-djdt-styles]")
58+
.forEach(function (element) {
59+
const styles = element.dataset.djdtStyles || "";
60+
styles.split(";").forEach(function (styleText) {
61+
const styleKeyPair = styleText.split(":");
62+
if (styleKeyPair.length === 2) {
63+
const name = styleKeyPair[0].trim();
64+
const value = styleKeyPair[1].trim();
65+
element.style[name] = value;
66+
}
67+
});
68+
});
69+
},
5070
};
5171

5272
function ajax(url, init) {

debug_toolbar/templates/debug_toolbar/panels/history.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{{ refresh_form }}
44
<button class="refreshHistory">Refresh</button>
55
</form>
6-
<table style="max-height:100%;">
6+
<table class="djdt-max-height-100">
77
<thead>
88
<tr>
99
<th>{% trans "Time" %}</th>

debug_toolbar/templates/debug_toolbar/panels/profiling.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
{% for call in func_list %}
1515
<tr class="{% for parent_id in call.parent_ids %} djToggleDetails_{{ parent_id }}{% endfor %}" id="profilingMain_{{ call.id }}">
1616
<td>
17-
<div style="padding-left:{{ call.indent }}px">
17+
<div data-djdt-styles="paddingLeft:{{ call.indent }}px">
1818
{% if call.has_subfuncs %}
1919
<button type="button" class="djProfileToggleDetails djToggleSwitch" data-toggle-name="profilingMain" data-toggle-id="{{ call.id }}">-</button>
2020
{% else %}

debug_toolbar/templates/debug_toolbar/panels/sql.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<ul>
33
{% for alias, info in databases %}
44
<li>
5-
<strong><span class="djdt-color" style="background-color:rgb({{ info.rgb_color|join:', ' }})"></span> {{ alias }}</strong>
5+
<strong><span class="djdt-color" data-djdt-styles="backgroundColor:rgb({{ info.rgb_color|join:', ' }})"></span> {{ alias }}</strong>
66
{{ info.time_spent|floatformat:"2" }} ms ({% blocktrans count info.num_queries as num %}{{ num }} query{% plural %}{{ num }} queries{% endblocktrans %}
77
{% if info.similar_count %}
88
{% blocktrans with count=info.similar_count trimmed %}
@@ -40,21 +40,21 @@
4040
<tbody>
4141
{% for query in queries %}
4242
<tr class="{% if query.is_slow %} djDebugRowWarning{% endif %}" id="sqlMain_{{ forloop.counter }}">
43-
<td><span class="djdt-color" style="background-color:rgb({{ query.rgb_color|join:', '}})"></span></td>
43+
<td><span class="djdt-color" data-djdt-styles="backgroundColor:rgb({{ query.rgb_color|join:', '}})"></span></td>
4444
<td class="djdt-toggle">
4545
<button type="button" class="djToggleSwitch" data-toggle-name="sqlMain" data-toggle-id="{{ forloop.counter }}">+</button>
4646
</td>
4747
<td>
4848
<div class="djDebugSql">{{ query.sql|safe }}</div>
4949
{% if query.similar_count %}
5050
<strong>
51-
<span class="djdt-color" style="background-color:{{ query.similar_color }}"></span>
51+
<span class="djdt-color" data-djdt-styles="backgroundColor:{{ query.similar_color }}"></span>
5252
{% blocktrans with count=query.similar_count %}{{ count }} similar queries.{% endblocktrans %}
5353
</strong>
5454
{% endif %}
5555
{% if query.duplicate_count %}
5656
<strong>
57-
<span class="djdt-color" style="background-color:{{ query.duplicate_color }}"></span>
57+
<span class="djdt-color" data-djdt-styles="backgroundColor:{{ query.duplicate_color }}"></span>
5858
{% blocktrans with dupes=query.duplicate_count %}Duplicated {{ dupes }} times.{% endblocktrans %}
5959
</strong>
6060
{% endif %}

debug_toolbar/toolbar.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
from django.core.exceptions import ImproperlyConfigured
1010
from django.template import TemplateSyntaxError
1111
from django.template.loader import render_to_string
12-
from django.urls import path
12+
from django.urls import path, resolve
13+
from django.urls.exceptions import Resolver404
1314
from django.utils.module_loading import import_string
1415

1516
from debug_toolbar import settings as dt_settings
@@ -133,6 +134,19 @@ def get_urls(cls):
133134
cls._urlpatterns = urlpatterns
134135
return cls._urlpatterns
135136

137+
@classmethod
138+
def is_toolbar_request(cls, request):
139+
"""
140+
Determine if the request is for a DebugToolbar view.
141+
"""
142+
# The primary caller of this function is in the middleware which may
143+
# not have resolver_match set.
144+
try:
145+
resolver_match = request.resolver_match or resolve(request.path)
146+
except Resolver404:
147+
return False
148+
return resolver_match.namespaces and resolver_match.namespaces[-1] == app_name
149+
136150

137151
app_name = "djdt"
138152
urlpatterns = DebugToolbar.get_urls()

docs/changes.rst

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ Next version
99
* Added ``PRETTIFY_SQL`` configuration option to support controlling
1010
SQL token grouping. By default it's set to True. When set to False,
1111
a performance improvement can be seen by the SQL panel.
12-
* Support JavaScript event when a panel loads of the format
13-
``djdt.panel.[PanelId]`` where PanelId is the panel's python class'
14-
``panel_id`` property. Listening for this event corrects the bug
15-
in the Timer Panel in which it doesn't insert the browser timings
16-
after being switching requests in the History Panel.
17-
12+
* Added a JavaScript event when a panel loads of the format
13+
``djdt.panel.[PanelId]`` where PanelId is the ``panel_id`` property
14+
of the panel's Python class. Listening for this event corrects the bug
15+
in the Timer Panel in which it didn't insert the browser timings
16+
after switching requests in the History Panel.
17+
* Fixed issue with the toolbar expecting URL paths to start with
18+
``/__debug__/`` while the documentation indicates it's not required.
1819

1920
3.2 (2020-12-03)
2021
----------------

docs/contributing.rst

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Tests
5353

5454
Once you've set up a development environment as explained above, you can run
5555
the test suite for the versions of Django and Python installed in that
56-
environment::
56+
environment using the SQLite database::
5757

5858
$ make test
5959

@@ -79,8 +79,23 @@ or by setting the ``DJANGO_SELENIUM_TESTS`` environment variable::
7979
$ DJANGO_SELENIUM_TESTS=true make coverage
8080
$ DJANGO_SELENIUM_TESTS=true tox
8181

82-
At this time, there isn't an easy way to test against databases other than
83-
SQLite.
82+
To test via `tox` against other databases, you'll need to create the user,
83+
database and assign the proper permissions. For PostgreSQL in a `psql`
84+
shell (note this allows the debug_toolbar user the permission to create
85+
databases)::
86+
87+
psql> CREATE USER debug_toolbar WITH PASSWORD 'debug_toolbar';
88+
psql> ALTER USER debug_toolbar CREATEDB;
89+
psql> CREATE DATABASE debug_toolbar;
90+
psql> GRANT ALL PRIVILEGES ON DATABASE debug_toolbar to debug_toolbar;
91+
92+
For MySQL/MariaDB in a `mysql` shell::
93+
94+
mysql> CREATE DATABASE debug_toolbar;
95+
mysql> CREATE USER 'debug_toolbar'@'localhost' IDENTIFIED BY 'debug_toolbar';
96+
mysql> GRANT ALL PRIVILEGES ON debug_toolbar.* TO 'debug_toolbar'@'localhost';
97+
mysql> GRANT ALL PRIVILEGES ON test_debug_toolbar.* TO 'debug_toolbar'@'localhost';
98+
8499

85100
Style
86101
-----
@@ -141,8 +156,8 @@ The release itself requires the following steps:
141156
Commit.
142157

143158
#. Bump version numbers in ``docs/changes.rst``, ``docs/conf.py``,
144-
``README.rst`` and ``setup.py``. Add the release date to
145-
``docs/changes.rst``. Commit.
159+
``README.rst``, ``debug_toolbar/__init__.py`` and ``setup.py``.
160+
Add the release date to ``docs/changes.rst``. Commit.
146161

147162
#. Tag the new version.
148163

example/settings.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,26 +73,23 @@
7373

7474
# To use another database, set the DB_BACKEND environment variable.
7575
if os.environ.get("DB_BACKEND", "").lower() == "postgresql":
76-
# % su postgres
77-
# % createuser debug_toolbar
78-
# % createdb debug_toolbar -O debug_toolbar
76+
# See docs/contributing for instructions on configuring PostgreSQL.
7977
DATABASES = {
8078
"default": {
8179
"ENGINE": "django.db.backends.postgresql",
8280
"NAME": "debug_toolbar",
8381
"USER": "debug_toolbar",
82+
"PASSWORD": "debug_toolbar",
8483
}
8584
}
8685
if os.environ.get("DB_BACKEND", "").lower() == "mysql":
87-
# % mysql
88-
# mysql> CREATE DATABASE debug_toolbar;
89-
# mysql> CREATE USER 'debug_toolbar'@'localhost';
90-
# mysql> GRANT ALL PRIVILEGES ON debug_toolbar.* TO 'debug_toolbar'@'localhost';
86+
# See docs/contributing for instructions on configuring MySQL/MariaDB.
9187
DATABASES = {
9288
"default": {
9389
"ENGINE": "django.db.backends.mysql",
9490
"NAME": "debug_toolbar",
9591
"USER": "debug_toolbar",
92+
"PASSWORD": "debug_toolbar",
9693
}
9794
}
9895

0 commit comments

Comments
 (0)