From 71ccab593b7ea7e3a1ab5dd971365c57822454ae Mon Sep 17 00:00:00 2001 From: Joel Marcotte Date: Sat, 15 Dec 2012 10:35:06 -0500 Subject: [PATCH 01/11] Fix for JSON integer match to a ChoiceField --- rest_framework/fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index da588082c..903c384e3 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -794,7 +794,7 @@ class ChoiceField(WritableField): if value == smart_unicode(k2): return True else: - if value == smart_unicode(k): + if value == smart_unicode(k) or value == k: return True return False From 01e06bcdf8a4678a312acbf11638fa6a033c50d6 Mon Sep 17 00:00:00 2001 From: Joel Marcotte Date: Sat, 15 Dec 2012 16:33:08 -0500 Subject: [PATCH 02/11] Added test for "positive_integer in choices tuple does not get parsed if not string". Signed-off-by: Joel Marcotte --- rest_framework/tests/models.py | 4 ++++ rest_framework/tests/serializer.py | 13 ++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/rest_framework/tests/models.py b/rest_framework/tests/models.py index 428bf130d..807bcf983 100644 --- a/rest_framework/tests/models.py +++ b/rest_framework/tests/models.py @@ -51,6 +51,10 @@ class RESTFrameworkModel(models.Model): abstract = True +class HasPositiveIntegerAsChoice(RESTFrameworkModel): + some_choices = ((1,'A'),(2,'B'),(3,'C')) + some_integer = models.PositiveIntegerField(choices=some_choices) + class Anchor(RESTFrameworkModel): text = models.CharField(max_length=100, default='anchor') diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py index 780177aa0..7f2c27b05 100644 --- a/rest_framework/tests/serializer.py +++ b/rest_framework/tests/serializer.py @@ -2,7 +2,7 @@ import datetime import pickle from django.test import TestCase from rest_framework import serializers -from rest_framework.tests.models import (Album, ActionItem, Anchor, BasicModel, +from rest_framework.tests.models import (HasPositiveIntegerAsChoice, Album, ActionItem, Anchor, BasicModel, BlankFieldModel, BlogPost, Book, CallableDefaultValueModel, DefaultValueModel, ManyToManyModel, Person, ReadOnlyManyToManyModel, Photo) @@ -69,6 +69,11 @@ class AlbumsSerializer(serializers.ModelSerializer): model = Album fields = ['title'] # lists are also valid options +class PositiveIntegerAsChoiceSerializer(serializers.ModelSerializer): + class Meta: + model = HasPositiveIntegerAsChoice + fields = ['some_integer'] + class BasicTests(TestCase): def setUp(self): @@ -285,6 +290,12 @@ class ValidationTests(TestCase): self.assertEquals(serializer.errors, {'info': [u'Ensure this value has at most 12 characters (it has 13).']}) +class PositiveIntegerAsChoiceTests(TestCase): + def test_positive_integer_in_json_is_correctly_parsed(self): + data = {'some_integer':1} + serializer = PositiveIntegerAsChoiceSerializer(data=data) + self.assertEquals(serializer.is_valid(), True) + class ModelValidationTests(TestCase): def test_validate_unique(self): """ From 6f25181979084e769658748ea342ff088ad245c0 Mon Sep 17 00:00:00 2001 From: Joel Marcotte Date: Sat, 15 Dec 2012 16:45:04 -0500 Subject: [PATCH 03/11] Reverting commit to previous state to see if the test is only relevant to django 1.5b2 --- rest_framework/fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 903c384e3..da588082c 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -794,7 +794,7 @@ class ChoiceField(WritableField): if value == smart_unicode(k2): return True else: - if value == smart_unicode(k) or value == k: + if value == smart_unicode(k): return True return False From 262d9c248918d1e9a2e6ee8008aca94e2e23dd82 Mon Sep 17 00:00:00 2001 From: Joel Marcotte Date: Sat, 15 Dec 2012 16:52:28 -0500 Subject: [PATCH 04/11] Final commit to restore the fix Signed-off-by: Joel Marcotte --- rest_framework/fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index da588082c..903c384e3 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -794,7 +794,7 @@ class ChoiceField(WritableField): if value == smart_unicode(k2): return True else: - if value == smart_unicode(k): + if value == smart_unicode(k) or value == k: return True return False From d90d5380d730b992b1b59aaf9d1f89fbbbba0f9f Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 19 Dec 2012 22:05:00 +0000 Subject: [PATCH 05/11] pep8 --- rest_framework/tests/renderers.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/rest_framework/tests/renderers.py b/rest_framework/tests/renderers.py index 9be4b1146..fb843676c 100644 --- a/rest_framework/tests/renderers.py +++ b/rest_framework/tests/renderers.py @@ -444,19 +444,19 @@ class CacheRenderTest(TestCase): return if state == None: return - if isinstance(state,tuple): - if not isinstance(state[0],dict): - state=state[1] + if isinstance(state, tuple): + if not isinstance(state[0], dict): + state = state[1] else: - state=state[0].update(state[1]) + state = state[0].update(state[1]) result = {} for i in state: try: - pickle.dumps(state[i],protocol=2) + pickle.dumps(state[i], protocol=2) except pickle.PicklingError: if not state[i] in seen: seen.append(state[i]) - result[i] = cls._get_pickling_errors(state[i],seen) + result[i] = cls._get_pickling_errors(state[i], seen) return result def http_resp(self, http_method, url): From 598ae3286ac6343a59e6d80fc93428539c5f836e Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 19 Dec 2012 22:05:35 +0000 Subject: [PATCH 06/11] Fix #521. (Browseable API exception on delete) --- rest_framework/mixins.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py index 8dc0c3295..2700606d0 100644 --- a/rest_framework/mixins.py +++ b/rest_framework/mixins.py @@ -124,6 +124,6 @@ class DestroyModelMixin(object): Should be mixed in with `SingleObjectBaseView`. """ def destroy(self, request, *args, **kwargs): - self.object = self.get_object() - self.object.delete() + obj = self.get_object() + obj.delete() return Response(status=status.HTTP_204_NO_CONTENT) From c29b08ad43c8de5f295176eaf0270427f3a737f3 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 19 Dec 2012 22:06:38 +0000 Subject: [PATCH 07/11] Update release notes --- docs/topics/release-notes.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/topics/release-notes.md b/docs/topics/release-notes.md index 87aaefbdb..c75f08790 100644 --- a/docs/topics/release-notes.md +++ b/docs/topics/release-notes.md @@ -8,6 +8,7 @@ ### Master +* Bugfix: Fix exception in browseable API on DELETE. * Bugfix: Fix issue where pk was was being set to a string if set by URL kwarg. ### 2.1.11 From 566b9ff27b93dfa089d054552a94d930e3f17c67 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 19 Dec 2012 22:41:00 +0000 Subject: [PATCH 08/11] Added @joual - Thanks! --- docs/topics/credits.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/topics/credits.md b/docs/topics/credits.md index c169fd74e..fa378cb28 100644 --- a/docs/topics/credits.md +++ b/docs/topics/credits.md @@ -79,6 +79,7 @@ The following people have helped make REST framework great. * Colin Murtaugh - [cmurtaugh] * Simon Pantzare - [pilt] * Szymon Teżewski - [sunscrapers] +* Joel Marcotte - [joual] Many thanks to everyone who's contributed to the project. @@ -193,3 +194,4 @@ You can also contact [@_tomchristie][twitter] directly on twitter. [cmurtaugh]: https://github.com/cmurtaugh [pilt]: https://github.com/pilt [sunscrapers]: https://github.com/sunscrapers +[joual]: https://github.com/joual From c097bcef586da4513d1d6f357d9eb3d7b4b0fffb Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 19 Dec 2012 22:42:11 +0000 Subject: [PATCH 09/11] Update release notes. --- docs/topics/release-notes.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/topics/release-notes.md b/docs/topics/release-notes.md index c75f08790..741d908cf 100644 --- a/docs/topics/release-notes.md +++ b/docs/topics/release-notes.md @@ -8,6 +8,7 @@ ### Master +* Bugfix: Fix bug that could occur using ChoiceField. * Bugfix: Fix exception in browseable API on DELETE. * Bugfix: Fix issue where pk was was being set to a string if set by URL kwarg. From a493c83248535c9fa7f78815b16bce7e88bf7966 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 19 Dec 2012 23:12:27 +0000 Subject: [PATCH 10/11] urls, patterns, include imports move to compat to support incoming 1.3 thru 1.6 import compatability --- rest_framework/compat.py | 6 ++++++ rest_framework/tests/authentication.py | 10 ++++------ rest_framework/tests/breadcrumbs.py | 2 +- rest_framework/tests/decorators.py | 2 +- rest_framework/tests/htmlrenderer.py | 2 +- rest_framework/tests/hyperlink_relations.py | 2 +- rest_framework/tests/hyperlinkedserializers.py | 2 +- rest_framework/tests/models.py | 5 +++-- rest_framework/tests/renderers.py | 3 +-- rest_framework/tests/request.py | 5 ++--- rest_framework/tests/response.py | 5 +---- rest_framework/tests/reverse.py | 2 +- rest_framework/tests/serializer.py | 4 ++++ rest_framework/tests/testcases.py | 6 ++++-- rest_framework/urls.py | 2 +- 15 files changed, 32 insertions(+), 26 deletions(-) diff --git a/rest_framework/compat.py b/rest_framework/compat.py index d4901437d..86952fb8d 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -5,6 +5,12 @@ versions of django/python, and compatibility wrappers around optional packages. # flake8: noqa import django +# location of patterns, url, include changes in 1.4 onwards +try: + from django.conf.urls import patterns, url, include +except: + from django.conf.urls.defaults import patterns, url, include + # django-filter is optional try: import django_filters diff --git a/rest_framework/tests/authentication.py b/rest_framework/tests/authentication.py index d498ae3e0..838e081bb 100644 --- a/rest_framework/tests/authentication.py +++ b/rest_framework/tests/authentication.py @@ -1,15 +1,13 @@ -from django.conf.urls.defaults import patterns from django.contrib.auth.models import User -from django.test import Client, TestCase - -from django.utils import simplejson as json from django.http import HttpResponse +from django.test import Client, TestCase +from django.utils import simplejson as json -from rest_framework.views import APIView from rest_framework import permissions - from rest_framework.authtoken.models import Token from rest_framework.authentication import TokenAuthentication +from rest_framework.compat import patterns +from rest_framework.views import APIView import base64 diff --git a/rest_framework/tests/breadcrumbs.py b/rest_framework/tests/breadcrumbs.py index 647ab96d0..df8916832 100644 --- a/rest_framework/tests/breadcrumbs.py +++ b/rest_framework/tests/breadcrumbs.py @@ -1,5 +1,5 @@ -from django.conf.urls.defaults import patterns, url from django.test import TestCase +from rest_framework.compat import patterns, url from rest_framework.utils.breadcrumbs import get_breadcrumbs from rest_framework.views import APIView diff --git a/rest_framework/tests/decorators.py b/rest_framework/tests/decorators.py index 41864d71e..8079c8cb3 100644 --- a/rest_framework/tests/decorators.py +++ b/rest_framework/tests/decorators.py @@ -1,7 +1,7 @@ from django.test import TestCase +from django.test.client import RequestFactory from rest_framework import status from rest_framework.response import Response -from django.test.client import RequestFactory from rest_framework.renderers import JSONRenderer from rest_framework.parsers import JSONParser from rest_framework.authentication import BasicAuthentication diff --git a/rest_framework/tests/htmlrenderer.py b/rest_framework/tests/htmlrenderer.py index 4caed59ee..54096206d 100644 --- a/rest_framework/tests/htmlrenderer.py +++ b/rest_framework/tests/htmlrenderer.py @@ -1,9 +1,9 @@ from django.core.exceptions import PermissionDenied -from django.conf.urls.defaults import patterns, url from django.http import Http404 from django.test import TestCase from django.template import TemplateDoesNotExist, Template import django.template.loader +from rest_framework.compat import patterns, url from rest_framework.decorators import api_view, renderer_classes from rest_framework.renderers import TemplateHTMLRenderer from rest_framework.response import Response diff --git a/rest_framework/tests/hyperlink_relations.py b/rest_framework/tests/hyperlink_relations.py index 8f0238737..9e8ecf709 100644 --- a/rest_framework/tests/hyperlink_relations.py +++ b/rest_framework/tests/hyperlink_relations.py @@ -1,7 +1,7 @@ -from django.conf.urls import patterns, url from django.db import models from django.test import TestCase from rest_framework import serializers +from rest_framework.compat import patterns, url def dummy_view(request, pk): diff --git a/rest_framework/tests/hyperlinkedserializers.py b/rest_framework/tests/hyperlinkedserializers.py index 24bf61bf8..ee4d8e577 100644 --- a/rest_framework/tests/hyperlinkedserializers.py +++ b/rest_framework/tests/hyperlinkedserializers.py @@ -1,8 +1,8 @@ -from django.conf.urls.defaults import patterns, url from django.test import TestCase from django.test.client import RequestFactory from django.utils import simplejson as json from rest_framework import generics, status, serializers +from rest_framework.compat import patterns, url from rest_framework.tests.models import Anchor, BasicModel, ManyToManyModel, BlogPost, BlogPostComment, Album, Photo, OptionalRelationModel factory = RequestFactory() diff --git a/rest_framework/tests/models.py b/rest_framework/tests/models.py index 1b1def30d..0759650ab 100644 --- a/rest_framework/tests/models.py +++ b/rest_framework/tests/models.py @@ -52,9 +52,10 @@ class RESTFrameworkModel(models.Model): class HasPositiveIntegerAsChoice(RESTFrameworkModel): - some_choices = ((1,'A'),(2,'B'),(3,'C')) + some_choices = ((1, 'A'), (2, 'B'), (3, 'C')) some_integer = models.PositiveIntegerField(choices=some_choices) + class Anchor(RESTFrameworkModel): text = models.CharField(max_length=100, default='anchor') @@ -164,7 +165,7 @@ class Photo(RESTFrameworkModel): # Model for issue #324 class BlankFieldModel(RESTFrameworkModel): - title = models.CharField(max_length=100, blank=True, null=True) + title = models.CharField(max_length=100, blank=True, null=False) # Model for issue #380 diff --git a/rest_framework/tests/renderers.py b/rest_framework/tests/renderers.py index fb843676c..c1b4e624b 100644 --- a/rest_framework/tests/renderers.py +++ b/rest_framework/tests/renderers.py @@ -1,13 +1,12 @@ import pickle import re -from django.conf.urls.defaults import patterns, url, include from django.core.cache import cache from django.test import TestCase from django.test.client import RequestFactory from rest_framework import status, permissions -from rest_framework.compat import yaml +from rest_framework.compat import yaml, patterns, url, include from rest_framework.response import Response from rest_framework.views import APIView from rest_framework.renderers import BaseRenderer, JSONRenderer, YAMLRenderer, \ diff --git a/rest_framework/tests/request.py b/rest_framework/tests/request.py index 2850992d2..2eb34ceaa 100644 --- a/rest_framework/tests/request.py +++ b/rest_framework/tests/request.py @@ -1,16 +1,15 @@ """ Tests for content parsing, and form-overloaded content parsing. """ -from django.conf.urls.defaults import patterns from django.contrib.auth.models import User from django.contrib.auth import authenticate, login, logout from django.contrib.sessions.middleware import SessionMiddleware from django.test import TestCase, Client +from django.test.client import RequestFactory from django.utils import simplejson as json - from rest_framework import status from rest_framework.authentication import SessionAuthentication -from django.test.client import RequestFactory +from rest_framework.compat import patterns from rest_framework.parsers import ( BaseParser, FormParser, diff --git a/rest_framework/tests/response.py b/rest_framework/tests/response.py index d7b75450c..875f4d422 100644 --- a/rest_framework/tests/response.py +++ b/rest_framework/tests/response.py @@ -1,8 +1,5 @@ -import unittest - -from django.conf.urls.defaults import patterns, url, include from django.test import TestCase - +from rest_framework.compat import patterns, url, include from rest_framework.response import Response from rest_framework.views import APIView from rest_framework import status diff --git a/rest_framework/tests/reverse.py b/rest_framework/tests/reverse.py index fd9a7d64e..8c86e1fbe 100644 --- a/rest_framework/tests/reverse.py +++ b/rest_framework/tests/reverse.py @@ -1,6 +1,6 @@ -from django.conf.urls.defaults import patterns, url from django.test import TestCase from django.test.client import RequestFactory +from rest_framework.compat import patterns, url from rest_framework.reverse import reverse factory = RequestFactory() diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py index 7f2c27b05..6ea4b4246 100644 --- a/rest_framework/tests/serializer.py +++ b/rest_framework/tests/serializer.py @@ -699,6 +699,10 @@ class BlankFieldTests(TestCase): serializer = self.model_serializer_class(data=self.data) self.assertEquals(serializer.is_valid(), True) + def test_create_model_null_field(self): + serializer = self.model_serializer_class(data={'title': None}) + self.assertEquals(serializer.is_valid(), True) + def test_create_not_blank_field(self): """ Test to ensure blank data in a field not marked as blank=True diff --git a/rest_framework/tests/testcases.py b/rest_framework/tests/testcases.py index c90224aa6..97f492ff4 100644 --- a/rest_framework/tests/testcases.py +++ b/rest_framework/tests/testcases.py @@ -6,6 +6,7 @@ from django.test import TestCase NO_SETTING = ('!', None) + class TestSettingsManager(object): """ A class which can modify some Django settings temporarily for a @@ -19,7 +20,7 @@ class TestSettingsManager(object): self._original_settings = {} def set(self, **kwargs): - for k,v in kwargs.iteritems(): + for k, v in kwargs.iteritems(): self._original_settings.setdefault(k, getattr(settings, k, NO_SETTING)) setattr(settings, k, v) @@ -31,7 +32,7 @@ class TestSettingsManager(object): call_command('syncdb', verbosity=0) def revert(self): - for k,v in self._original_settings.iteritems(): + for k, v in self._original_settings.iteritems(): if v == NO_SETTING: delattr(settings, k) else: @@ -57,6 +58,7 @@ class SettingsTestCase(TestCase): def tearDown(self): self.settings_manager.revert() + class TestModelsTestCase(SettingsTestCase): def setUp(self, *args, **kwargs): installed_apps = tuple(settings.INSTALLED_APPS) + ('rest_framework.tests',) diff --git a/rest_framework/urls.py b/rest_framework/urls.py index bcdc23e74..fbe4bc07e 100644 --- a/rest_framework/urls.py +++ b/rest_framework/urls.py @@ -12,7 +12,7 @@ your authentication settings include `SessionAuthentication`. url(r'^auth', include('rest_framework.urls', namespace='rest_framework')) ) """ -from django.conf.urls.defaults import patterns, url +from rest_framework.compat import patterns, url template_name = {'template_name': 'rest_framework/login.html'} From c27295dcbdf89b3cbc10d6325833e428c66c0f2a Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 19 Dec 2012 23:12:57 +0000 Subject: [PATCH 11/11] Update minor Django versions in tox --- tox.ini | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tox.ini b/tox.ini index 69eb38237..22c85e493 100644 --- a/tox.ini +++ b/tox.ini @@ -12,12 +12,12 @@ deps = https://github.com/django/django/zipball/master [testenv:py2.7-django1.4] basepython = python2.7 -deps = django==1.4.1 +deps = django==1.4.3 django-filter==0.5.4 [testenv:py2.7-django1.3] basepython = python2.7 -deps = django==1.3.3 +deps = django==1.3.5 django-filter==0.5.4 [testenv:py2.6-django1.5] @@ -27,10 +27,10 @@ deps = https://github.com/django/django/zipball/master [testenv:py2.6-django1.4] basepython = python2.6 -deps = django==1.4.1 +deps = django==1.4.3 django-filter==0.5.4 [testenv:py2.6-django1.3] basepython = python2.6 -deps = django==1.3.3 +deps = django==1.3.5 django-filter==0.5.4