From bf7fcc495b36a692570f6fe9210c96e746f75bf8 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 10 Jul 2017 10:14:31 +0100 Subject: [PATCH] Raise validation error on invalid timezone parsing. --- rest_framework/fields.py | 8 +++----- tests/test_fields.py | 1 + 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 769f11466..14b264ff9 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1137,18 +1137,16 @@ class DateTimeField(Field): if input_format.lower() == ISO_8601: try: parsed = parse_datetime(value) - except (ValueError, TypeError): - pass - else: if parsed is not None: return self.enforce_timezone(parsed) + except (ValueError, TypeError): + pass else: try: parsed = self.datetime_parser(value, input_format) + return self.enforce_timezone(parsed) except (ValueError, TypeError): pass - else: - return self.enforce_timezone(parsed) humanized_format = humanize_datetime.datetime_formats(input_formats) self.fail('invalid', format=humanized_format) diff --git a/tests/test_fields.py b/tests/test_fields.py index bb3d349b4..cf5721187 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -1152,6 +1152,7 @@ class TestDateTimeField(FieldValues): invalid_inputs = { 'abc': ['Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z].'], '2001-99-99T99:00': ['Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z].'], + '2018-08-16 22:00-24:00': ['Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z].'], datetime.date(2001, 1, 1): ['Expected a datetime but got a date.'], } outputs = {