From faf4b988fe75dd4045bc5c62496cc4f2e0db8c4d Mon Sep 17 00:00:00 2001 From: Nasir Hussain Date: Mon, 16 Sep 2019 22:29:13 +0500 Subject: [PATCH] Fixed #30758 -- Made RangeFields use multiple hidden inputs for initial data. --- django/contrib/postgres/forms/ranges.py | 33 +++++++++++++++---------- tests/postgres_tests/test_ranges.py | 31 +++++++++++++++++++---- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/django/contrib/postgres/forms/ranges.py b/django/contrib/postgres/forms/ranges.py index 47a16a1bbd..5a20975eb4 100644 --- a/django/contrib/postgres/forms/ranges.py +++ b/django/contrib/postgres/forms/ranges.py @@ -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 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) diff --git a/tests/postgres_tests/test_ranges.py b/tests/postgres_tests/test_ranges.py index c18c351031..3702221107 100644 --- a/tests/postgres_tests/test_ranges.py +++ b/tests/postgres_tests/test_ranges.py @@ -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): - + + + """ ) form = DateTimeRangeForm({ @@ -531,12 +533,31 @@ class TestFormField(PostgreSQLSimpleTestCase): value="2010-01-01 11:13:00" id="id_datetime_field_0"> - + + """ ) + 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()