diff options
Diffstat (limited to 'rest_framework')
| -rw-r--r-- | rest_framework/compat.py | 23 | ||||
| -rw-r--r-- | rest_framework/fields.py | 8 |
2 files changed, 27 insertions, 4 deletions
diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 2b4ddb02..7303c32a 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -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: diff --git a/rest_framework/fields.py b/rest_framework/fields.py index cbd3334a..12975ae4 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -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 |
