From e032be43279f076965be3f1cf98b9d6ac2d8b759 Mon Sep 17 00:00:00 2001 From: Jesus Anaya Date: Thu, 1 Nov 2018 12:36:45 -0700 Subject: [PATCH] Fixed test runner for Django >= 1.8. Fixed issue with imports in python 3 --- disqus/__init__.py | 18 +++++--- disqus/api.py | 44 ++++++++++++++----- disqus/management/commands/disqus_dumpdata.py | 10 ++--- disqus/management/commands/disqus_export.py | 9 ++-- runtests.py | 23 +++++++--- 5 files changed, 69 insertions(+), 35 deletions(-) diff --git a/disqus/__init__.py b/disqus/__init__.py index c3f52c8..4acdd29 100644 --- a/disqus/__init__.py +++ b/disqus/__init__.py @@ -1,9 +1,15 @@ -import urllib -import urllib2 +try: + # Python 3 + from urllib.request import urlopen + from urllib.parse import urlencode +except ImportError: + # Python 2 + from urllib2 import urlopen + from urllib import urlencode from django.core.management.base import CommandError -from django.utils import simplejson as json from django.conf import settings +import json def call(method, data, post=False): """ @@ -14,12 +20,12 @@ def call(method, data, post=False): if post: # POST request url += "/" - data = urllib.urlencode(data) + data = urlencode(data) else: # GET request - url += "?%s" % urllib.urlencode(data) + url += "?%s" % urlencode(data) data = '' - res = json.load(urllib2.urlopen(url, data)) + res = json.load(urlopen(url, data)) if not res['succeeded']: raise CommandError("'%s' failed: %s\nData: %s" % (method, res['code'], data)) return res['message'] diff --git a/disqus/api.py b/disqus/api.py index a0f0d58..b5d974a 100644 --- a/disqus/api.py +++ b/disqus/api.py @@ -1,13 +1,33 @@ -from urllib import urlencode -import urllib2 +try: + # Python 3 + from urllib.request import ( + urlopen, + build_opener, + install_opener, + ProxyHandler, + Request, + URLError + ) + from urllib.parse import urlencode +except ImportError: + # Python 2 + from urllib2 import ( + urlopen, + build_opener, + install_opener, + ProxyHandler, + Request, + URLError + ) + from urllib import urlencode -from django.utils import simplejson as json +import json -# A custom ProxyHandler for the urllib2 module that will not +# A custom ProxyHandler class that will not # auto-detect proxy settings -proxy_support = urllib2.ProxyHandler({}) -opener = urllib2.build_opener(proxy_support) -urllib2.install_opener(opener) +proxy_support = ProxyHandler({}) +opener = build_opener(proxy_support) +install_opener(opener) class DisqusException(Exception): """Exception raised for errors with the DISQUS API.""" @@ -56,15 +76,15 @@ def call_method(**kwargs): def _get_request(self, request_url, request_method, **params): """ - Return a urllib2.Request object that has the GET parameters + Return a Request object that has the GET parameters attached to the url or the POST data attached to the object. """ if request_method == 'GET': if params: request_url += '&%s' % urlencode(params) - request = urllib2.Request(request_url) + request = Request(request_url) elif request_method == 'POST': - request = urllib2.Request(request_url, urlencode(params,doseq=1)) + request = Request(request_url, urlencode(params,doseq=1)) return request def call(self, method, **params): @@ -76,8 +96,8 @@ def call(self, method, **params): url = self.api_url % method request = self._get_request(url, self.METHODS[method], **params) try: - response = urllib2.urlopen(request) - except urllib2.URLError, e: + response = urlopen(request) + except URLError as e: raise else: response_json = json.loads(response.read()) diff --git a/disqus/management/commands/disqus_dumpdata.py b/disqus/management/commands/disqus_dumpdata.py index 2fae41b..38c6022 100644 --- a/disqus/management/commands/disqus_dumpdata.py +++ b/disqus/management/commands/disqus_dumpdata.py @@ -1,9 +1,7 @@ -from optparse import make_option - from django.core.management.base import NoArgsCommand, CommandError -from django.utils import simplejson as json - from disqus.api import DisqusClient +from optparse import make_option +import json class Command(NoArgsCommand): @@ -26,8 +24,8 @@ def handle(self, **options): filter_ = options.get('filter') exclude = options.get('exclude') - # Get a list of all forums for an API key. Each API key can have - # multiple forums associated. This application only supports the one + # Get a list of all forums for an API key. Each API key can have + # multiple forums associated. This application only supports the one # set in the DISQUS_WEBSITE_SHORTNAME variable forum_list = client.get_forum_list(user_api_key=settings.DISQUS_API_KEY) try: diff --git a/disqus/management/commands/disqus_export.py b/disqus/management/commands/disqus_export.py index f7f3f4a..48b577d 100644 --- a/disqus/management/commands/disqus_export.py +++ b/disqus/management/commands/disqus_export.py @@ -5,9 +5,8 @@ from django.contrib import comments from django.contrib.sites.models import Site from django.core.management.base import NoArgsCommand -from django.utils import simplejson as json - from disqus.api import DisqusClient +import json class Command(NoArgsCommand): @@ -74,8 +73,8 @@ def handle(self, **options): if not comments_count: return - # Get a list of all forums for an API key. Each API key can have - # multiple forums associated. This application only supports the one + # Get a list of all forums for an API key. Each API key can have + # multiple forums associated. This application only supports the one # set in the DISQUS_WEBSITE_SHORTNAME variable forum_list = client.get_forum_list(user_api_key=settings.DISQUS_API_KEY) try: @@ -104,7 +103,7 @@ def handle(self, **options): forum_api_key=forum_api_key) # if no thread with the URL could be found, we create a new one. - # to do this, we first need to create the thread and then + # to do this, we first need to create the thread and then # update the thread with a URL. if not thread: thread = client.thread_by_identifier( diff --git a/runtests.py b/runtests.py index 47f5989..24add88 100644 --- a/runtests.py +++ b/runtests.py @@ -2,8 +2,8 @@ import logging import sys from os.path import dirname, abspath - from django.conf import settings +import django if not settings.configured: settings.configure( @@ -19,17 +19,28 @@ DEBUG=False, ) -from django.test.simple import run_tests - def runtests(*test_args): if not test_args: test_args = ['disqus'] + parent = dirname(abspath(__file__)) sys.path.insert(0, parent) - failures = run_tests(test_args, verbosity=1, interactive=True) - sys.exit(failures) + + if django.VERSION < (1, 8): + from django.test.simple import run_tests + + failures = run_tests(test_args, verbosity=1, interactive=True) + sys.exit(failures) + + else: + from django.test.runner import DiscoverRunner + + django.setup() + runner = DiscoverRunner(verbosity=1, interactive=True) + failures = runner.run_tests(test_args) + sys.exit(failures) if __name__ == '__main__': - runtests(*sys.argv[1:]) \ No newline at end of file + runtests(*sys.argv[1:])