From 4cadf088d8b4c48fc056222d46d4d9e7e25bcae9 Mon Sep 17 00:00:00 2001 From: Tim Schilling Date: Fri, 22 Jan 2021 12:09:18 -0600 Subject: [PATCH 1/3] Correct RENDER_PANELS functionality and when enabled disable HistoryPanel. The render panels setting used to control whether the toolbar would keep the contents of the panels in the background or render the toolbar on every page. The history panel relies on loading panels in the background. If panels should be rendered on the request, then the history panel is can't/shouldn't work. --- debug_toolbar/panels/history/panel.py | 6 ++++ .../templates/debug_toolbar/base.html | 5 ++- .../debug_toolbar/includes/panel_content.html | 6 ++-- debug_toolbar/toolbar.py | 4 +++ docs/changes.rst | 4 +++ docs/configuration.rst | 2 ++ docs/installation.rst | 8 +++++ docs/panels.rst | 9 +++-- tests/test_integration.py | 35 ++++++++++++++++++- 9 files changed, 72 insertions(+), 7 deletions(-) diff --git a/debug_toolbar/panels/history/panel.py b/debug_toolbar/panels/history/panel.py index e80d8c93a..393d91d4f 100644 --- a/debug_toolbar/panels/history/panel.py +++ b/debug_toolbar/panels/history/panel.py @@ -20,6 +20,12 @@ class HistoryPanel(Panel): nav_title = _("History") template = "debug_toolbar/panels/history.html" + @property + def enabled(self): + # Do not show the history panel if the panels are rendered on request + # rather than loaded via ajax. + return super().enabled and not self.toolbar.should_render_panels() + @property def is_historical(self): """The HistoryPanel should not be included in the historical panels.""" diff --git a/debug_toolbar/templates/debug_toolbar/base.html b/debug_toolbar/templates/debug_toolbar/base.html index 78b9b7fe2..7abc5476f 100644 --- a/debug_toolbar/templates/debug_toolbar/base.html +++ b/debug_toolbar/templates/debug_toolbar/base.html @@ -7,7 +7,10 @@ {% endblock %}
diff --git a/debug_toolbar/templates/debug_toolbar/includes/panel_content.html b/debug_toolbar/templates/debug_toolbar/includes/panel_content.html index 2c1a1b195..8c2e446b1 100644 --- a/debug_toolbar/templates/debug_toolbar/includes/panel_content.html +++ b/debug_toolbar/templates/debug_toolbar/includes/panel_content.html @@ -7,11 +7,11 @@

{{ panel.title }}

- {% if toolbar.store_id %} + {% if toolbar.should_render_panels %} +
{{ panel.content }}
+ {% else %}
- {% else %} -
{{ panel.content }}
{% endif %}
diff --git a/debug_toolbar/toolbar.py b/debug_toolbar/toolbar.py index 9638ba1f8..3d8937126 100644 --- a/debug_toolbar/toolbar.py +++ b/debug_toolbar/toolbar.py @@ -78,6 +78,10 @@ def render_toolbar(self): raise def should_render_panels(self): + """Determine whether the panels should be rendered during the request + + If False, the panels will be loaded via Ajax. + """ render_panels = self.config["RENDER_PANELS"] if render_panels is None: render_panels = self.request.META["wsgi.multiprocess"] diff --git a/docs/changes.rst b/docs/changes.rst index 6207b6797..04c7cf2dc 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -9,6 +9,10 @@ Next version * Added ``PRETTIFY_SQL`` configuration option to support controlling SQL token grouping. By default it's set to True. When set to False, a performance improvement can be seen by the SQL panel. +* ``HistoryPanel`` will be disabled when ``RENDER_PANELS`` is ``True`` + or if it's not set, but the server is running with multiple processes. +* Fixes ``RENDER_PANELS`` functionality so that when ``True`` panels are + rendered during the request and not loaded asynchronously. 3.2 (2020-12-03) diff --git a/docs/configuration.rst b/docs/configuration.rst index 92b493000..01d5b8436 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -66,6 +66,8 @@ Toolbar options The toolbar searches for this string in the HTML and inserts itself just before. +.. _RENDER_PANELS: + * ``RENDER_PANELS`` Default: ``None`` diff --git a/docs/installation.rst b/docs/installation.rst index 87b964e12..eb60c1277 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -147,3 +147,11 @@ And for Apache: .. _JavaScript module: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules .. _CORS errors: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSMissingAllowOrigin .. _Access-Control-Allow-Origin header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin + +Django Channels & Async +^^^^^^^^^^^^^^^^^^^^^^^ + +The Debug Toolbar currently doesn't support Django Channels or async projects. +If you are using Django channels are having issues getting panels to load, +please review the documentation for the configuration option +:ref:`RENDER_PANELS `. diff --git a/docs/panels.rst b/docs/panels.rst index c21e90801..f83c0770d 100644 --- a/docs/panels.rst +++ b/docs/panels.rst @@ -17,6 +17,11 @@ History This panel shows the history of requests made and allows switching to a past snapshot of the toolbar to view that request's stats. +.. caution:: + If :ref:`RENDER_PANELS ` configuration option is set to + ``True`` or if the server runs with multiple processes, the History Panel + will be disabled. + Version ~~~~~~~ @@ -184,9 +189,9 @@ URL: https://github.com/danyi1212/django-windowsauth Path: ``windows_auth.panels.LDAPPanel`` -LDAP Operations performed during the request, including timing, request and response messages, +LDAP Operations performed during the request, including timing, request and response messages, the entries received, write changes list, stack-tracing and error debugging. -This panel also shows connection usage metrics when it is collected. +This panel also shows connection usage metrics when it is collected. `Check out the docs `_. Line Profiler diff --git a/tests/test_integration.py b/tests/test_integration.py index 8c28f6c6e..f1f1cb1bb 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -289,11 +289,44 @@ def test_sql_profile_checks_show_toolbar(self): self.assertEqual(response.status_code, 404) @override_settings(DEBUG_TOOLBAR_CONFIG={"RENDER_PANELS": True}) - def test_data_store_id_not_rendered_when_none(self): + def test_render_panels_in_request(self): + """ + Test that panels are are rendered during the request with + RENDER_PANELS=TRUE + """ url = "/regular/basic/" response = self.client.get(url) self.assertIn(b'id="djDebug"', response.content) + # Verify the store id is not included. self.assertNotIn(b"data-store-id", response.content) + # Verify the history panel was disabled + self.assertIn( + b'', + response.content, + ) + # Verify the a panel was rendered + self.assertIn(b"Response headers", response.content) + + @override_settings(DEBUG_TOOLBAR_CONFIG={"RENDER_PANELS": False}) + def test_load_panels(self): + """ + Test that panels are not rendered during the request with + RENDER_PANELS=False + """ + url = "/execute_sql/" + response = self.client.get(url) + self.assertIn(b'id="djDebug"', response.content) + # Verify the store id is included. + self.assertIn(b"data-store-id", response.content) + # Verify the history panel was not disabled + self.assertNotIn( + b'', + response.content, + ) + # Verify the a panel was not rendered + self.assertNotIn(b"Response headers", response.content) def test_view_returns_template_response(self): response = self.client.get("/template_response/basic/") From 765fbd14523390e859b77c3fb1c093544c7a39ce Mon Sep 17 00:00:00 2001 From: Tim Schilling Date: Fri, 12 Feb 2021 08:05:47 -0600 Subject: [PATCH 2/3] Improved RENDER_PANELS documentation and change log. --- docs/changes.rst | 7 ++++--- docs/configuration.rst | 9 ++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/docs/changes.rst b/docs/changes.rst index 04c7cf2dc..c59a59fd2 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -9,9 +9,10 @@ Next version * Added ``PRETTIFY_SQL`` configuration option to support controlling SQL token grouping. By default it's set to True. When set to False, a performance improvement can be seen by the SQL panel. -* ``HistoryPanel`` will be disabled when ``RENDER_PANELS`` is ``True`` - or if it's not set, but the server is running with multiple processes. -* Fixes ``RENDER_PANELS`` functionality so that when ``True`` panels are +* Disabled ``HistoryPanel`` when ``RENDER_PANELS`` is ``True`` + or if ``RENDER_PANELS`` is ``None`` and the WSGI container is + running with multiple processes. +* Fixed ``RENDER_PANELS`` functionality so that when ``True`` panels are rendered during the request and not loaded asynchronously. diff --git a/docs/configuration.rst b/docs/configuration.rst index 01d5b8436..0d7cd87c4 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -73,14 +73,17 @@ Toolbar options Default: ``None`` If set to ``False``, the debug toolbar will keep the contents of panels in - memory on the server and load them on demand. If set to ``True``, it will - render panels inside every page. This may slow down page rendering but it's + memory on the server and load them on demand. + + If set to ``True``, it will disable ``HistoryPanel`` and render panels + inside every page. This may slow down page rendering but it's required on multi-process servers, for example if you deploy the toolbar in production (which isn't recommended). The default value of ``None`` tells the toolbar to automatically do the right thing depending on whether the WSGI container runs multiple processes. - This setting allows you to force a different behavior if needed. + This setting allows you to force a different behavior if needed. If the + WSGI container runs multiple processes, it will disable ``HistoryPanel``. * ``RESULTS_CACHE_SIZE`` From dc3f80be5cacfa469f8817caeacd9175c7f94b80 Mon Sep 17 00:00:00 2001 From: Tim Schilling Date: Fri, 12 Feb 2021 09:04:45 -0600 Subject: [PATCH 3/3] Add async to the allowed spellings. --- docs/spelling_wordlist.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/spelling_wordlist.txt b/docs/spelling_wordlist.txt index ede7915a1..7250ed750 100644 --- a/docs/spelling_wordlist.txt +++ b/docs/spelling_wordlist.txt @@ -37,3 +37,4 @@ unhashable uWSGI validator Werkzeug +async