Browse Source

Skip Locale query when WAGTAIL_I18N_ENABLED is False

- Fixes #10329
zerolab 1 year ago
parent
commit
cd131ddec8

+ 2 - 1
CHANGELOG.txt

@@ -85,7 +85,8 @@ Changelog
  * Fix: Fix incorrect API serialisation for document `download_url` when `WAGTAILDOCS_SERVE_METHOD` is `direct` (Swojak-A)
  * Fix: Fix template configuration of snippets index results view (fidoriel, Sage Abdullah)
  * Fix: Prevent long preview mode names from making the select element overflow the side panel (Sage Abdullah)
- * Autosize text area field will now correctly resize when switching between comments toggle states (Suyash Srivastava)
+ * Fix: Autosize text area field will now correctly resize when switching between comments toggle states (Suyash Srivastava)
+ * Fix: When i18n is not enabled, avoid making a Locale query on every page view (Dan Braghis)
  * Docs: Add code block to make it easier to understand contribution docs (Suyash Singh)
  * Docs: Add new "Icons" page for icons customisation and reuse across the admin interface (Coen van der Kamp, Thibaud Colas)
  * Docs: Fix broken formatting for MultiFieldPanel / FieldRowPanel permission kwarg docs (Matt Westcott)

+ 1 - 0
docs/releases/5.0.md

@@ -141,6 +141,7 @@ We hope this new theme will bring accessibility improvements for users who perfe
  * Fix incorrect API serialisation for document `download_url` when `WAGTAILDOCS_SERVE_METHOD` is `direct` (Swojak-A)
  * Fix template configuration of snippets index results view (fidoriel, Sage Abdullah)
  * Prevent long preview mode names from making the select element overflow the side panel (Sage Abdullah)
+ * When i18n is not enabled, avoid making a Locale query on every page view (Dan Braghis)
 
 ### Documentation
 

+ 3 - 0
wagtail/models/i18n.py

@@ -240,6 +240,9 @@ class TranslatableMixin(models.Model):
         Note: This will return translations that are in draft. If you want to exclude
         these, use the ``.localized`` attribute.
         """
+        if not getattr(settings, "WAGTAIL_I18N_ENABLED", False):
+            return self
+
         try:
             locale = Locale.get_active()
         except (LookupError, Locale.DoesNotExist):

+ 8 - 0
wagtail/tests/test_page_model.py

@@ -3496,6 +3496,7 @@ class TestDefaultLocale(TestCase):
         self.assertEqual(page.locale, fr_locale)
 
 
+@override_settings(WAGTAIL_I18N_ENABLED=True)
 class TestLocalized(TestCase):
     fixtures = ["test.json"]
 
@@ -3520,6 +3521,13 @@ class TestLocalized(TestCase):
                 self.event_page.localized_draft, self.fr_event_page.page_ptr
             )
 
+    @override_settings(WAGTAIL_I18N_ENABLED=False)
+    def test_localized_different_language_with_wagtail_i18n_enabled_false(self):
+        """Should return the same page if WAGTAIL_I18N_ENABLED is False"""
+        with translation.override("fr"):
+            self.assertEqual(self.event_page.localized, self.event_page)
+            self.assertEqual(self.event_page.localized_draft, self.event_page)
+
     def test_localized_different_language_unpublished(self):
         # We shouldn't autolocalize if the translation is unpublished
         self.fr_event_page.unpublish()

+ 3 - 1
wagtail/tests/test_translatablemixin.py

@@ -2,7 +2,7 @@ from unittest.mock import patch
 
 from django.conf import settings
 from django.core import checks
-from django.test import TestCase
+from django.test import TestCase, override_settings
 
 from wagtail.models import Locale
 from wagtail.test.i18n.models import (
@@ -21,6 +21,7 @@ def make_test_instance(model=None, **kwargs):
     return model.objects.create(**kwargs)
 
 
+@override_settings(WAGTAIL_I18N_ENABLED=True)
 class TestTranslatableMixin(TestCase):
     def setUp(self):
         language_codes = dict(settings.LANGUAGES).keys()
@@ -158,6 +159,7 @@ class TestTranslatableMixin(TestCase):
         self.assertEqual(copy_translatable_child.locale, self.another_locale)
 
 
+@override_settings(WAGTAIL_I18N_ENABLED=True)
 class TestLocalized(TestCase):
     def setUp(self):
         self.en_locale = Locale.objects.get()