Explorar el Código

Fixes #23751: Preserve lazy translation of comment field help text (#23762)

Use format_lazy() to prevent the help text from being translated at
import time, ensuring it respects the active language.

Add regression coverage for localized help text.
lexapi hace 15 horas
padre
commit
cd637bbdc9
Se han modificado 2 ficheros con 32 adiciones y 6 borrados
  1. 9 4
      netbox/utilities/forms/fields/fields.py
  2. 23 2
      netbox/utilities/tests/test_forms.py

+ 9 - 4
netbox/utilities/forms/fields/fields.py

@@ -7,6 +7,7 @@ from django.db.models import Count
 from django.forms.fields import InvalidJSONInput
 from django.forms.fields import JSONField as _JSONField
 from django.templatetags.static import static
+from django.utils.text import format_lazy
 from django.utils.translation import gettext_lazy as _
 from netaddr import EUI, AddrFormatError
 
@@ -61,10 +62,14 @@ class CommentField(forms.CharField):
     """
     widget = widgets.MarkdownWidget
     label = _('Comments')
-    help_text = _(
-        '<i class="mdi mdi-information-outline" aria-hidden="true"></i> '
-        '<a href="{url}" target="_blank" tabindex="-1">Markdown</a> syntax is supported'
-    ).format(url=static('docs/reference/markdown/'))
+    # Keep the help text lazy so it uses the active language instead of the language at import time.
+    help_text = format_lazy(
+        _(
+            '<i class="mdi mdi-information-outline" aria-hidden="true"></i> '
+            '<a href="{url}" target="_blank" tabindex="-1">Markdown</a> syntax is supported'
+        ),
+        url=static('docs/reference/markdown/')
+    )
 
     def __init__(self, *, label=label, help_text=help_text, required=False, **kwargs):
         super().__init__(label=label, help_text=help_text, required=required, **kwargs)

+ 23 - 2
netbox/utilities/tests/test_forms.py

@@ -4,14 +4,18 @@ from types import SimpleNamespace
 
 from django import forms
 from django.contrib.contenttypes.models import ContentType
-from django.test import TestCase, override_settings
+from django.templatetags.static import static
+from django.test import TestCase, override_settings, tag
+from django.utils import translation
+from django.utils.functional import Promise
 from django.utils.html import escape
+from django.utils.translation import gettext
 
 from dcim.models import Rack, Site
 from netbox.choices import ColorChoices, ImportFormatChoices
 from utilities.choices import Choice, ChoiceSet
 from utilities.forms.bulk_import import BulkImportForm
-from utilities.forms.fields import ChoiceField, MultipleChoiceField, TypedChoiceField
+from utilities.forms.fields import ChoiceField, CommentField, MultipleChoiceField, TypedChoiceField
 from utilities.forms.fields.csv import CSVModelChoiceField, CSVModelMultipleChoiceField, CSVSelectWidget
 from utilities.forms.fields.dynamic import (
     DynamicChoiceField,
@@ -689,6 +693,23 @@ class SelectMultipleWidgetTestCase(TestCase):
         self.assertEqual(widget.choices[1][1], [(3, 'Option 3')])
 
 
+class FormFieldsTestCase(TestCase):
+    """
+    Tests for shared form field classes.
+    """
+
+    @tag('regression')  # Ref: #23751
+    def test_comment_field_help_text_translation(self):
+        with translation.override('it'):
+            field = CommentField()
+            self.assertIsInstance(field.help_text, Promise)
+            expected = gettext(
+                '<i class="mdi mdi-information-outline" aria-hidden="true"></i> '
+                '<a href="{url}" target="_blank" tabindex="-1">Markdown</a> syntax is supported'
+            ).format(url=static('docs/reference/markdown/'))
+            self.assertEqual(str(field.help_text), expected)
+
+
 class DynamicChoiceFieldTestCase(TestCase):
     """
     Validate that DynamicChoiceField.get_bound_field() limits choices to the current