Fixed #29623 -- Fixed translation failure of DurationField's "overflow" error message.

This commit is contained in:
Tim Graham 2018-08-07 18:04:48 -04:00
parent 9fee229874
commit 730173d1c5
3 changed files with 18 additions and 7 deletions

View File

@ -468,12 +468,7 @@ class DateTimeField(BaseTemporalField):
class DurationField(Field): class DurationField(Field):
default_error_messages = { default_error_messages = {
'invalid': _('Enter a valid duration.'), 'invalid': _('Enter a valid duration.'),
'overflow': _( 'overflow': _('The number of days must be between {min_days} and {max_days}.')
'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,
)
)
} }
def prepare_value(self, value): def prepare_value(self, value):
@ -489,7 +484,10 @@ class DurationField(Field):
try: try:
value = parse_duration(str(value)) value = parse_duration(str(value))
except OverflowError: 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: if value is None:
raise ValidationError(self.error_messages['invalid'], code='invalid') raise ValidationError(self.error_messages['invalid'], code='invalid')
return value return value

View File

@ -21,3 +21,6 @@ Bugfixes
* Fixed a regression in Django 2.0 where combining ``Q`` objects with ``__in`` * Fixed a regression in Django 2.0 where combining ``Q`` objects with ``__in``
lookups and lists crashed (:ticket:`29643`). lookups and lists crashed (:ticket:`29643`).
* Fixed translation failure of ``DurationField``'s "overflow" error message
(:ticket:`29623`).

View File

@ -3,6 +3,7 @@ import datetime
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.forms import DurationField from django.forms import DurationField
from django.test import SimpleTestCase from django.test import SimpleTestCase
from django.utils import translation
from django.utils.duration import duration_string from django.utils.duration import duration_string
from . import FormFieldAssertionsMixin from . import FormFieldAssertionsMixin
@ -31,6 +32,15 @@ class DurationFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
with self.assertRaisesMessage(ValidationError, msg): with self.assertRaisesMessage(ValidationError, msg):
f.clean('-1000000000 00:00:00') 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): def test_durationfield_render(self):
self.assertWidgetRendersTo( self.assertWidgetRendersTo(
DurationField(initial=datetime.timedelta(hours=1)), DurationField(initial=datetime.timedelta(hours=1)),