mirror of https://github.com/django/django.git
Fixed #30758 -- Made RangeFields use multiple hidden inputs for initial data.
This commit is contained in:
parent
733dbb21c7
commit
faf4b988fe
|
@ -2,20 +2,38 @@ from psycopg2.extras import DateRange, DateTimeTZRange, NumericRange
|
|||
|
||||
from django import forms
|
||||
from django.core import exceptions
|
||||
from django.forms.widgets import MultiWidget
|
||||
from django.forms.widgets import HiddenInput, MultiWidget
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
__all__ = [
|
||||
'BaseRangeField', 'IntegerRangeField', 'DecimalRangeField',
|
||||
'DateTimeRangeField', 'DateRangeField', 'RangeWidget',
|
||||
'DateTimeRangeField', 'DateRangeField', 'HiddenRangeWidget', 'RangeWidget',
|
||||
]
|
||||
|
||||
|
||||
class RangeWidget(MultiWidget):
|
||||
def __init__(self, base_widget, attrs=None):
|
||||
widgets = (base_widget, base_widget)
|
||||
super().__init__(widgets, attrs)
|
||||
|
||||
def decompress(self, value):
|
||||
if value:
|
||||
return (value.lower, value.upper)
|
||||
return (None, None)
|
||||
|
||||
|
||||
class HiddenRangeWidget(RangeWidget):
|
||||
"""A widget that splits input into two <input type="hidden"> inputs."""
|
||||
def __init__(self, attrs=None):
|
||||
super().__init__(HiddenInput, attrs)
|
||||
|
||||
|
||||
class BaseRangeField(forms.MultiValueField):
|
||||
default_error_messages = {
|
||||
'invalid': _('Enter two valid values.'),
|
||||
'bound_ordering': _('The start of the range must not exceed the end of the range.'),
|
||||
}
|
||||
hidden_widget = HiddenRangeWidget
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
if 'widget' not in kwargs:
|
||||
|
@ -82,14 +100,3 @@ class DateRangeField(BaseRangeField):
|
|||
default_error_messages = {'invalid': _('Enter two valid dates.')}
|
||||
base_field = forms.DateField
|
||||
range_type = DateRange
|
||||
|
||||
|
||||
class RangeWidget(MultiWidget):
|
||||
def __init__(self, base_widget, attrs=None):
|
||||
widgets = (base_widget, base_widget)
|
||||
super().__init__(widgets, attrs)
|
||||
|
||||
def decompress(self, value):
|
||||
if value:
|
||||
return (value.lower, value.upper)
|
||||
return (None, None)
|
||||
|
|
|
@ -5,6 +5,7 @@ from decimal import Decimal
|
|||
from django import forms
|
||||
from django.core import exceptions, serializers
|
||||
from django.db.models import DateField, DateTimeField, F, Func, Value
|
||||
from django.http import QueryDict
|
||||
from django.test import override_settings
|
||||
from django.utils import timezone
|
||||
|
||||
|
@ -513,8 +514,9 @@ class TestFormField(PostgreSQLSimpleTestCase):
|
|||
</th><td>
|
||||
<input type="text" name="datetime_field_0" id="id_datetime_field_0">
|
||||
<input type="text" name="datetime_field_1" id="id_datetime_field_1">
|
||||
<input type="hidden" name="initial-datetime_field" value="[None, None]"
|
||||
id="initial-id_datetime_field"></td></tr>
|
||||
<input type="hidden" name="initial-datetime_field_0" id="initial-id_datetime_field_0">
|
||||
<input type="hidden" name="initial-datetime_field_1" id="initial-id_datetime_field_1">
|
||||
</td></tr>
|
||||
"""
|
||||
)
|
||||
form = DateTimeRangeForm({
|
||||
|
@ -531,12 +533,31 @@ class TestFormField(PostgreSQLSimpleTestCase):
|
|||
value="2010-01-01 11:13:00" id="id_datetime_field_0">
|
||||
<input type="text" name="datetime_field_1"
|
||||
value="2020-12-12 16:59:00" id="id_datetime_field_1">
|
||||
<input type="hidden" name="initial-datetime_field"
|
||||
value="['2010-01-01 11:13:00', '2020-12-12 16:59:00']"
|
||||
id="initial-id_datetime_field"></td></tr>
|
||||
<input type="hidden" name="initial-datetime_field_0" value="2010-01-01 11:13:00"
|
||||
id="initial-id_datetime_field_0">
|
||||
<input type="hidden" name="initial-datetime_field_1" value="2020-12-12 16:59:00"
|
||||
id="initial-id_datetime_field_1"></td></tr>
|
||||
"""
|
||||
)
|
||||
|
||||
def test_datetime_form_initial_data(self):
|
||||
class DateTimeRangeForm(forms.Form):
|
||||
datetime_field = pg_forms.DateTimeRangeField(show_hidden_initial=True)
|
||||
|
||||
data = QueryDict(mutable=True)
|
||||
data.update({
|
||||
'datetime_field_0': '2010-01-01 11:13:00',
|
||||
'datetime_field_1': '',
|
||||
'initial-datetime_field_0': '2010-01-01 10:12:00',
|
||||
'initial-datetime_field_1': '',
|
||||
})
|
||||
form = DateTimeRangeForm(data=data)
|
||||
self.assertTrue(form.has_changed())
|
||||
|
||||
data['initial-datetime_field_0'] = '2010-01-01 11:13:00'
|
||||
form = DateTimeRangeForm(data=data)
|
||||
self.assertFalse(form.has_changed())
|
||||
|
||||
def test_rendering(self):
|
||||
class RangeForm(forms.Form):
|
||||
ints = pg_forms.IntegerRangeField()
|
||||
|
|
Loading…
Reference in New Issue