Skip to content

Commit 5ba2368

Browse files
authored
Merge pull request #4987 from halfstrik/master
Added test for DateTimeField validation when server has timezone with…
2 parents aafd0a6 + b0a0c30 commit 5ba2368

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

rest_framework/compat.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,14 @@ def pygments_highlight(text, lang, style):
275275
def pygments_css(style):
276276
return None
277277

278+
279+
try:
280+
import pytz
281+
from pytz.exceptions import InvalidTimeError
282+
except ImportError:
283+
InvalidTimeError = Exception
284+
285+
278286
# `separators` argument to `json.dumps()` differs between 2.x and 3.x
279287
# See: http://bugs.python.org/issue22767
280288
if six.PY3:
@@ -339,6 +347,7 @@ def set_many(instance, field, value):
339347
field = getattr(instance, field)
340348
field.set(value)
341349

350+
342351
def include(module, namespace=None, app_name=None):
343352
from django.conf.urls import include
344353
if django.VERSION < (1,9):

rest_framework/fields.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333

3434
from rest_framework import ISO_8601
3535
from rest_framework.compat import (
36-
get_remote_field, unicode_repr, unicode_to_repr, value_from_object
36+
InvalidTimeError, get_remote_field, unicode_repr, unicode_to_repr,
37+
value_from_object
3738
)
3839
from rest_framework.exceptions import ErrorDetail, ValidationError
3940
from rest_framework.settings import api_settings
@@ -1087,6 +1088,7 @@ class DateTimeField(Field):
10871088
default_error_messages = {
10881089
'invalid': _('Datetime has wrong format. Use one of these formats instead: {format}.'),
10891090
'date': _('Expected a datetime but got a date.'),
1091+
'make_aware': _('Invalid datetime for the timezone "{timezone}".')
10901092
}
10911093
datetime_parser = datetime.datetime.strptime
10921094

@@ -1107,7 +1109,10 @@ def enforce_timezone(self, value):
11071109
field_timezone = getattr(self, 'timezone', self.default_timezone())
11081110

11091111
if (field_timezone is not None) and not timezone.is_aware(value):
1110-
return timezone.make_aware(value, field_timezone)
1112+
try:
1113+
return timezone.make_aware(value, field_timezone)
1114+
except InvalidTimeError:
1115+
self.fail('make_aware', timezone=field_timezone)
11111116
elif (field_timezone is None) and timezone.is_aware(value):
11121117
return timezone.make_naive(value, utc)
11131118
return value

tests/test_fields.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from django.utils.timezone import utc
1313

1414
import rest_framework
15-
from rest_framework import serializers
15+
from rest_framework import compat, serializers
1616
from rest_framework.fields import is_simple_callable
1717

1818
try:
@@ -1205,6 +1205,30 @@ class TestNaiveDateTimeField(FieldValues):
12051205
field = serializers.DateTimeField(default_timezone=None)
12061206

12071207

1208+
class TestNaiveDayLightSavingTimeTimeZoneDateTimeField(FieldValues):
1209+
"""
1210+
Invalid values for `DateTimeField` with datetime in DST shift (non-existing or ambiguous) and timezone with DST.
1211+
Timezone America/New_York has DST shift from 2017-03-12T02:00:00 to 2017-03-12T03:00:00 and
1212+
from 2017-11-05T02:00:00 to 2017-11-05T01:00:00 in 2017.
1213+
"""
1214+
valid_inputs = {}
1215+
invalid_inputs = {
1216+
'2017-03-12T02:30:00': ['Invalid datetime for the timezone "America/New_York".'],
1217+
'2017-11-05T01:30:00': ['Invalid datetime for the timezone "America/New_York".']
1218+
}
1219+
outputs = {}
1220+
1221+
class MockTimezone:
1222+
@staticmethod
1223+
def localize(value, is_dst):
1224+
raise compat.InvalidTimeError()
1225+
1226+
def __str__(self):
1227+
return 'America/New_York'
1228+
1229+
field = serializers.DateTimeField(default_timezone=MockTimezone())
1230+
1231+
12081232
class TestTimeField(FieldValues):
12091233
"""
12101234
Valid and invalid values for `TimeField`.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy