Skip to content
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

1843 new ajax request resets whole view if historypanel is enabled #1872

Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
096fdf4
New AJAX request resets whole view if HistoryPanel is enabled
elineda Jan 15, 2024
f499e41
New AJAX request resets whole view if HistoryPanel is enabled
elineda Jan 18, 2024
73c96eb
New AJAX request resets whole view if HistoryPanel is enabled
elineda Jan 23, 2024
def324a
New AJAX request resets whole view if HistoryPanel is enabled
elineda Jan 24, 2024
150e11b
New AJAX request resets whole view if HistoryPanel is enabled
elineda Jan 24, 2024
0419cd7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 24, 2024
c6d3b65
Add ajax in spelling_wordlist.txt
elineda Jan 24, 2024
bd2a24e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 24, 2024
d23bbd2
New AJAX request resets whole view if HistoryPanel is enabled
elineda Jan 24, 2024
5c3da4c
Merge remote-tracking branch 'origin/1843-new-ajax-request-resets-who…
elineda Jan 24, 2024
1d2ed26
Merge branch 'main' into 1843-new-ajax-request-resets-whole-view-if-h…
elineda Jan 25, 2024
4cb7900
Documentation suggestions
tim-schilling Jan 25, 2024
20fad64
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 25, 2024
59e6a28
Update for line lengths.
tim-schilling Jan 25, 2024
43a6966
Clean-up Tim's English in the changelog
tim-schilling Jan 26, 2024
ba03e9f
New AJAX request resets whole view if HistoryPanel is enabled
elineda Jan 29, 2024
0fa7647
New AJAX request resets whole view if HistoryPanel is enabled
elineda Jan 29, 2024
ea556df
New AJAX request resets whole view if HistoryPanel is enabled
elineda Jan 29, 2024
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
1 change: 1 addition & 0 deletions debug_toolbar/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"SQL_WARNING_THRESHOLD": 500, # milliseconds
"OBSERVE_REQUEST_CALLBACK": "debug_toolbar.toolbar.observe_request",
"TOOLBAR_LANGUAGE": None,
"UPDATE_ON_FETCH": True,
}


Expand Down
3 changes: 3 additions & 0 deletions debug_toolbar/static/debug_toolbar/js/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,6 @@ $$.on(djDebug, "click", ".refreshHistory", function (event) {
event.preventDefault();
refreshHistory();
});
// We don't refresh the whole toolbar each fetch or ajax request,
// so we need to refresh the history when we open the panel
$$.onPanelRender(djDebug, "HistoryPanel", refreshHistory);
elineda marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 5 additions & 1 deletion debug_toolbar/static/debug_toolbar/js/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ function getDebugElement() {

const djdt = {
handleDragged: false,
needUpdateOnFetch: false,
init() {
const djDebug = getDebugElement();
djdt.needUpdateOnFetch = djDebug.dataset.updateOnFetch === "True";
$$.on(djDebug, "click", "#djDebugPanelList li a", function (event) {
event.preventDefault();
if (!this.className) {
Expand Down Expand Up @@ -274,7 +276,9 @@ const djdt = {
storeId = encodeURIComponent(storeId);
const dest = `${sidebarUrl}?store_id=${storeId}`;
slowjax(dest).then(function (data) {
replaceToolbarState(storeId, data);
if (djdt.needUpdateOnFetch){
replaceToolbarState(storeId, data);
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion debug_toolbar/templates/debug_toolbar/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
data-sidebar-url="{{ history_url }}"
{% endif %}
data-default-show="{% if toolbar.config.SHOW_COLLAPSED %}false{% else %}true{% endif %}"
{{ toolbar.config.ROOT_TAG_EXTRA_ATTRS|safe }}>
{{ toolbar.config.ROOT_TAG_EXTRA_ATTRS|safe }} data-update-on-fetch="{{ toolbar.config.UPDATE_ON_FETCH|safe }}">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{{ toolbar.config.ROOT_TAG_EXTRA_ATTRS|safe }} data-update-on-fetch="{{ toolbar.config.UPDATE_ON_FETCH|safe }}">
{{ toolbar.config.ROOT_TAG_EXTRA_ATTRS|safe }} data-update-on-fetch="{{ toolbar.config.UPDATE_ON_FETCH }}">

I'm not 100% sure but I don't think |safe is necessary.

Also, you could use |yesno:'true,false' or do it as data-default-show so that the value is lowercased already, but I'm just suggesting that, not asking for anything.

Copy link
Member Author

@elineda elineda Jan 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed safe. But I didn't used yesno because the result of this will be a string and we will need do to the same things in js for checking it, lowercased or not.

<div class="djdt-hidden" id="djDebugToolbar">
<ul id="djDebugPanelList">
<li><a id="djHideToolBarButton" href="#" title="{% trans 'Hide toolbar' %}">{% trans "Hide" %} »</a></li>
Expand Down
3 changes: 3 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Pending
<https://astral.sh/blog/the-ruff-formatter>`__.
* Changed the default position of the toolbar from top to the upper top
position.
* Added the setting, ``UPDATE_ON_FETCH`` to control whether the
toolbar automatically updates to the latest AJAX request or not.
It defaults to ``False``.

4.2.0 (2023-08-10)
------------------
Expand Down
10 changes: 10 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ Toolbar options
but want to render your application in French, you would set this to
``"en-us"`` and :setting:`LANGUAGE_CODE` to ``"fr"``.

.. _UPDATE_ON_FETCH:

* ``UPDATE_ON_FETCH``

Default: ``False``

This controls whether the toolbar should update to the latest AJAX
request when it occurs. This is useful to enable if when "boosting"
the web application with a JavaScript library such as htmx.
tim-schilling marked this conversation as resolved.
Show resolved Hide resolved

Panel options
~~~~~~~~~~~~~

Expand Down
1 change: 1 addition & 0 deletions docs/spelling_wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Pympler
Roboto
Transifex
Werkzeug
ajax
async
backend
backends
Expand Down
21 changes: 21 additions & 0 deletions tests/templates/ajax/ajax.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends "base.html" %}
{% block content %}
<div id="click_for_ajax">click for ajax</div>

<script>

let click_for_ajax = document.getElementById("click_for_ajax")
function send_ajax() {
let xhr = new XMLHttpRequest();
let url = '/json_view/';
xhr.open("GET", url, true);
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText);
}
}
xhr.send();
}
document.addEventListener("click", (event) => {send_ajax()});
</script>
{% endblock %}
17 changes: 17 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,3 +749,20 @@ def test_toolbar_language_will_render_to_locale_when_set_both(self):
)
self.assertIn("Query", table.text)
self.assertIn("Action", table.text)

def test_ajax_dont_refresh(self):
self.get("/ajax/")
make_ajax = self.selenium.find_element(By.ID, "click_for_ajax")
make_ajax.click()
history_panel = self.selenium.find_element(By.ID, "djdt-HistoryPanel")
self.assertIn("/ajax/", history_panel.text)
self.assertNotIn("/json_view/", history_panel.text)

@override_settings(DEBUG_TOOLBAR_CONFIG={"UPDATE_ON_FETCH": True})
def test_ajax_refresh(self):
self.get("/ajax/")
make_ajax = self.selenium.find_element(By.ID, "click_for_ajax")
make_ajax.click()
history_panel = self.selenium.find_element(By.ID, "djdt-HistoryPanel")
self.assertNotIn("/ajax/", history_panel.text)
self.assertIn("/json_view/", history_panel.text)
1 change: 1 addition & 0 deletions tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
path("cached_low_level_view/", views.cached_low_level_view),
path("json_view/", views.json_view),
path("redirect/", views.redirect_view),
path("ajax/", views.ajax_view),
path("login_without_redirect/", LoginView.as_view(redirect_field_name=None)),
path("admin/", admin.site.urls),
path("__debug__/", include("debug_toolbar.urls")),
Expand Down
4 changes: 4 additions & 0 deletions tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ def listcomp_view(request):

def redirect_view(request):
return HttpResponseRedirect("/regular/redirect/")


def ajax_view(request):
return render(request, "ajax/ajax.html")
Loading