From 80071cc7c40d08c3f315d93e2c229fba0e666766 Mon Sep 17 00:00:00 2001 From: "Bart K. de Koning" Date: Thu, 6 Jun 2024 17:27:31 -0400 Subject: [PATCH] Amended fix for #1682 -- added warning in nav_subtitle --- debug_toolbar/panels/templates/panel.py | 19 +++++++++++++++++-- tests/panels/test_template.py | 16 ++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/debug_toolbar/panels/templates/panel.py b/debug_toolbar/panels/templates/panel.py index c007e032e..d8a027af5 100644 --- a/debug_toolbar/panels/templates/panel.py +++ b/debug_toolbar/panels/templates/panel.py @@ -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" @@ -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( { diff --git a/tests/panels/test_template.py b/tests/panels/test_template.py index 241fc3dc8..7870fdec0 100644 --- a/tests/panels/test_template.py +++ b/tests/panels/test_template.py @@ -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 = """
@@ -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