8000 fix: django 5's choice widget is not lazy (#7707) · django-cms/django-cms@19c66fe · GitHub
[go: up one dir, main page]

Skip to content

Commit 19c66fe

Browse files
authored
fix: django 5's choice widget is not lazy (#7707)
* Support for Django 5.0 * Fix: test.yml and django Promise handling * Shorten github action names * Update test.yml * Update _cms.scss * Update setup.py * Fix: Django 5 choice widget is not lazy either * Add test * Fix: Swapped underscore * Deprecate SuperLazyIterator and LazyChoiceField
1 parent 22d6869 commit 19c66fe

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

cms/forms/fields.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1+
import warnings
2+
13
from django import forms
24
from django.contrib.admin.widgets import RelatedFieldWidgetWrapper
35
from django.core.validators import EMPTY_VALUES
6+
from django.forms import ChoiceField
47
from django.utils.translation import gettext_lazy as _
58

69
from cms.forms.utils import get_page_choices, get_site_choices
710
from cms.forms.validators import validate_url
811
from cms.forms.widgets import PageSelectWidget, PageSmartLinkWidget
912
from cms.models.pagemodel import Page
13+
from cms.utils.compat import DJANGO_4_2
1014

1115

12-
class SuperLazyIterator():
16+
class SuperLazyIterator:
1317
def __init__(self, func):
18+
warnings.warn("SuperLazyIterator is deprecated.",
19+
DeprecationWarning, stacklevel=2)
1420
self.func = func
1521

1622
def __iter__(self):
@@ -19,14 +25,23 @@ def __iter__(self):
1925

2026
class LazyChoiceField(forms.ChoiceField):
2127

28+
def __init__(self, *args, **kwargs):
29+
warnings.warn("LazyChoiceField is deprecated. Use Django's ChoiceField instead.",
30+
DeprecationWarning, stacklevel=2)
31+
super().__init__(*args, **kwargs)
32+
2233
@property
2334
def choices(self):
2435
return self._choices
2536

2637
@choices.setter
2738
def choices(self, value):
28-
# we overwrite this function so no normalize(value) is called
29-
self._choices = self.widget.choices = value
39+
# we overwrite this function so no list(value) or normalize_choices(value) is called
40+
# also, do not call the widget's setter as of Django 5
41+
if DJANGO_4_2:
42+
self._choices = self.widget.choices = value
43+
else:
44+
self._choices = self.widget._choices = value
3045

3146

3247
class PageSelectFormField(forms.MultiValueField):
@@ -49,13 +64,11 @@ def __init__(self, queryset=None, empty_label="---------", cache_choices=False,
4964
errors = self.default_error_messages.copy()
5065
if 'error_messages' in kwargs:
5166
errors.update(kwargs['error_messages'])
52-
site_choices = SuperLazyIterator(get_site_choices)
53-
page_choices = SuperLazyIterator(get_page_choices)
5467
self.limit_choices_to = limit_choices_to
5568
kwargs['required'] = required
5669
fields = (
57-
LazyChoiceField(choices=site_choices, required=False, error_messages={'invalid': errors['invalid_site']}),
58-
LazyChoiceField(choices=page_choices, required=False, error_messages={'invalid': errors['invalid_page']}),
70+
ChoiceField(choices=get_site_choices, required=False, error_messages={'invalid': errors['invalid_site']}),
71+
ChoiceField(choices=get_page_choices, required=False, error_messages={'invalid': errors['invalid_page']}),
5972
)
6073

6174
# Remove the unexpected blank kwarg if it's supplied,

cms/tests/test_forms.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,17 @@ def test_superlazy_iterator_behaves_properly_for_pages(self):
246246

247247
self.assertEqual(normal_result, list(lazy_result))
248248

249+
def test_lazy_choice_field_behaves_properly(self):
250+
"""Ensure LazyChoiceField is really lazy"""
251+
choices_called = False
252+
def get_choices():
253+
nonlocal choices_called
254+
choices_called = True
255+
return ("", "-----"),
256+
257+
LazyChoiceField(choices=SuperLazyIterator(get_choices))
258+
self.assertFalse(choices_called, "Lazy choice function called")
259+
249260

250261
class PermissionFormTestCase(CMSTestCase):
251262

0 commit comments

Comments
 (0)
0