From 730173d1c5cf210d8e3bd951fa49f64b9bc561ca Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Tue, 7 Aug 2018 18:04:48 -0400 Subject: [PATCH] Fixed #29623 -- Fixed translation failure of DurationField's "overflow" error message. --- django/forms/fields.py | 12 +++++------- docs/releases/2.1.1.txt | 3 +++ tests/forms_tests/field_tests/test_durationfield.py | 10 ++++++++++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/django/forms/fields.py b/django/forms/fields.py index 370f78e8b5..6e19c79144 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -468,12 +468,7 @@ class DateTimeField(BaseTemporalField): class DurationField(Field): default_error_messages = { 'invalid': _('Enter a valid duration.'), - 'overflow': _( - 'The number of days must be between {min_days} and {max_days}.'.format( - min_days=datetime.timedelta.min.days, - max_days=datetime.timedelta.max.days, - ) - ) + 'overflow': _('The number of days must be between {min_days} and {max_days}.') } def prepare_value(self, value): @@ -489,7 +484,10 @@ class DurationField(Field): try: value = parse_duration(str(value)) except OverflowError: - raise ValidationError(self.error_messages['overflow'], code='overflow') + raise ValidationError(self.error_messages['overflow'].format( + min_days=datetime.timedelta.min.days, + max_days=datetime.timedelta.max.days, + ), code='overflow') if value is None: raise ValidationError(self.error_messages['invalid'], code='invalid') return value diff --git a/docs/releases/2.1.1.txt b/docs/releases/2.1.1.txt index f6e4bc567f..a24cbc47e2 100644 --- a/docs/releases/2.1.1.txt +++ b/docs/releases/2.1.1.txt @@ -21,3 +21,6 @@ Bugfixes * Fixed a regression in Django 2.0 where combining ``Q`` objects with ``__in`` lookups and lists crashed (:ticket:`29643`). + +* Fixed translation failure of ``DurationField``'s "overflow" error message + (:ticket:`29623`). diff --git a/tests/forms_tests/field_tests/test_durationfield.py b/tests/forms_tests/field_tests/test_durationfield.py index 4eac37c102..2c2e17acd3 100644 --- a/tests/forms_tests/field_tests/test_durationfield.py +++ b/tests/forms_tests/field_tests/test_durationfield.py @@ -3,6 +3,7 @@ import datetime from django.core.exceptions import ValidationError from django.forms import DurationField from django.test import SimpleTestCase +from django.utils import translation from django.utils.duration import duration_string from . import FormFieldAssertionsMixin @@ -31,6 +32,15 @@ class DurationFieldTest(FormFieldAssertionsMixin, SimpleTestCase): with self.assertRaisesMessage(ValidationError, msg): f.clean('-1000000000 00:00:00') + def test_overflow_translation(self): + msg = "Le nombre de jours doit ĂȘtre entre {min_days} et {max_days}.".format( + min_days=datetime.timedelta.min.days, + max_days=datetime.timedelta.max.days, + ) + with translation.override('fr'): + with self.assertRaisesMessage(ValidationError, msg): + DurationField().clean('1000000000 00:00:00') + def test_durationfield_render(self): self.assertWidgetRendersTo( DurationField(initial=datetime.timedelta(hours=1)),