Sfoglia il codice sorgente

Cast settings' verbose_name to string

- Fix for using gettext_lazy for verbose_name on a generic setting model, you get a TypeError: __str__ returned non-string (type __proxy__)
Sébastien Corbin 1 anno fa
parent
commit
707e719d29

+ 1 - 0
CHANGELOG.txt

@@ -31,6 +31,7 @@ Changelog
  * Fix: Ensure taggit field type-ahead options show correctly in the dark mode theme (Sage Abdullah)
  * Fix: Fix the lock description message missing the model_name variable when locked only by system (Sébastien Corbin)
  * Fix: Fix empty blocks created in migration operations (Sandil Ranasinghe)
+ * Fix: Ensure that gettext_lazy works correctly when using verbose_name on a generic Settings models (Sébastien Corbin)
  * Docs: Document how to add non-ModelAdmin views to a `ModelAdminGroup` (Onno Timmerman)
  * Docs: Document how to add StructBlock data to a StreamField (Ramon Wenger)
  * Docs: Update ReadTheDocs settings to v2 to resolve urllib3 issue in linkcheck extension (Thibaud Colas)

+ 1 - 0
docs/releases/5.1.md

@@ -61,6 +61,7 @@ Thank you to Damilola for his work, and to Google for sponsoring this project.
  * Ensure taggit field type-ahead options show correctly in the dark mode theme (Sage Abdullah)
  * Fix the lock description message missing the model_name variable when locked only by system (Sébastien Corbin)
  * Fix empty blocks created in migration operations (Sandil Ranasinghe)
+ * Ensure that `gettext_lazy` works correctly when using `verbose_name` on a generic Settings models (Sébastien Corbin)
 
 ### Documentation
 

+ 1 - 1
wagtail/contrib/settings/models.py

@@ -199,4 +199,4 @@ class BaseGenericSetting(AbstractSetting):
         return obj
 
     def __str__(self):
-        return self._meta.verbose_name
+        return str(self._meta.verbose_name)

+ 8 - 0
wagtail/contrib/settings/tests/generic/test_model.py

@@ -113,3 +113,11 @@ class GenericSettingModelTestCase(GenericSettingsTestMixin, TestCase):
                 self.assertEqual(settings.get_page_url("test_attribute"), "")
                 # when called indirectly via shortcut
                 self.assertEqual(settings.page_url.test_attribute, "")
+
+    def test_display_as_string(self):
+        self._create_importantpagesgenericsetting_object()
+
+        self.assertEqual(
+            str(ImportantPagesGenericSetting.load()),
+            "Important pages settings",
+        )

+ 19 - 0
wagtail/test/testapp/migrations/0025_alter_importantpagesgenericsetting_options.py

@@ -0,0 +1,19 @@
+# Generated by Django 4.0.10 on 2023-06-29 08:13
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+    dependencies = [
+        ("tests", "0024_fullfeaturedsnippet_country_code_and_more"),
+    ]
+
+    operations = [
+        migrations.AlterModelOptions(
+            name="importantpagesgenericsetting",
+            options={
+                "verbose_name": "Important pages settings",
+                "verbose_name_plural": "Important pages settings",
+            },
+        ),
+    ]

+ 5 - 1
wagtail/test/testapp/models.py

@@ -1588,7 +1588,7 @@ class ImportantPagesSiteSetting(BaseSiteSetting):
     )
 
 
-@register_setting
+@register_setting(name="important-pages-generic-setting")
 class ImportantPagesGenericSetting(BaseGenericSetting):
     sign_up_page = models.ForeignKey(
         "wagtailcore.Page", related_name="+", null=True, on_delete=models.SET_NULL
@@ -1600,6 +1600,10 @@ class ImportantPagesGenericSetting(BaseGenericSetting):
         "wagtailcore.Page", related_name="+", null=True, on_delete=models.SET_NULL
     )
 
+    class Meta:
+        verbose_name = _("Important pages settings")
+        verbose_name_plural = _("Important pages settings")
+
 
 @register_setting(icon="icon-setting-tag")
 class IconSiteSetting(BaseSiteSetting):