From a493c83248535c9fa7f78815b16bce7e88bf7966 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Wed, 19 Dec 2012 23:12:27 +0000 Subject: [PATCH] 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'}