From 2129f32f55cda950ef220c130dc7de55bea29caf Mon Sep 17 00:00:00 2001 From: Jerome Leclanche Date: Fri, 15 Sep 2017 03:20:50 +0300 Subject: [PATCH] Switch to isort and clean up imports --- docs/conf.py | 11 ++++++++--- oauth2_provider/admin.py | 6 ++---- oauth2_provider/compat.py | 1 + .../contrib/rest_framework/permissions.py | 2 +- oauth2_provider/generators.py | 3 +-- oauth2_provider/oauth2_validators.py | 7 ++----- oauth2_provider/scopes.py | 3 +-- oauth2_provider/signals.py | 1 + oauth2_provider/views/application.py | 4 +++- oauth2_provider/views/base.py | 4 ++-- oauth2_provider/views/generic.py | 4 +++- tests/test_auth_backends.py | 5 +---- tests/test_authorization_code.py | 7 +++---- tests/test_client_credential.py | 6 ++---- tests/test_decorators.py | 5 +---- tests/test_introspection_auth.py | 3 ++- tests/test_introspection_view.py | 1 + tests/test_mixins.py | 5 +++-- tests/test_models.py | 7 +++---- tests/test_oauth2_backends.py | 9 ++++++--- tests/test_oauth2_validators.py | 15 +++++++-------- tests/test_password.py | 1 + tests/test_rest_framework.py | 5 +---- tests/test_scopes.py | 8 ++++---- tests/test_scopes_backend.py | 3 +-- tests/test_token_revocation.py | 4 +--- tests/test_token_view.py | 5 +---- tests/urls.py | 1 + tox.ini | 17 +++++++++++++---- 29 files changed, 77 insertions(+), 76 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 321887d76..f4a5f2148 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -11,7 +11,14 @@ # All configuration values have a default; values that are commented out # serve to show the default. -import sys, os, re +import os +import re +import sys + +import django + +import oauth2_provider + # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the @@ -22,10 +29,8 @@ os.environ["DJANGO_SETTINGS_MODULE"] = "tests.settings" -import django django.setup() -import oauth2_provider # -- General configuration ----------------------------------------------------- diff --git a/oauth2_provider/admin.py b/oauth2_provider/admin.py index 1f8312f4a..c6bbe44b7 100644 --- a/oauth2_provider/admin.py +++ b/oauth2_provider/admin.py @@ -1,10 +1,8 @@ from django.contrib import admin from .models import ( - get_access_token_model, - get_application_model, - get_grant_model, - get_refresh_token_model, + get_access_token_model, get_application_model, + get_grant_model, get_refresh_token_model ) diff --git a/oauth2_provider/compat.py b/oauth2_provider/compat.py index 4e8be2296..6e455b0b3 100644 --- a/oauth2_provider/compat.py +++ b/oauth2_provider/compat.py @@ -5,6 +5,7 @@ # flake8: noqa from __future__ import unicode_literals + # urlparse in python3 has been renamed to urllib.parse try: from urlparse import parse_qs, parse_qsl, urlparse, urlsplit, urlunparse, urlunsplit diff --git a/oauth2_provider/contrib/rest_framework/permissions.py b/oauth2_provider/contrib/rest_framework/permissions.py index cce946a4b..e5df053d0 100644 --- a/oauth2_provider/contrib/rest_framework/permissions.py +++ b/oauth2_provider/contrib/rest_framework/permissions.py @@ -3,8 +3,8 @@ from django.core.exceptions import ImproperlyConfigured from rest_framework.permissions import BasePermission, IsAuthenticated -from .authentication import OAuth2Authentication from ...settings import oauth2_settings +from .authentication import OAuth2Authentication log = logging.getLogger("oauth2_provider") diff --git a/oauth2_provider/generators.py b/oauth2_provider/generators.py index 5b861e74d..6e8124979 100644 --- a/oauth2_provider/generators.py +++ b/oauth2_provider/generators.py @@ -1,5 +1,4 @@ -from __future__ import absolute_import -from __future__ import unicode_literals +from __future__ import absolute_import, unicode_literals from oauthlib.common import generate_client_id as oauthlib_generate_client_id from oauthlib.common import UNICODE_ASCII_CHARACTER_SET diff --git a/oauth2_provider/oauth2_validators.py b/oauth2_provider/oauth2_validators.py index ec34e58d5..2cc4b4b20 100644 --- a/oauth2_provider/oauth2_validators.py +++ b/oauth2_provider/oauth2_validators.py @@ -17,11 +17,8 @@ from .compat import unquote_plus from .exceptions import FatalClientError from .models import ( - AbstractApplication, - get_access_token_model, - get_application_model, - get_grant_model, - get_refresh_token_model, + AbstractApplication, get_access_token_model, + get_application_model, get_grant_model, get_refresh_token_model ) from .scopes import get_scopes_backend from .settings import oauth2_settings diff --git a/oauth2_provider/scopes.py b/oauth2_provider/scopes.py index 5320e0f65..d0eae5789 100644 --- a/oauth2_provider/scopes.py +++ b/oauth2_provider/scopes.py @@ -1,5 +1,4 @@ -from __future__ import absolute_import -from __future__ import unicode_literals +from __future__ import absolute_import, unicode_literals from .settings import oauth2_settings diff --git a/oauth2_provider/signals.py b/oauth2_provider/signals.py index 1ed40b4aa..af7f4ae2f 100644 --- a/oauth2_provider/signals.py +++ b/oauth2_provider/signals.py @@ -1,3 +1,4 @@ from django.dispatch import Signal + app_authorized = Signal(providing_args=['request', 'token']) diff --git a/oauth2_provider/views/application.py b/oauth2_provider/views/application.py index 10cfff87d..0bd0dd691 100644 --- a/oauth2_provider/views/application.py +++ b/oauth2_provider/views/application.py @@ -1,7 +1,9 @@ from django.contrib.auth.mixins import LoginRequiredMixin from django.forms.models import modelform_factory from django.urls import reverse_lazy -from django.views.generic import CreateView, DeleteView, DetailView, ListView, UpdateView +from django.views.generic import ( + CreateView, DeleteView, DetailView, ListView, UpdateView +) from ..models import get_application_model diff --git a/oauth2_provider/views/base.py b/oauth2_provider/views/base.py index d15572f51..f56f7eb3a 100644 --- a/oauth2_provider/views/base.py +++ b/oauth2_provider/views/base.py @@ -9,14 +9,14 @@ from django.views.decorators.debug import sensitive_post_parameters from django.views.generic import FormView, View -from ..signals import app_authorized -from .mixins import OAuthLibMixin from ..exceptions import OAuthToolkitError from ..forms import AllowForm from ..http import HttpResponseUriRedirect from ..models import get_access_token_model, get_application_model from ..scopes import get_scopes_backend from ..settings import oauth2_settings +from ..signals import app_authorized +from .mixins import OAuthLibMixin log = logging.getLogger("oauth2_provider") diff --git a/oauth2_provider/views/generic.py b/oauth2_provider/views/generic.py index 2a1219036..c9bbc6af4 100644 --- a/oauth2_provider/views/generic.py +++ b/oauth2_provider/views/generic.py @@ -1,7 +1,9 @@ from django.views.generic import View -from .mixins import ProtectedResourceMixin, ReadWriteScopedResourceMixin, ScopedResourceMixin from ..settings import oauth2_settings +from .mixins import ( + ProtectedResourceMixin, ReadWriteScopedResourceMixin, ScopedResourceMixin +) class ProtectedResourceView(ProtectedResourceMixin, View): diff --git a/tests/test_auth_backends.py b/tests/test_auth_backends.py index 4096a62fe..530caa738 100644 --- a/tests/test_auth_backends.py +++ b/tests/test_auth_backends.py @@ -7,10 +7,7 @@ from oauth2_provider.backends import OAuth2Backend from oauth2_provider.middleware import OAuth2TokenMiddleware -from oauth2_provider.models import ( - get_access_token_model, - get_application_model, -) +from oauth2_provider.models import get_access_token_model, get_application_model UserModel = get_user_model() diff --git a/tests/test_authorization_code.py b/tests/test_authorization_code.py index fa7410216..824818450 100644 --- a/tests/test_authorization_code.py +++ b/tests/test_authorization_code.py @@ -12,13 +12,12 @@ from oauth2_provider.compat import parse_qs, urlencode, urlparse from oauth2_provider.models import ( - get_access_token_model, - get_application_model, - get_grant_model, - get_refresh_token_model, + get_access_token_model, get_application_model, + get_grant_model, get_refresh_token_model ) from oauth2_provider.settings import oauth2_settings from oauth2_provider.views import ProtectedResourceView + from .utils import get_basic_auth_header diff --git a/tests/test_client_credential.py b/tests/test_client_credential.py index 80a94e5ad..7ec49ed67 100644 --- a/tests/test_client_credential.py +++ b/tests/test_client_credential.py @@ -9,15 +9,13 @@ from oauthlib.oauth2 import BackendApplicationServer from oauth2_provider.compat import quote_plus -from oauth2_provider.models import ( - get_access_token_model, - get_application_model, -) +from oauth2_provider.models import get_access_token_model, get_application_model from oauth2_provider.oauth2_backends import OAuthLibCore from oauth2_provider.oauth2_validators import OAuth2Validator from oauth2_provider.settings import oauth2_settings from oauth2_provider.views import ProtectedResourceView from oauth2_provider.views.mixins import OAuthLibMixin + from .utils import get_basic_auth_header diff --git a/tests/test_decorators.py b/tests/test_decorators.py index 6443b9611..0732b2920 100644 --- a/tests/test_decorators.py +++ b/tests/test_decorators.py @@ -5,10 +5,7 @@ from django.utils import timezone from oauth2_provider.decorators import protected_resource, rw_protected_resource -from oauth2_provider.models import ( - get_access_token_model, - get_application_model, -) +from oauth2_provider.models import get_access_token_model, get_application_model from oauth2_provider.settings import oauth2_settings diff --git a/tests/test_introspection_auth.py b/tests/test_introspection_auth.py index 76aa6cec5..61fa2ec4d 100644 --- a/tests/test_introspection_auth.py +++ b/tests/test_introspection_auth.py @@ -6,7 +6,7 @@ from django.conf.urls import include, url from django.contrib.auth import get_user_model from django.http import HttpResponse -from django.test import override_settings, TestCase +from django.test import TestCase, override_settings from django.utils import timezone from oauthlib.common import Request @@ -15,6 +15,7 @@ from oauth2_provider.settings import oauth2_settings from oauth2_provider.views import ScopedProtectedResourceView + try: from unittest import mock except ImportError: diff --git a/tests/test_introspection_view.py b/tests/test_introspection_view.py index 0bbcfa88c..0d9b2ce74 100644 --- a/tests/test_introspection_view.py +++ b/tests/test_introspection_view.py @@ -11,6 +11,7 @@ from oauth2_provider.models import get_access_token_model, get_application_model from oauth2_provider.settings import oauth2_settings + Application = get_application_model() AccessToken = get_access_token_model() UserModel = get_user_model() diff --git a/tests/test_mixins.py b/tests/test_mixins.py index 4dd6bc329..a4a116555 100644 --- a/tests/test_mixins.py +++ b/tests/test_mixins.py @@ -3,12 +3,13 @@ from django.core.exceptions import ImproperlyConfigured from django.test import RequestFactory, TestCase from django.views.generic import View - from oauthlib.oauth2 import Server from oauth2_provider.oauth2_backends import OAuthLibCore from oauth2_provider.oauth2_validators import OAuth2Validator -from oauth2_provider.views.mixins import OAuthLibMixin, ProtectedResourceMixin, ScopedResourceMixin +from oauth2_provider.views.mixins import ( + OAuthLibMixin, ProtectedResourceMixin, ScopedResourceMixin +) class BaseTest(TestCase): diff --git a/tests/test_models.py b/tests/test_models.py index 474b830e7..083cf1357 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -7,13 +7,12 @@ from django.utils import timezone from oauth2_provider.models import ( - get_access_token_model, - get_application_model, - get_grant_model, - get_refresh_token_model, + get_access_token_model, get_application_model, + get_grant_model, get_refresh_token_model ) from oauth2_provider.settings import oauth2_settings + Application = get_application_model() Grant = get_grant_model() AccessToken = get_access_token_model() diff --git a/tests/test_oauth2_backends.py b/tests/test_oauth2_backends.py index a18e62a3a..e7d42b80c 100644 --- a/tests/test_oauth2_backends.py +++ b/tests/test_oauth2_backends.py @@ -1,14 +1,17 @@ import json +from django.test import RequestFactory, TestCase + +from oauth2_provider.backends import get_oauthlib_core +from oauth2_provider.oauth2_backends import JSONOAuthLibCore, OAuthLibCore + + try: from unittest import mock except ImportError: import mock -from django.test import RequestFactory, TestCase -from oauth2_provider.backends import get_oauthlib_core -from oauth2_provider.oauth2_backends import JSONOAuthLibCore, OAuthLibCore class TestOAuthLibCoreBackend(TestCase): diff --git a/tests/test_oauth2_validators.py b/tests/test_oauth2_validators.py index 929e70a9b..92f024f48 100644 --- a/tests/test_oauth2_validators.py +++ b/tests/test_oauth2_validators.py @@ -1,10 +1,5 @@ import datetime -try: - from unittest import mock -except ImportError: - import mock - from django.contrib.auth import get_user_model from django.test import TransactionTestCase from django.utils import timezone @@ -12,13 +7,17 @@ from oauth2_provider.exceptions import FatalClientError from oauth2_provider.models import ( - get_access_token_model, - get_application_model, - get_refresh_token_model, + get_access_token_model, get_application_model, get_refresh_token_model ) from oauth2_provider.oauth2_validators import OAuth2Validator +try: + from unittest import mock +except ImportError: + import mock + + UserModel = get_user_model() Application = get_application_model() AccessToken = get_access_token_model() diff --git a/tests/test_password.py b/tests/test_password.py index 40dcd1f15..9a295c9b2 100644 --- a/tests/test_password.py +++ b/tests/test_password.py @@ -9,6 +9,7 @@ from oauth2_provider.models import get_application_model from oauth2_provider.settings import oauth2_settings from oauth2_provider.views import ProtectedResourceView + from .utils import get_basic_auth_header diff --git a/tests/test_rest_framework.py b/tests/test_rest_framework.py index 6c910bbfe..61090a502 100644 --- a/tests/test_rest_framework.py +++ b/tests/test_rest_framework.py @@ -8,10 +8,7 @@ from django.test.utils import override_settings from django.utils import timezone -from oauth2_provider.models import ( - get_access_token_model, - get_application_model, -) +from oauth2_provider.models import get_access_token_model, get_application_model from oauth2_provider.settings import oauth2_settings diff --git a/tests/test_scopes.py b/tests/test_scopes.py index 75eb557df..daccfed00 100644 --- a/tests/test_scopes.py +++ b/tests/test_scopes.py @@ -9,12 +9,12 @@ from oauth2_provider.compat import parse_qs, urlparse from oauth2_provider.models import ( - get_access_token_model, - get_application_model, - get_grant_model, + get_access_token_model, get_application_model, get_grant_model ) from oauth2_provider.settings import oauth2_settings -from oauth2_provider.views import ReadWriteScopedResourceView, ScopedProtectedResourceView +from oauth2_provider.views import ( + ReadWriteScopedResourceView, ScopedProtectedResourceView +) from .utils import get_basic_auth_header diff --git a/tests/test_scopes_backend.py b/tests/test_scopes_backend.py index 26ca7ee85..06d45b0ed 100644 --- a/tests/test_scopes_backend.py +++ b/tests/test_scopes_backend.py @@ -1,5 +1,4 @@ -from __future__ import absolute_import -from __future__ import unicode_literals +from __future__ import absolute_import, unicode_literals from oauth2_provider.scopes import SettingsScopes diff --git a/tests/test_token_revocation.py b/tests/test_token_revocation.py index 60d4456b1..8870b9d7b 100644 --- a/tests/test_token_revocation.py +++ b/tests/test_token_revocation.py @@ -9,9 +9,7 @@ from oauth2_provider.compat import urlencode from oauth2_provider.models import ( - get_access_token_model, - get_application_model, - get_refresh_token_model, + get_access_token_model, get_application_model, get_refresh_token_model ) from oauth2_provider.settings import oauth2_settings diff --git a/tests/test_token_view.py b/tests/test_token_view.py index e64d3e0f2..5c0a92d47 100644 --- a/tests/test_token_view.py +++ b/tests/test_token_view.py @@ -7,10 +7,7 @@ from django.urls import reverse from django.utils import timezone -from oauth2_provider.models import ( - get_access_token_model, - get_application_model, -) +from oauth2_provider.models import get_access_token_model, get_application_model Application = get_application_model() diff --git a/tests/urls.py b/tests/urls.py index 96f95e7c7..16dcf6ded 100644 --- a/tests/urls.py +++ b/tests/urls.py @@ -1,6 +1,7 @@ from django.conf.urls import include, url from django.contrib import admin + admin.autodiscover() diff --git a/tox.ini b/tox.ini index 6919bdef9..9cbcede4b 100644 --- a/tox.ini +++ b/tox.ini @@ -33,11 +33,13 @@ commands = make html deps = sphinx [testenv:flake8] -commands = flake8 +commands = + flake8 {toxinidir} {posargs} + isort {toxinidir} -c deps = flake8 - flake8-import-order flake8-quotes + isort [coverage:run] source = oauth2_provider @@ -48,7 +50,14 @@ django_find_project = false [flake8] max-line-length = 110 -exclude = docs/, migrations/, .tox/ -import-order-style = smarkets +exclude = docs/, oauth2_provider/migrations/, .tox/ application-import-names = oauth2_provider inline-quotes = " + +[isort] +lines_after_imports = 2 +known_first_party = oauth2_provider +multi_line_output = 5 +skip = oauth2_provider/migrations/, .tox/ +line_length = 80 +balanced_wrapping = True