mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-05-22 21:46:08 +03:00
Added pytz exception in compat module.
Mock pytz.timezone localize in tests. Ref: #4986
This commit is contained in:
parent
e4a1bd140b
commit
b0a0c30bfe
|
@ -2,4 +2,3 @@
|
||||||
pytest==3.0.5
|
pytest==3.0.5
|
||||||
pytest-django==3.1.2
|
pytest-django==3.1.2
|
||||||
pytest-cov==2.4.0
|
pytest-cov==2.4.0
|
||||||
pytz==2016.10
|
|
||||||
|
|
|
@ -275,6 +275,14 @@ except ImportError:
|
||||||
def pygments_css(style):
|
def pygments_css(style):
|
||||||
return None
|
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
|
# `separators` argument to `json.dumps()` differs between 2.x and 3.x
|
||||||
# See: http://bugs.python.org/issue22767
|
# See: http://bugs.python.org/issue22767
|
||||||
if six.PY3:
|
if six.PY3:
|
||||||
|
@ -339,6 +347,7 @@ def set_many(instance, field, value):
|
||||||
field = getattr(instance, field)
|
field = getattr(instance, field)
|
||||||
field.set(value)
|
field.set(value)
|
||||||
|
|
||||||
|
|
||||||
def include(module, namespace=None, app_name=None):
|
def include(module, namespace=None, app_name=None):
|
||||||
from django.conf.urls import include
|
from django.conf.urls import include
|
||||||
if django.VERSION < (1,9):
|
if django.VERSION < (1,9):
|
||||||
|
|
|
@ -33,7 +33,8 @@ from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
from rest_framework import ISO_8601
|
from rest_framework import ISO_8601
|
||||||
from rest_framework.compat import (
|
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.exceptions import ErrorDetail, ValidationError
|
||||||
from rest_framework.settings import api_settings
|
from rest_framework.settings import api_settings
|
||||||
|
@ -1108,7 +1109,7 @@ class DateTimeField(Field):
|
||||||
if (field_timezone is not None) and not timezone.is_aware(value):
|
if (field_timezone is not None) and not timezone.is_aware(value):
|
||||||
try:
|
try:
|
||||||
return timezone.make_aware(value, field_timezone)
|
return timezone.make_aware(value, field_timezone)
|
||||||
except Exception:
|
except InvalidTimeError:
|
||||||
self.fail('make_aware', timezone=field_timezone)
|
self.fail('make_aware', timezone=field_timezone)
|
||||||
elif (field_timezone is None) and timezone.is_aware(value):
|
elif (field_timezone is None) and timezone.is_aware(value):
|
||||||
return timezone.make_naive(value, utc)
|
return timezone.make_naive(value, utc)
|
||||||
|
|
|
@ -9,10 +9,10 @@ import pytest
|
||||||
from django.http import QueryDict
|
from django.http import QueryDict
|
||||||
from django.test import TestCase, override_settings
|
from django.test import TestCase, override_settings
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
from django.utils.timezone import pytz, utc
|
from django.utils.timezone import utc
|
||||||
|
|
||||||
import rest_framework
|
import rest_framework
|
||||||
from rest_framework import serializers
|
from rest_framework import compat, serializers
|
||||||
from rest_framework.fields import is_simple_callable
|
from rest_framework.fields import is_simple_callable
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -1217,7 +1217,16 @@ class TestNaiveDayLightSavingTimeTimeZoneDateTimeField(FieldValues):
|
||||||
'2017-11-05T01:30:00': ['Invalid datetime for the timezone "America/New_York".']
|
'2017-11-05T01:30:00': ['Invalid datetime for the timezone "America/New_York".']
|
||||||
}
|
}
|
||||||
outputs = {}
|
outputs = {}
|
||||||
field = serializers.DateTimeField(default_timezone=pytz.timezone('America/New_York'))
|
|
||||||
|
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):
|
class TestTimeField(FieldValues):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user