More compat fixes

This commit is contained in:
Tom Christie 2014-09-22 16:45:06 +01:00
parent 5586b6581d
commit e5f0a97595
3 changed files with 33 additions and 8 deletions

View File

@ -139,6 +139,29 @@ else:
self.message = kwargs.pop('message', self.message)
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
if 'patch' not in View.http_method_names:

View File

@ -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.translation import ugettext_lazy as _
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.utils import html, representation, humanize_datetime
import datetime
@ -335,7 +335,7 @@ class EmailField(CharField):
def __init__(self, **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)
def to_internal_value(self, data):
@ -381,7 +381,7 @@ class URLField(CharField):
def __init__(self, **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)
@ -525,7 +525,7 @@ class DecimalField(Field):
return None
if not isinstance(value, decimal.Decimal):
value = decimal.Decimal(value)
value = decimal.Decimal(str(value).strip())
context = decimal.getcontext().copy()
context.prec = self.max_digits

View File

@ -2,6 +2,7 @@ from decimal import Decimal
from django.utils import timezone
from rest_framework import fields
import datetime
import django
import pytest
@ -92,7 +93,7 @@ class TestEmailField(FieldValues):
' example@example.com ': 'example@example.com',
}
invalid_inputs = {
'example.com': ['Enter a valid email address.']
'examplecom': ['Enter a valid email address.']
}
outputs = {}
field = fields.EmailField()
@ -267,8 +268,8 @@ class TestMinMaxDecimalField(FieldValues):
Valid and invalid values for `DecimalField` with min and max limits.
"""
valid_inputs = {
'10.0': 10.0,
'20.0': 20.0,
'10.0': Decimal('10.0'),
'20.0': Decimal('20.0'),
}
invalid_inputs = {
'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-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-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, 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 = {
'abc': ['Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z]'],