From 3d15adaeb11a2f2f2961280026232743afc61398 Mon Sep 17 00:00:00 2001 From: "tom christie tom@tomchristie.com" Date: Tue, 26 Apr 2011 20:20:31 +0100 Subject: [PATCH] Fixes #35 - Import json from django's built-in package (Does cleverness in determing best lib to use) --- djangorestframework/emitters.py | 8 ++------ djangorestframework/parsers.py | 7 ++----- djangorestframework/tests/authentication.py | 10 ++++------ djangorestframework/tests/reverse.py | 6 +----- examples/blogpost/tests.py | 12 +++++------- examples/pygments_api/tests.py | 5 +---- 6 files changed, 15 insertions(+), 33 deletions(-) diff --git a/djangorestframework/emitters.py b/djangorestframework/emitters.py index 2769a4f55..60a4b5dcb 100644 --- a/djangorestframework/emitters.py +++ b/djangorestframework/emitters.py @@ -3,10 +3,11 @@ django-rest-framework also provides HTML and PlainText emitters that help self-d by serializing the output along with documentation regarding the Resource, output status and headers, and providing forms and links depending on the allowed methods, emitters and parsers on the Resource. """ +from django import forms from django.conf import settings from django.http import HttpResponse from django.template import RequestContext, loader -from django import forms +from django.utils import simplejson as json from djangorestframework.response import NoContent, ResponseException from djangorestframework.validators import FormValidatorMixin @@ -21,11 +22,6 @@ import string import re from decimal import Decimal -try: - import json -except ImportError: - import simplejson as json - _MSIE_USER_AGENT = re.compile(r'^Mozilla/[0-9]+\.[0-9]+ \([^)]*; MSIE [0-9]+\.[0-9]+[a-z]?;[^)]*\)(?!.* Opera )') diff --git a/djangorestframework/parsers.py b/djangorestframework/parsers.py index 1503342c7..35003a0f7 100644 --- a/djangorestframework/parsers.py +++ b/djangorestframework/parsers.py @@ -9,16 +9,13 @@ We need a method to be able to: and multipart/form-data. (eg also handle multipart/json) """ from django.http.multipartparser import MultiPartParser as DjangoMPParser +from django.utils import simplejson as json + from djangorestframework.response import ResponseException from djangorestframework import status from djangorestframework.utils import as_tuple from djangorestframework.mediatypes import MediaType -try: - import json -except ImportError: - import simplejson as json - try: from urlparse import parse_qs except ImportError: diff --git a/djangorestframework/tests/authentication.py b/djangorestframework/tests/authentication.py index af9c34ca9..246ad4a02 100644 --- a/djangorestframework/tests/authentication.py +++ b/djangorestframework/tests/authentication.py @@ -1,16 +1,14 @@ from django.conf.urls.defaults import patterns -from django.test import TestCase -from django.test import Client +from django.test import Client, TestCase +from django.utils import simplejson as json + from djangorestframework.compat import RequestFactory from djangorestframework.resource import Resource from django.contrib.auth.models import User from django.contrib.auth import login import base64 -try: - import json -except ImportError: - import simplejson as json + class MockResource(Resource): allowed_methods = ('POST',) diff --git a/djangorestframework/tests/reverse.py b/djangorestframework/tests/reverse.py index a862e39a6..2718ebca3 100644 --- a/djangorestframework/tests/reverse.py +++ b/djangorestframework/tests/reverse.py @@ -1,14 +1,10 @@ from django.conf.urls.defaults import patterns, url from django.core.urlresolvers import reverse from django.test import TestCase +from django.utils import simplejson as json from djangorestframework.resource import Resource -try: - import json -except ImportError: - import simplejson as json - class MockResource(Resource): """Mock resource which simply returns a URL, so that we can ensure that reversed URLs are fully qualified""" diff --git a/examples/blogpost/tests.py b/examples/blogpost/tests.py index dfb4d5f57..d4084e729 100644 --- a/examples/blogpost/tests.py +++ b/examples/blogpost/tests.py @@ -3,11 +3,13 @@ from django.test import TestCase from django.core.urlresolvers import reverse +from django.utils import simplejson as json + +from djangorestframework.compat import RequestFactory + from blogpost import views, models import blogpost -#import json -#from rest.utils import xml2dict, dict2xml class AcceptHeaderTests(TestCase): """Test correct behaviour of the Accept header as specified by RFC 2616: @@ -164,11 +166,7 @@ class AllowedMethodsTests(TestCase): #above testcases need to probably moved to the core -from djangorestframework.compat import RequestFactory -try: - import json -except ImportError: - import simplejson as json + class TestRotation(TestCase): """For the example the maximum amount of Blogposts is capped off at views.MAX_POSTS. diff --git a/examples/pygments_api/tests.py b/examples/pygments_api/tests.py index 017823b93..b4b01e8d1 100644 --- a/examples/pygments_api/tests.py +++ b/examples/pygments_api/tests.py @@ -1,12 +1,9 @@ from django.test import TestCase +from django.utils import simplejson as json from djangorestframework.compat import RequestFactory from pygments_api import views import tempfile, shutil -try: - import json -except ImportError: - import simplejson as json class TestPygmentsExample(TestCase):