Skip to content

Commit b72c03e

Browse files
committed
Changed issues to alerts, added docstring to check_invalid_... function, changed call in nav_subtitle to get_stats
1 parent 97fa2e5 commit b72c03e

File tree

4 files changed

+39
-27
lines changed

4 files changed

+39
-27
lines changed

debug_toolbar/panels/alerts.py

+18-12
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,26 @@ class AlertsPanel(Panel):
5151

5252
def __init__(self, *args, **kwargs):
5353
super().__init__(*args, **kwargs)
54-
self.issues = []
54+
self.alerts = []
5555

5656
@property
5757
def nav_subtitle(self):
58-
if self.issues:
59-
issue_text = "issue" if len(self.issues) == 1 else "issues"
60-
return f"{len(self.issues)} {issue_text} found"
58+
alerts = self.get_stats()["alerts"]
59+
if alerts:
60+
alert_text = "alert" if len(alerts) == 1 else "alerts"
61+
return f"{len(alerts)} {alert_text}"
6162
else:
6263
return ""
6364

64-
def add_issue(self, issue):
65-
self.issues.append(issue)
65+
def add_alert(self, alert):
66+
self.alerts.append(alert)
6667

6768
def check_invalid_file_form_configuration(self, html_content):
69+
"""
70+
Inspects HTML content for a form that includes a file input but does
71+
not have the encoding type set to multipart/form-data, and warns the
72+
user if so.
73+
"""
6874
parser = FormParser()
6975
parser.feed(html_content)
7076

@@ -78,18 +84,18 @@ def check_invalid_file_form_configuration(self, html_content):
7884
)
7985
):
8086
form_id = form["form_attrs"].get("id", "no form id")
81-
issue = (
87+
alert = (
8288
f'Form with id "{form_id}" contains file input but '
8389
"does not have multipart/form-data encoding."
8490
)
85-
self.add_issue({"issue": issue})
86-
return self.issues
91+
self.add_alert({"alert": alert})
92+
return self.alerts
8793

8894
def generate_stats(self, request, response):
8995
html_content = response.content.decode(response.charset)
9096
self.check_invalid_file_form_configuration(html_content)
9197

92-
# Further issue checks can go here
98+
# Further alert checks can go here
9399

94-
# Write all issues to record_stats
95-
self.record_stats({"issues": self.issues})
100+
# Write all alerts to record_stats
101+
self.record_stats({"alerts": self.alerts})
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{% load i18n %}
22

3-
{% if issues %}
4-
<h4>{% trans "Issues found" %}</h4>
5-
{% for issue in issues %}
3+
{% if alerts %}
4+
<h4>{% trans "alerts found" %}</h4>
5+
{% for alert in alerts %}
66
<ul>
7-
<li>{{ issue.issue }}</li>
7+
<li>{{ alert.alert }}</li>
88
</ul>
99
{% endfor %}
1010
{% else %}
11-
<p>No issues found.</p>
11+
<p>No alerts found.</p>
1212
{% endif %}

example/templates/index.html

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ <h1>Index of Tests</h1>
2323
<button id="incrementFetch" data-url="{% url 'ajax_increment' %}" type="button">Increment via fetch</button>
2424
<button id="incrementXHR" data-url="{% url 'ajax_increment' %}" type="button">Increment via XHR</button>
2525
</p>
26+
<form id="hi1" action="/submit1" method="get">
27+
<form id="hi2" action="/submit2" method="get">
28+
<input type="text" name="test_input">
29+
<input type="submit" value="Submit">
30+
</form>
31+
</form>
32+
<input type = "file" form = "hi1">
2633
<script>
2734
const incrementFetch = document.querySelector("#incrementFetch");
2835
const incrementXHR = document.querySelector("#incrementXHR");

tests/panels/test_alerts.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,16 @@
77
class AlertsPanelTestCase(BaseTestCase):
88
panel_id = "AlertsPanel"
99

10-
def test_issue_warning_display(self):
10+
def test_alert_warning_display(self):
1111
"""
12-
Test that the panel (does not) display[s] a warning when there are
13-
(no) issues.
12+
Test that the panel (does not) display[s] an alert when there are
13+
(no) problems.
1414
"""
15-
self.panel.issues = 0
16-
nav_subtitle = self.panel.nav_subtitle
17-
self.assertNotIn("issues found", nav_subtitle)
15+
self.panel.record_stats({"alerts": []})
16+
self.assertNotIn("alerts", self.panel.nav_subtitle)
1817

19-
self.panel.issues = ["Issue 1", "Issue 2"]
20-
nav_subtitle = self.panel.nav_subtitle
21-
self.assertIn("2 issues found", nav_subtitle)
18+
self.panel.record_stats({"alerts": ["Alert 1", "Alert 2"]})
19+
self.assertIn("2 alerts", self.panel.nav_subtitle)
2220

2321
def test_file_form_without_enctype_multipart_form_data(self):
2422
"""
@@ -31,7 +29,7 @@ def test_file_form_without_enctype_multipart_form_data(self):
3129
'Form with id "test-form" contains file input '
3230
"but does not have multipart/form-data encoding."
3331
)
34-
self.assertEqual(result[0]["issue"], expected_error)
32+
self.assertEqual(result[0]["alert"], expected_error)
3533
self.assertEqual(len(result), 1)
3634

3735
def test_file_form_with_enctype_multipart_form_data(self):
@@ -50,6 +48,7 @@ def test_integration_file_form_without_enctype_multipart_form_data(self):
5048

5149
self.panel.generate_stats(self.request, response)
5250

51+
self.assertIn("1 alert", self.panel.nav_subtitle)
5352
self.assertIn(
5453
"Form with id &quot;test-form&quot; contains file input "
5554
"but does not have multipart/form-data encoding.",

0 commit comments

Comments
 (0)