mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-16 19:41:06 +03:00
More compat fixes
This commit is contained in:
parent
5586b6581d
commit
e5f0a97595
|
@ -139,6 +139,29 @@ else:
|
||||||
self.message = kwargs.pop('message', self.message)
|
self.message = kwargs.pop('message', self.message)
|
||||||
super(MaxValueValidator, self).__init__(*args, **kwargs)
|
super(MaxValueValidator, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
# URLValidator only accept `message` in 1.6+
|
||||||
|
if django.VERSION >= (1, 6):
|
||||||
|
from django.core.validators import URLValidator
|
||||||
|
else:
|
||||||
|
from django.core.validators import URLValidator as DjangoURLValidator
|
||||||
|
|
||||||
|
class URLValidator(DjangoURLValidator):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
self.message = kwargs.pop('message', self.message)
|
||||||
|
super(URLValidator, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
# EmailValidator requires explicit regex prior to 1.6+
|
||||||
|
if django.VERSION >= (1, 6):
|
||||||
|
from django.core.validators import EmailValidator
|
||||||
|
else:
|
||||||
|
from django.core.validators import EmailValidator as DjangoEmailValidator
|
||||||
|
from django.core.validators import email_re
|
||||||
|
|
||||||
|
class EmailValidator(DjangoEmailValidator):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(EmailValidator, self).__init__(email_re, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
# PATCH method is not implemented by Django
|
# PATCH method is not implemented by Django
|
||||||
if 'patch' not in View.http_method_names:
|
if 'patch' not in View.http_method_names:
|
||||||
|
|
|
@ -6,7 +6,7 @@ from django.utils.dateparse import parse_date, parse_datetime, parse_time
|
||||||
from django.utils.encoding import is_protected_type
|
from django.utils.encoding import is_protected_type
|
||||||
from django.utils.translation import ugettext_lazy as _
|
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 smart_text, MinValueValidator, MaxValueValidator
|
from rest_framework.compat import smart_text, EmailValidator, MinValueValidator, MaxValueValidator, URLValidator
|
||||||
from rest_framework.settings import api_settings
|
from rest_framework.settings import api_settings
|
||||||
from rest_framework.utils import html, representation, humanize_datetime
|
from rest_framework.utils import html, representation, humanize_datetime
|
||||||
import datetime
|
import datetime
|
||||||
|
@ -335,7 +335,7 @@ class EmailField(CharField):
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
super(EmailField, self).__init__(**kwargs)
|
super(EmailField, self).__init__(**kwargs)
|
||||||
validator = validators.EmailValidator(message=self.error_messages['invalid'])
|
validator = EmailValidator(message=self.error_messages['invalid'])
|
||||||
self.validators.append(validator)
|
self.validators.append(validator)
|
||||||
|
|
||||||
def to_internal_value(self, data):
|
def to_internal_value(self, data):
|
||||||
|
@ -381,7 +381,7 @@ class URLField(CharField):
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
super(URLField, self).__init__(**kwargs)
|
super(URLField, self).__init__(**kwargs)
|
||||||
validator = validators.URLValidator(message=self.error_messages['invalid'])
|
validator = URLValidator(message=self.error_messages['invalid'])
|
||||||
self.validators.append(validator)
|
self.validators.append(validator)
|
||||||
|
|
||||||
|
|
||||||
|
@ -525,7 +525,7 @@ class DecimalField(Field):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if not isinstance(value, decimal.Decimal):
|
if not isinstance(value, decimal.Decimal):
|
||||||
value = decimal.Decimal(value)
|
value = decimal.Decimal(str(value).strip())
|
||||||
|
|
||||||
context = decimal.getcontext().copy()
|
context = decimal.getcontext().copy()
|
||||||
context.prec = self.max_digits
|
context.prec = self.max_digits
|
||||||
|
|
|
@ -2,6 +2,7 @@ from decimal import Decimal
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from rest_framework import fields
|
from rest_framework import fields
|
||||||
import datetime
|
import datetime
|
||||||
|
import django
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
@ -92,7 +93,7 @@ class TestEmailField(FieldValues):
|
||||||
' example@example.com ': 'example@example.com',
|
' example@example.com ': 'example@example.com',
|
||||||
}
|
}
|
||||||
invalid_inputs = {
|
invalid_inputs = {
|
||||||
'example.com': ['Enter a valid email address.']
|
'examplecom': ['Enter a valid email address.']
|
||||||
}
|
}
|
||||||
outputs = {}
|
outputs = {}
|
||||||
field = fields.EmailField()
|
field = fields.EmailField()
|
||||||
|
@ -267,8 +268,8 @@ class TestMinMaxDecimalField(FieldValues):
|
||||||
Valid and invalid values for `DecimalField` with min and max limits.
|
Valid and invalid values for `DecimalField` with min and max limits.
|
||||||
"""
|
"""
|
||||||
valid_inputs = {
|
valid_inputs = {
|
||||||
'10.0': 10.0,
|
'10.0': Decimal('10.0'),
|
||||||
'20.0': 20.0,
|
'20.0': Decimal('20.0'),
|
||||||
}
|
}
|
||||||
invalid_inputs = {
|
invalid_inputs = {
|
||||||
'9.9': ['Ensure this value is greater than or equal to 10.'],
|
'9.9': ['Ensure this value is greater than or equal to 10.'],
|
||||||
|
@ -368,9 +369,10 @@ class TestDateTimeField(FieldValues):
|
||||||
'2001-01-01 13:00': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()),
|
'2001-01-01 13:00': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()),
|
||||||
'2001-01-01T13:00': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()),
|
'2001-01-01T13:00': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()),
|
||||||
'2001-01-01T13:00Z': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()),
|
'2001-01-01T13:00Z': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()),
|
||||||
'2001-01-01T14:00+0100': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()),
|
|
||||||
datetime.datetime(2001, 1, 1, 13, 00): datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()),
|
datetime.datetime(2001, 1, 1, 13, 00): datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()),
|
||||||
datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()): datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()),
|
datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()): datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC()),
|
||||||
|
# Note that 1.4 does not support timezone string parsing.
|
||||||
|
'2001-01-01T14:00+01:00' if (django.VERSION > (1, 4)) else '2001-01-01T13:00Z': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=timezone.UTC())
|
||||||
}
|
}
|
||||||
invalid_inputs = {
|
invalid_inputs = {
|
||||||
'abc': ['Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]'],
|
'abc': ['Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]'],
|
||||||
|
|
Loading…
Reference in New Issue
Block a user