From c54f394904c3f93211b8aa073de4e9e50110f831 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 22 Sep 2014 13:57:45 +0100 Subject: Ensure 'messages' in fields are respected in preference to default validator messages --- rest_framework/compat.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'rest_framework/compat.py') diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 7c05bed9..2b4ddb02 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -121,6 +121,25 @@ else: return [m.upper() for m in self.http_method_names if hasattr(self, m)] + +# MinValueValidator and MaxValueValidator only accept `message` in 1.8+ +if django.VERSION >= (1, 8): + from django.core.validators import MinValueValidator, MaxValueValidator +else: + from django.core.validators import MinValueValidator as DjangoMinValueValidator + from django.core.validators import MaxValueValidator as DjangoMaxValueValidator + + class MinValueValidator(DjangoMinValueValidator): + def __init__(self, *args, **kwargs): + self.message = kwargs.pop('message', self.message) + super(MinValueValidator, self).__init__(*args, **kwargs) + + class MaxValueValidator(DjangoMaxValueValidator): + def __init__(self, *args, **kwargs): + self.message = kwargs.pop('message', self.message) + super(MaxValueValidator, self).__init__(*args, **kwargs) + + # PATCH method is not implemented by Django if 'patch' not in View.http_method_names: View.http_method_names = View.http_method_names + ['patch'] -- cgit v1.2.3 From e5f0a97595ff9280c7876fc917f6feb27b5ea95d Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 22 Sep 2014 16:45:06 +0100 Subject: More compat fixes --- rest_framework/compat.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'rest_framework/compat.py') 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: -- cgit v1.2.3 From dee3f78cb688b1bee892ef78d6eec23ccf67a80e Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 26 Sep 2014 17:06:20 +0100 Subject: FileField and ImageField --- rest_framework/compat.py | 9 --------- 1 file changed, 9 deletions(-) (limited to 'rest_framework/compat.py') diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 7303c32a..89af9b48 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -84,15 +84,6 @@ except ImportError: from collections import UserDict from collections import MutableMapping as DictMixin -# Try to import PIL in either of the two ways it can end up installed. -try: - from PIL import Image -except ImportError: - try: - import Image - except ImportError: - Image = None - def get_model_name(model_cls): try: -- cgit v1.2.3 From 381771731f48c75e7d5951e353049cceec386512 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 1 Oct 2014 13:09:14 +0100 Subject: Use six.text_type instead of str everywhere --- rest_framework/compat.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'rest_framework/compat.py') diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 89af9b48..3993cee6 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -5,11 +5,12 @@ versions of django/python, and compatibility wrappers around optional packages. # flake8: noqa from __future__ import unicode_literals -import django -import inspect + from django.core.exceptions import ImproperlyConfigured from django.conf import settings from django.utils import six +import django +import inspect # Handle django.utils.encoding rename in 1.5 onwards. @@ -177,12 +178,12 @@ class RequestFactory(DjangoRequestFactory): r = { 'PATH_INFO': self._get_path(parsed), 'QUERY_STRING': force_text(parsed[4]), - 'REQUEST_METHOD': str(method), + 'REQUEST_METHOD': six.text_type(method), } if data: r.update({ 'CONTENT_LENGTH': len(data), - 'CONTENT_TYPE': str(content_type), + 'CONTENT_TYPE': six.text_type(content_type), 'wsgi.input': FakePayload(data), }) elif django.VERSION <= (1, 4): -- cgit v1.2.3 From dfab9af294972720f59890967cd9ae1a6c0796b6 Mon Sep 17 00:00:00 2001 From: Craig de Stigter Date: Fri, 3 Oct 2014 08:41:18 +1300 Subject: Minor: fix spelling and grammar, mostly in 3.0 announcement --- rest_framework/compat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'rest_framework/compat.py') diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 3993cee6..e4e69580 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -131,7 +131,7 @@ else: self.message = kwargs.pop('message', self.message) super(MaxValueValidator, self).__init__(*args, **kwargs) -# URLValidator only accept `message` in 1.6+ +# URLValidator only accepts `message` in 1.6+ if django.VERSION >= (1, 6): from django.core.validators import URLValidator else: -- cgit v1.2.3 From 5d247a65c89594a7ab5ce2333612f23eadc6828d Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 9 Oct 2014 15:11:19 +0100 Subject: First pass on nested serializers in HTML --- rest_framework/compat.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'rest_framework/compat.py') diff --git a/rest_framework/compat.py b/rest_framework/compat.py index e4e69580..4ab23a4d 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -114,12 +114,15 @@ else: -# MinValueValidator and MaxValueValidator only accept `message` in 1.8+ +# MinValueValidator, MaxValueValidator et al. only accept `message` in 1.8+ if django.VERSION >= (1, 8): from django.core.validators import MinValueValidator, MaxValueValidator + from django.core.validators import MinLengthValidator, MaxLengthValidator else: from django.core.validators import MinValueValidator as DjangoMinValueValidator from django.core.validators import MaxValueValidator as DjangoMaxValueValidator + from django.core.validators import MinLengthValidator as DjangoMinLengthValidator + from django.core.validators import MaxLengthValidator as DjangoMaxLengthValidator class MinValueValidator(DjangoMinValueValidator): def __init__(self, *args, **kwargs): @@ -131,6 +134,17 @@ else: self.message = kwargs.pop('message', self.message) super(MaxValueValidator, self).__init__(*args, **kwargs) + class MinLengthValidator(DjangoMinLengthValidator): + def __init__(self, *args, **kwargs): + self.message = kwargs.pop('message', self.message) + super(MinLengthValidator, self).__init__(*args, **kwargs) + + class MaxLengthValidator(DjangoMaxLengthValidator): + def __init__(self, *args, **kwargs): + self.message = kwargs.pop('message', self.message) + super(MaxLengthValidator, self).__init__(*args, **kwargs) + + # URLValidator only accepts `message` in 1.6+ if django.VERSION >= (1, 6): from django.core.validators import URLValidator -- cgit v1.2.3 From cd40dcb06500424ef9016913a1a89db0deed8a1f Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 30 Oct 2014 16:53:12 +0000 Subject: Ensure json.dumps(separators=...) works on both 2.x and 3.x --- rest_framework/compat.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'rest_framework/compat.py') diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 4ab23a4d..0201bd83 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -291,6 +291,15 @@ except ImportError: oauth2_constants = None provider_now = None +# `seperators` argument to `json.dumps()` differs between 2.x and 3.x +# See: http://bugs.python.org/issue22767 +if six.PY3: + SHORT_SEPARATORS = (',', ':') + LONG_SEPARATORS = (', ', ': ') +else: + SHORT_SEPARATORS = (b',', b':') + LONG_SEPARATORS = (b', ', b': ') + # Handle lazy strings across Py2/Py3 from django.utils.functional import Promise -- cgit v1.2.3 From 4e001dbb7ac0bc13d6d5fbb4524e905184610aa2 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 6 Nov 2014 12:00:30 +0000 Subject: Drop usage of SortedDict. Closes #2027. --- rest_framework/compat.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'rest_framework/compat.py') diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 0201bd83..5bd85e74 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -26,6 +26,16 @@ except ImportError: from django.utils.encoding import force_unicode as force_text +# OrderedDict only available in Python 2.7. +# This will always be the case in Django 1.7 and above, as these versions +# no longer support Python 2.6. +# For Django <= 1.6 and Python 2.6 fall back to OrderedDict. +try: + from collections import OrderedDict +except: + from django.utils.datastructures import SortedDict as OrderedDict + + # HttpResponseBase only exists from 1.5 onwards try: from django.http.response import HttpResponseBase -- cgit v1.2.3