Skip to content

Commit

Permalink
Amended fix for django-commons#1682 -- added warning in nav_subtitle
Browse files Browse the repository at this point in the history
  • Loading branch information
bkdekoning committed Jun 6, 2024
1 parent d19bf7b commit 80071cc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
19 changes: 17 additions & 2 deletions debug_toolbar/panels/templates/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,23 @@ def title(self):

@property
def nav_subtitle(self):
subtitle_parts = []
if self.templates:
return self.templates[0]["template"].name
return ""
template_name = self.templates[0]["template"].name or "No template name"
subtitle_parts.append(template_name)
if self.invalid_file_form_configs_count:
# Add warning for user indicating file form configuration issue
issue_text = (
"issue" if self.invalid_file_form_configs_count == 1 else "issues"
)
subtitle_parts.append(
f"{self.invalid_file_form_configs_count} file form configuration "
f"{issue_text} found"
)
if len(subtitle_parts) > 1:
return " - ".join(subtitle_parts)
else:
return subtitle_parts[0] if subtitle_parts else ""

template = "debug_toolbar/panels/templates.html"

Expand Down Expand Up @@ -260,6 +274,7 @@ def generate_stats(self, request, response):
invalid_file_form_configs = self.check_invalid_file_form_configuration(
html_content
)
self.invalid_file_form_configs_count = len(invalid_file_form_configs)

self.record_stats(
{
Expand Down
16 changes: 16 additions & 0 deletions tests/panels/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def test_file_form_without_enctype_multipart_form_data(self):
'but missing enctype="multipart/form-data".'
)
self.assertEqual(result[0]["error_message"], expected_error)
self.assertEqual(len(result), 1)

def test_file_form_with_enctype_multipart_form_data(self):
test_form = """<form id="test-form" enctype="multipart/form-data">
Expand All @@ -90,6 +91,21 @@ def test_file_form_with_enctype_multipart_form_data(self):

self.assertEqual(len(result), 0)

def test_file_form_configuration_warning_display(self):
"""
Test that the panel (does not) display[s] a warning when there are (no)
forms with file input types but encoding not set to multipart/form-data.
"""
# Should not show issue in subtitle when no invalid configurations
self.panel.invalid_file_form_configs_count = 0
nav_subtitle = self.panel.nav_subtitle
self.assertNotIn("file form configuration issue", nav_subtitle)

# Should show issue in subtitle when invalid configurations
self.panel.invalid_file_form_configs_count = 2
nav_subtitle = self.panel.nav_subtitle
self.assertIn("2 file form configuration issues found", nav_subtitle)

def test_insert_content(self):
"""
Test that the panel only inserts content after generate_stats and
Expand Down

0 comments on commit 80071cc

Please sign in to comment.