Skip to content

Commit

Permalink
Merge pull request #1440 from tim-schilling/render-panels-rework
Browse files Browse the repository at this point in the history
Correct RENDER_PANELS functionality and when enabled disable HistoryPanel
  • Loading branch information
matthiask authored Jul 20, 2021
2 parents 085f8dd + 8d39876 commit 9b6aae4
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 8 deletions.
6 changes: 6 additions & 0 deletions debug_toolbar/panels/history/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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."""
Expand Down
5 changes: 4 additions & 1 deletion debug_toolbar/templates/debug_toolbar/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
<script type="module" src="{% static 'debug_toolbar/js/toolbar.js' %}" async></script>
{% endblock %}
<div id="djDebug" class="djdt-hidden" dir="ltr"
{% if toolbar.store_id %}data-store-id="{{ toolbar.store_id }}" data-render-panel-url="{% url 'djdt:render_panel' %}"{% endif %}
{% if not toolbar.should_render_panels %}
data-store-id="{{ toolbar.store_id }}"
data-render-panel-url="{% url 'djdt:render_panel' %}"
{% endif %}
data-default-show="{% if toolbar.config.SHOW_COLLAPSED %}false{% else %}true{% endif %}"
{{ toolbar.config.ROOT_TAG_EXTRA_ATTRS|safe }}>
<div class="djdt-hidden" id="djDebugToolbar">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<h3>{{ panel.title }}</h3>
</div>
<div class="djDebugPanelContent">
{% if toolbar.store_id %}
{% if toolbar.should_render_panels %}
<div class="djdt-scroll">{{ panel.content }}</div>
{% else %}
<div class="djdt-loader"></div>
<div class="djdt-scroll"></div>
{% else %}
<div class="djdt-scroll">{{ panel.content }}</div>
{% endif %}
</div>
</div>
Expand Down
4 changes: 4 additions & 0 deletions debug_toolbar/toolbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,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"]
Expand Down
5 changes: 5 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ Next version
------------

* Ensured that the handle stays within bounds when resizing the window.
* 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.


3.2.1 (2021-04-14)
Expand Down
11 changes: 8 additions & 3 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,24 @@ Toolbar options
The toolbar searches for this string in the HTML and inserts itself just
before.

.. _RENDER_PANELS:

* ``RENDER_PANELS``

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``

Expand Down
8 changes: 8 additions & 0 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <RENDER_PANELS>`.
5 changes: 5 additions & 0 deletions docs/panels.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <RENDER_PANELS>` configuration option is set to
``True`` or if the server runs with multiple processes, the History Panel
will be disabled.

Version
~~~~~~~

Expand Down
1 change: 1 addition & 0 deletions docs/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ unhashable
uWSGI
validator
Werkzeug
async
35 changes: 34 additions & 1 deletion tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,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'<input type="checkbox" data-cookie="djdtHistoryPanel" '
b'title="Enable for next and successive requests">',
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'<input type="checkbox" data-cookie="djdtHistoryPanel" '
b'title="Enable for next and successive requests">',
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/")
Expand Down

0 comments on commit 9b6aae4

Please sign in to comment.