Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
5ba2368f
Commit
5ba2368f
authored
Apr 27, 2017
by
Tom Christie
Committed by
GitHub
Apr 27, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4987 from halfstrik/master
Added test for DateTimeField validation when server has timezone with…
parents
aafd0a64
b0a0c30b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
41 additions
and
3 deletions
+41
-3
rest_framework/compat.py
+9
-0
rest_framework/fields.py
+7
-2
tests/test_fields.py
+25
-1
No files found.
rest_framework/compat.py
View file @
5ba2368f
...
...
@@ -275,6 +275,14 @@ except ImportError:
def
pygments_css
(
style
):
return
None
try
:
import
pytz
from
pytz.exceptions
import
InvalidTimeError
except
ImportError
:
InvalidTimeError
=
Exception
# `separators` argument to `json.dumps()` differs between 2.x and 3.x
# See: http://bugs.python.org/issue22767
if
six
.
PY3
:
...
...
@@ -339,6 +347,7 @@ def set_many(instance, field, value):
field
=
getattr
(
instance
,
field
)
field
.
set
(
value
)
def
include
(
module
,
namespace
=
None
,
app_name
=
None
):
from
django.conf.urls
import
include
if
django
.
VERSION
<
(
1
,
9
):
...
...
rest_framework/fields.py
View file @
5ba2368f
...
...
@@ -33,7 +33,8 @@ from django.utils.translation import ugettext_lazy as _
from
rest_framework
import
ISO_8601
from
rest_framework.compat
import
(
get_remote_field
,
unicode_repr
,
unicode_to_repr
,
value_from_object
InvalidTimeError
,
get_remote_field
,
unicode_repr
,
unicode_to_repr
,
value_from_object
)
from
rest_framework.exceptions
import
ErrorDetail
,
ValidationError
from
rest_framework.settings
import
api_settings
...
...
@@ -1087,6 +1088,7 @@ class DateTimeField(Field):
default_error_messages
=
{
'invalid'
:
_
(
'Datetime has wrong format. Use one of these formats instead: {format}.'
),
'date'
:
_
(
'Expected a datetime but got a date.'
),
'make_aware'
:
_
(
'Invalid datetime for the timezone "{timezone}".'
)
}
datetime_parser
=
datetime
.
datetime
.
strptime
...
...
@@ -1107,7 +1109,10 @@ class DateTimeField(Field):
field_timezone
=
getattr
(
self
,
'timezone'
,
self
.
default_timezone
())
if
(
field_timezone
is
not
None
)
and
not
timezone
.
is_aware
(
value
):
return
timezone
.
make_aware
(
value
,
field_timezone
)
try
:
return
timezone
.
make_aware
(
value
,
field_timezone
)
except
InvalidTimeError
:
self
.
fail
(
'make_aware'
,
timezone
=
field_timezone
)
elif
(
field_timezone
is
None
)
and
timezone
.
is_aware
(
value
):
return
timezone
.
make_naive
(
value
,
utc
)
return
value
...
...
tests/test_fields.py
View file @
5ba2368f
...
...
@@ -12,7 +12,7 @@ from django.utils import six
from
django.utils.timezone
import
utc
import
rest_framework
from
rest_framework
import
serializers
from
rest_framework
import
compat
,
serializers
from
rest_framework.fields
import
is_simple_callable
try
:
...
...
@@ -1205,6 +1205,30 @@ class TestNaiveDateTimeField(FieldValues):
field
=
serializers
.
DateTimeField
(
default_timezone
=
None
)
class
TestNaiveDayLightSavingTimeTimeZoneDateTimeField
(
FieldValues
):
"""
Invalid values for `DateTimeField` with datetime in DST shift (non-existing or ambiguous) and timezone with DST.
Timezone America/New_York has DST shift from 2017-03-12T02:00:00 to 2017-03-12T03:00:00 and
from 2017-11-05T02:00:00 to 2017-11-05T01:00:00 in 2017.
"""
valid_inputs
=
{}
invalid_inputs
=
{
'2017-03-12T02:30:00'
:
[
'Invalid datetime for the timezone "America/New_York".'
],
'2017-11-05T01:30:00'
:
[
'Invalid datetime for the timezone "America/New_York".'
]
}
outputs
=
{}
class
MockTimezone
:
@staticmethod
def
localize
(
value
,
is_dst
):
raise
compat
.
InvalidTimeError
()
def
__str__
(
self
):
return
'America/New_York'
field
=
serializers
.
DateTimeField
(
default_timezone
=
MockTimezone
())
class
TestTimeField
(
FieldValues
):
"""
Valid and invalid values for `TimeField`.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment