diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py index 1e6277fb87..a3887740d8 100644 --- a/django/contrib/admin/widgets.py +++ b/django/contrib/admin/widgets.py @@ -213,17 +213,6 @@ class ManyToManyRawIdWidget(ForeignKeyRawIdWidget): if value: return value.split(',') - def _has_changed(self, initial, data): - if initial is None: - initial = [] - if data is None: - data = [] - if len(initial) != len(data): - return True - for pk1, pk2 in zip(initial, data): - if force_text(pk1) != force_text(pk2): - return True - return False class RelatedFieldWidgetWrapper(forms.Widget): """ @@ -279,9 +268,6 @@ class RelatedFieldWidgetWrapper(forms.Widget): def value_from_datadict(self, data, files, name): return self.widget.value_from_datadict(data, files, name) - def _has_changed(self, initial, data): - return self.widget._has_changed(initial, data) - def id_for_label(self, id_): return self.widget.id_for_label(id_) diff --git a/django/contrib/gis/admin/widgets.py b/django/contrib/gis/admin/widgets.py index f4379be7f3..a06933660f 100644 --- a/django/contrib/gis/admin/widgets.py +++ b/django/contrib/gis/admin/widgets.py @@ -7,7 +7,7 @@ from django.utils import six from django.utils import translation from django.contrib.gis.gdal import OGRException -from django.contrib.gis.geos import GEOSGeometry, GEOSException, fromstr +from django.contrib.gis.geos import GEOSGeometry, GEOSException # Creating a template context that contains Django settings # values needed by admin map templates. @@ -117,25 +117,3 @@ class OpenLayersWidget(Textarea): raise TypeError map_options[js_name] = value return map_options - - def _has_changed(self, initial, data): - """ Compare geographic value of data with its initial value. """ - - # Ensure we are dealing with a geographic object - if isinstance(initial, six.string_types): - try: - initial = GEOSGeometry(initial) - except (GEOSException, ValueError): - initial = None - - # Only do a geographic comparison if both values are available - if initial and data: - data = fromstr(data) - data.transform(initial.srid) - # If the initial value was not added by the browser, the geometry - # provided may be slightly different, the first time it is saved. - # The comparison is done with a very low tolerance. - return not initial.equals_exact(data, tolerance=0.000001) - else: - # Check for change of state of existence - return bool(initial) != bool(data) diff --git a/django/contrib/gis/forms/fields.py b/django/contrib/gis/forms/fields.py index cefb6830ba..ab2e37f1e1 100644 --- a/django/contrib/gis/forms/fields.py +++ b/django/contrib/gis/forms/fields.py @@ -1,11 +1,13 @@ from __future__ import unicode_literals from django import forms +from django.utils import six from django.utils.translation import ugettext_lazy as _ # While this couples the geographic forms to the GEOS library, # it decouples from database (by not importing SpatialBackend). -from django.contrib.gis.geos import GEOSException, GEOSGeometry +from django.contrib.gis.geos import GEOSException, GEOSGeometry, fromstr + class GeometryField(forms.Field): """ @@ -73,3 +75,25 @@ class GeometryField(forms.Field): raise forms.ValidationError(self.error_messages['transform_error']) return geom + + def _has_changed(self, initial, data): + """ Compare geographic value of data with its initial value. """ + + # Ensure we are dealing with a geographic object + if isinstance(initial, six.string_types): + try: + initial = GEOSGeometry(initial) + except (GEOSException, ValueError): + initial = None + + # Only do a geographic comparison if both values are available + if initial and data: + data = fromstr(data) + data.transform(initial.srid) + # If the initial value was not added by the browser, the geometry + # provided may be slightly different, the first time it is saved. + # The comparison is done with a very low tolerance. + return not initial.equals_exact(data, tolerance=0.000001) + else: + # Check for change of state of existence + return bool(initial) != bool(data) diff --git a/django/contrib/gis/tests/geoadmin/tests.py b/django/contrib/gis/tests/geoadmin/tests.py index 6fadebdb9a..669914bdea 100644 --- a/django/contrib/gis/tests/geoadmin/tests.py +++ b/django/contrib/gis/tests/geoadmin/tests.py @@ -38,7 +38,7 @@ class GeoAdminTest(TestCase): """ Check that changes are accurately noticed by OpenLayersWidget. """ geoadmin = admin.site._registry[City] form = geoadmin.get_changelist_form(None)() - has_changed = form.fields['point'].widget._has_changed + has_changed = form.fields['point']._has_changed initial = Point(13.4197458572965953, 52.5194108501149799, srid=4326) data_same = "SRID=3857;POINT(1493879.2754093995 6894592.019687599)" diff --git a/django/forms/extras/widgets.py b/django/forms/extras/widgets.py index c5ca1424c8..e939a8f665 100644 --- a/django/forms/extras/widgets.py +++ b/django/forms/extras/widgets.py @@ -135,11 +135,3 @@ class SelectDateWidget(Widget): s = Select(choices=choices) select_html = s.render(field % name, val, local_attrs) return select_html - - def _has_changed(self, initial, data): - try: - input_format = get_format('DATE_INPUT_FORMATS')[0] - data = datetime_safe.datetime.strptime(data, input_format).date() - except (TypeError, ValueError): - pass - return super(SelectDateWidget, self)._has_changed(initial, data) diff --git a/django/forms/fields.py b/django/forms/fields.py index 4438812a37..1e9cbcb4d9 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -175,6 +175,25 @@ class Field(object): """ return {} + def _has_changed(self, initial, data): + """ + Return True if data differs from initial. + """ + # For purposes of seeing whether something has changed, None is + # the same as an empty string, if the data or inital value we get + # is None, replace it w/ ''. + if data is None: + data_value = '' + else: + data_value = data + if initial is None: + initial_value = '' + else: + initial_value = initial + if force_text(initial_value) != force_text(data_value): + return True + return False + def __deepcopy__(self, memo): result = copy.copy(self) memo[id(self)] = result @@ -348,6 +367,13 @@ class BaseTemporalField(Field): def strptime(self, value, format): raise NotImplementedError('Subclasses must define this method.') + def _has_changed(self, initial, data): + try: + data = self.to_python(data) + except ValidationError: + return True + return self.to_python(initial) != data + class DateField(BaseTemporalField): widget = DateInput input_formats = formats.get_format_lazy('DATE_INPUT_FORMATS') @@ -371,6 +397,7 @@ class DateField(BaseTemporalField): def strptime(self, value, format): return datetime.datetime.strptime(value, format).date() + class TimeField(BaseTemporalField): widget = TimeInput input_formats = formats.get_format_lazy('TIME_INPUT_FORMATS') @@ -529,6 +556,12 @@ class FileField(Field): return initial return data + def _has_changed(self, initial, data): + if data is None: + return False + return True + + class ImageField(FileField): default_error_messages = { 'invalid_image': _("Upload a valid image. The file you uploaded was either not an image or a corrupted image."), @@ -618,6 +651,7 @@ class URLField(CharField): value = urlunsplit(url_fields) return value + class BooleanField(Field): widget = CheckboxInput @@ -636,6 +670,15 @@ class BooleanField(Field): raise ValidationError(self.error_messages['required']) return value + def _has_changed(self, initial, data): + # Sometimes data or initial could be None or '' which should be the + # same thing as False. + if initial == 'False': + # show_hidden_initial may have transformed False to 'False' + initial = False + return bool(initial) != bool(data) + + class NullBooleanField(BooleanField): """ A field whose valid values are None, True and False. Invalid values are @@ -660,6 +703,15 @@ class NullBooleanField(BooleanField): def validate(self, value): pass + def _has_changed(self, initial, data): + # None (unknown) and False (No) are not the same + if initial is not None: + initial = bool(initial) + if data is not None: + data = bool(data) + return initial != data + + class ChoiceField(Field): widget = Select default_error_messages = { @@ -739,6 +791,7 @@ class TypedChoiceField(ChoiceField): def validate(self, value): pass + class MultipleChoiceField(ChoiceField): hidden_widget = MultipleHiddenInput widget = SelectMultiple @@ -765,6 +818,18 @@ class MultipleChoiceField(ChoiceField): if not self.valid_value(val): raise ValidationError(self.error_messages['invalid_choice'] % {'value': val}) + def _has_changed(self, initial, data): + if initial is None: + initial = [] + if data is None: + data = [] + if len(initial) != len(data): + return True + initial_set = set([force_text(value) for value in initial]) + data_set = set([force_text(value) for value in data]) + return data_set != initial_set + + class TypedMultipleChoiceField(MultipleChoiceField): def __init__(self, *args, **kwargs): self.coerce = kwargs.pop('coerce', lambda val: val) @@ -899,6 +964,18 @@ class MultiValueField(Field): """ raise NotImplementedError('Subclasses must implement this method.') + def _has_changed(self, initial, data): + if initial is None: + initial = ['' for x in range(0, len(data))] + else: + if not isinstance(initial, list): + initial = self.widget.decompress(initial) + for field, initial, data in zip(self.fields, initial, data): + if field._has_changed(initial, data): + return True + return False + + class FilePathField(ChoiceField): def __init__(self, path, match=None, recursive=False, allow_files=True, allow_folders=False, required=True, widget=None, label=None, diff --git a/django/forms/forms.py b/django/forms/forms.py index 3299c2becc..f532391296 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -341,7 +341,13 @@ class BaseForm(object): hidden_widget = field.hidden_widget() initial_value = hidden_widget.value_from_datadict( self.data, self.files, initial_prefixed_name) - if field.widget._has_changed(initial_value, data_value): + if hasattr(field.widget, '_has_changed'): + warnings.warn("The _has_changed method on widgets is deprecated," + " define it at field level instead.", + PendingDeprecationWarning, stacklevel=2) + if field.widget._has_changed(initial_value, data_value): + self._changed_data.append(name) + elif field._has_changed(initial_value, data_value): self._changed_data.append(name) return self._changed_data changed_data = property(_get_changed_data) diff --git a/django/forms/models.py b/django/forms/models.py index 03a14dc9ff..837da74814 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -858,15 +858,12 @@ def inlineformset_factory(parent_model, model, form=ModelForm, # Fields ##################################################################### -class InlineForeignKeyHiddenInput(HiddenInput): - def _has_changed(self, initial, data): - return False - class InlineForeignKeyField(Field): """ A basic integer field that deals with validating the given value to a given parent instance in an inline. """ + widget = HiddenInput default_error_messages = { 'invalid_choice': _('The inline foreign key did not match the parent instance primary key.'), } @@ -881,7 +878,6 @@ class InlineForeignKeyField(Field): else: kwargs["initial"] = self.parent_instance.pk kwargs["required"] = False - kwargs["widget"] = InlineForeignKeyHiddenInput super(InlineForeignKeyField, self).__init__(*args, **kwargs) def clean(self, value): @@ -899,6 +895,9 @@ class InlineForeignKeyField(Field): raise ValidationError(self.error_messages['invalid_choice']) return self.parent_instance + def _has_changed(self, initial, data): + return False + class ModelChoiceIterator(object): def __init__(self, field): self.field = field diff --git a/django/forms/widgets.py b/django/forms/widgets.py index d6ea56f0c8..303844d44b 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -208,25 +208,6 @@ class Widget(six.with_metaclass(MediaDefiningClass)): """ return data.get(name, None) - def _has_changed(self, initial, data): - """ - Return True if data differs from initial. - """ - # For purposes of seeing whether something has changed, None is - # the same as an empty string, if the data or inital value we get - # is None, replace it w/ ''. - if data is None: - data_value = '' - else: - data_value = data - if initial is None: - initial_value = '' - else: - initial_value = initial - if force_text(initial_value) != force_text(data_value): - return True - return False - def id_for_label(self, id_): """ Returns the HTML ID attribute of this Widget for use by a """) - # Test the usage of _has_changed - self.assertFalse(w._has_changed(None, None)) - self.assertFalse(w._has_changed([], None)) - self.assertTrue(w._has_changed(None, ['1'])) - self.assertFalse(w._has_changed([1, 2], ['1', '2'])) - self.assertTrue(w._has_changed([1, 2], ['1'])) - self.assertTrue(w._has_changed([1, 2], ['1', '3'])) - self.assertFalse(w._has_changed([2, 1], ['1', '2'])) - # Unicode choices are correctly rendered as HTML self.assertHTMLEqual(w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]), '') @@ -886,21 +833,6 @@ beatle J R Ringo False""") w = MyMultiWidget(widgets=(TextInput(attrs={'class': 'big'}), TextInput(attrs={'class': 'small'})), attrs={'id': 'bar'}) self.assertHTMLEqual(w.render('name', ['john', 'lennon']), '
') - w = MyMultiWidget(widgets=(TextInput(), TextInput())) - - # test with no initial data - self.assertTrue(w._has_changed(None, ['john', 'lennon'])) - - # test when the data is the same as initial - self.assertFalse(w._has_changed('john__lennon', ['john', 'lennon'])) - - # test when the first widget's data has changed - self.assertTrue(w._has_changed('john__lennon', ['alfred', 'lennon'])) - - # test when the last widget's data has changed. this ensures that it is not - # short circuiting while testing the widgets. - self.assertTrue(w._has_changed('john__lennon', ['john', 'denver'])) - def test_splitdatetime(self): w = SplitDateTimeWidget() self.assertHTMLEqual(w.render('date', ''), '') @@ -916,10 +848,6 @@ beatle J R Ringo False""") w = SplitDateTimeWidget(date_format='%d/%m/%Y', time_format='%H:%M') self.assertHTMLEqual(w.render('date', datetime.datetime(2006, 1, 10, 7, 30)), '') - self.assertTrue(w._has_changed(datetime.datetime(2008, 5, 6, 12, 40, 00), ['2008-05-06', '12:40:00'])) - self.assertFalse(w._has_changed(datetime.datetime(2008, 5, 6, 12, 40, 00), ['06/05/2008', '12:40'])) - self.assertTrue(w._has_changed(datetime.datetime(2008, 5, 6, 12, 40, 00), ['06/05/2008', '12:41'])) - def test_datetimeinput(self): w = DateTimeInput() self.assertHTMLEqual(w.render('date', None), '') @@ -934,13 +862,6 @@ beatle J R Ringo False""") # Use 'format' to change the way a value is displayed. w = DateTimeInput(format='%d/%m/%Y %H:%M', attrs={'type': 'datetime'}) self.assertHTMLEqual(w.render('date', d), '') - self.assertFalse(w._has_changed(d, '17/09/2007 12:51')) - - # Make sure a custom format works with _has_changed. The hidden input will use - data = datetime.datetime(2010, 3, 6, 12, 0, 0) - custom_format = '%d.%m.%Y %H:%M' - w = DateTimeInput(format=custom_format) - self.assertFalse(w._has_changed(formats.localize_input(data), data.strftime(custom_format))) def test_dateinput(self): w = DateInput() @@ -957,13 +878,6 @@ beatle J R Ringo False""") # Use 'format' to change the way a value is displayed. w = DateInput(format='%d/%m/%Y', attrs={'type': 'date'}) self.assertHTMLEqual(w.render('date', d), '') - self.assertFalse(w._has_changed(d, '17/09/2007')) - - # Make sure a custom format works with _has_changed. The hidden input will use - data = datetime.date(2010, 3, 6) - custom_format = '%d.%m.%Y' - w = DateInput(format=custom_format) - self.assertFalse(w._has_changed(formats.localize_input(data), data.strftime(custom_format))) def test_timeinput(self): w = TimeInput() @@ -982,13 +896,6 @@ beatle J R Ringo False""") # Use 'format' to change the way a value is displayed. w = TimeInput(format='%H:%M', attrs={'type': 'time'}) self.assertHTMLEqual(w.render('time', t), '') - self.assertFalse(w._has_changed(t, '12:51')) - - # Make sure a custom format works with _has_changed. The hidden input will use - data = datetime.time(13, 0) - custom_format = '%I:%M %p' - w = TimeInput(format=custom_format) - self.assertFalse(w._has_changed(formats.localize_input(data), data.strftime(custom_format))) def test_splithiddendatetime(self): from django.forms.widgets import SplitHiddenDateTimeWidget @@ -1016,10 +923,6 @@ class FormsI18NWidgetsTestCase(TestCase): deactivate() super(FormsI18NWidgetsTestCase, self).tearDown() - def test_splitdatetime(self): - w = SplitDateTimeWidget(date_format='%d/%m/%Y', time_format='%H:%M') - self.assertTrue(w._has_changed(datetime.datetime(2008, 5, 6, 12, 40, 00), ['06.05.2008', '12:41'])) - def test_datetimeinput(self): w = DateTimeInput() d = datetime.datetime(2007, 9, 17, 12, 51, 34, 482548)