From 8bc9ba8c8aa968fd04c4d67b46c0e8c40f46101c Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 29 Feb 2020 23:56:21 -0600 Subject: [PATCH] Fixes references to serializers + isort --- demo/demo/urls.py | 3 +- demo/demo/wsgi.py | 4 +- dj_rest_auth/app_settings.py | 40 ++++++++++---------- dj_rest_auth/models.py | 2 - dj_rest_auth/registration/app_settings.py | 11 +++--- dj_rest_auth/registration/serializers.py | 6 +-- dj_rest_auth/registration/urls.py | 2 +- dj_rest_auth/registration/views.py | 46 +++++++++++------------ dj_rest_auth/serializers.py | 7 ++-- dj_rest_auth/social_serializers.py | 1 + dj_rest_auth/tests/mixins.py | 6 +-- dj_rest_auth/tests/settings.py | 2 +- dj_rest_auth/tests/test_api.py | 14 +++---- dj_rest_auth/tests/test_social.py | 15 ++++---- dj_rest_auth/tests/urls.py | 26 ++++++------- dj_rest_auth/urls.py | 8 ++-- dj_rest_auth/utils.py | 11 ++++++ dj_rest_auth/views.py | 23 +++++------- docs/conf.py | 2 +- runtests.py | 7 ++-- setup.py | 4 +- 21 files changed, 119 insertions(+), 121 deletions(-) diff --git a/demo/demo/urls.py b/demo/demo/urls.py index 787c6e9..4f63ba1 100644 --- a/demo/demo/urls.py +++ b/demo/demo/urls.py @@ -1,7 +1,6 @@ from django.conf.urls import include, url from django.contrib import admin -from django.views.generic import TemplateView, RedirectView - +from django.views.generic import RedirectView, TemplateView from rest_framework_swagger.views import get_swagger_view urlpatterns = [ diff --git a/demo/demo/wsgi.py b/demo/demo/wsgi.py index ef48754..863501d 100644 --- a/demo/demo/wsgi.py +++ b/demo/demo/wsgi.py @@ -8,7 +8,9 @@ https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/ """ import os -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "demo.settings") from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "demo.settings") + application = get_wsgi_application() diff --git a/dj_rest_auth/app_settings.py b/dj_rest_auth/app_settings.py index 7f8094f..d89202d 100644 --- a/dj_rest_auth/app_settings.py +++ b/dj_rest_auth/app_settings.py @@ -1,34 +1,36 @@ +from dj_rest_auth.serializers import JWTSerializer as DefaultJWTSerializer +from dj_rest_auth.serializers import LoginSerializer as DefaultLoginSerializer +from dj_rest_auth.serializers import \ + PasswordChangeSerializer as DefaultPasswordChangeSerializer +from dj_rest_auth.serializers import \ + PasswordResetConfirmSerializer as DefaultPasswordResetConfirmSerializer +from dj_rest_auth.serializers import \ + PasswordResetSerializer as DefaultPasswordResetSerializer +from dj_rest_auth.serializers import TokenSerializer as DefaultTokenSerializer +from dj_rest_auth.serializers import \ + UserDetailsSerializer as DefaultUserDetailsSerializer from django.conf import settings -from dj_rest_auth.serializers import ( - TokenSerializer as DefaultTokenSerializer, - JWTSerializer as DefaultJWTSerializer, - UserDetailsSerializer as DefaultUserDetailsSerializer, - LoginSerializer as DefaultLoginSerializer, - PasswordResetSerializer as DefaultPasswordResetSerializer, - PasswordResetConfirmSerializer as DefaultPasswordResetConfirmSerializer, - PasswordChangeSerializer as DefaultPasswordChangeSerializer) -from .utils import default_create_token +from .utils import default_create_token, import_callable -create_token = getattr(settings, 'REST_AUTH_TOKEN_CREATOR', default_create_token) +create_token = import_callable(getattr(settings, 'REST_AUTH_TOKEN_CREATOR', default_create_token)) serializers = getattr(settings, 'REST_AUTH_SERIALIZERS', {}) -TokenSerializer = serializers.get('TOKEN_SERIALIZER', DefaultTokenSerializer) +TokenSerializer = import_callable(serializers.get('TOKEN_SERIALIZER', DefaultTokenSerializer)) -JWTSerializer = serializers.get('JWT_SERIALIZER', DefaultJWTSerializer) +JWTSerializer = import_callable(serializers.get('JWT_SERIALIZER', DefaultJWTSerializer)) -UserDetailsSerializer = serializers.get('USER_DETAILS_SERIALIZER', DefaultUserDetailsSerializer) +UserDetailsSerializer = import_callable(serializers.get('USER_DETAILS_SERIALIZER', DefaultUserDetailsSerializer)) -LoginSerializer = serializers.get('LOGIN_SERIALIZER', DefaultLoginSerializer) +LoginSerializer = import_callable(serializers.get('LOGIN_SERIALIZER', DefaultLoginSerializer)) -PasswordResetSerializer = serializers.get( - 'PASSWORD_RESET_SERIALIZER', - DefaultPasswordResetSerializer - ) +PasswordResetSerializer = import_callable(serializers.get( + 'PASSWORD_RESET_SERIALIZER', DefaultPasswordResetSerializer +)) PasswordResetConfirmSerializer = serializers.get( 'PASSWORD_RESET_CONFIRM_SERIALIZER', DefaultPasswordResetConfirmSerializer ) -PasswordChangeSerializer = serializers.get('PASSWORD_CHANGE_SERIALIZER', DefaultPasswordChangeSerializer) +PasswordChangeSerializer = import_callable(serializers.get('PASSWORD_CHANGE_SERIALIZER', DefaultPasswordChangeSerializer)) diff --git a/dj_rest_auth/models.py b/dj_rest_auth/models.py index 5f690da..654c0ae 100644 --- a/dj_rest_auth/models.py +++ b/dj_rest_auth/models.py @@ -1,8 +1,6 @@ from django.conf import settings - from rest_framework.authtoken.models import Token as DefaultTokenModel - # Register your models here. TokenModel = getattr(settings, 'REST_AUTH_TOKEN_MODEL', DefaultTokenModel) diff --git a/dj_rest_auth/registration/app_settings.py b/dj_rest_auth/registration/app_settings.py index fdeae83..bbc7075 100644 --- a/dj_rest_auth/registration/app_settings.py +++ b/dj_rest_auth/registration/app_settings.py @@ -1,14 +1,13 @@ -from django.conf import settings - -from rest_framework.permissions import AllowAny -from dj_rest_auth.registration.serializers import ( +from dj_rest_auth.registration.serializers import \ RegisterSerializer as DefaultRegisterSerializer -) +from django.conf import settings +from rest_framework.permissions import AllowAny +from ..utils import import_callable serializers = getattr(settings, 'REST_AUTH_REGISTER_SERIALIZERS', {}) -RegisterSerializer = serializers.get('REGISTER_SERIALIZER', DefaultRegisterSerializer) +RegisterSerializer = import_callable(serializers.get('REGISTER_SERIALIZER', DefaultRegisterSerializer)) def register_permission_classes(): diff --git a/dj_rest_auth/registration/serializers.py b/dj_rest_auth/registration/serializers.py index 4f99c18..fc7c92b 100644 --- a/dj_rest_auth/registration/serializers.py +++ b/dj_rest_auth/registration/serializers.py @@ -1,6 +1,8 @@ +from django.contrib.auth import get_user_model from django.http import HttpRequest from django.utils.translation import ugettext_lazy as _ -from django.contrib.auth import get_user_model +from requests.exceptions import HTTPError +from rest_framework import serializers try: from allauth.account import app_settings as allauth_settings @@ -14,8 +16,6 @@ try: except ImportError: raise ImportError("allauth needs to be added to INSTALLED_APPS.") -from rest_framework import serializers -from requests.exceptions import HTTPError class SocialAccountSerializer(serializers.ModelSerializer): diff --git a/dj_rest_auth/registration/urls.py b/dj_rest_auth/registration/urls.py index 1004695..c42cec6 100644 --- a/dj_rest_auth/registration/urls.py +++ b/dj_rest_auth/registration/urls.py @@ -1,5 +1,5 @@ -from django.views.generic import TemplateView from django.conf.urls import url +from django.views.generic import TemplateView from .views import RegisterView, VerifyEmailView diff --git a/dj_rest_auth/registration/views.py b/dj_rest_auth/registration/views.py index f8d1120..81ed1ea 100644 --- a/dj_rest_auth/registration/views.py +++ b/dj_rest_auth/registration/views.py @@ -1,34 +1,30 @@ +from allauth.account import app_settings as allauth_settings +from allauth.account.adapter import get_adapter +from allauth.account.utils import complete_signup +from allauth.account.views import ConfirmEmailView +from allauth.socialaccount import signals +from allauth.socialaccount.adapter import get_adapter as get_social_adapter +from allauth.socialaccount.models import SocialAccount +from dj_rest_auth.app_settings import (JWTSerializer, TokenSerializer, + create_token) +from dj_rest_auth.models import TokenModel +from dj_rest_auth.registration.serializers import (SocialAccountSerializer, + SocialConnectSerializer, + SocialLoginSerializer, + VerifyEmailSerializer) +from dj_rest_auth.utils import jwt_encode +from dj_rest_auth.views import LoginView from django.conf import settings from django.utils.decorators import method_decorator from django.utils.translation import ugettext_lazy as _ from django.views.decorators.debug import sensitive_post_parameters - -from rest_framework.views import APIView -from rest_framework.response import Response -from rest_framework.permissions import (AllowAny, - IsAuthenticated) -from rest_framework.generics import CreateAPIView, ListAPIView, GenericAPIView -from rest_framework.exceptions import NotFound from rest_framework import status +from rest_framework.exceptions import NotFound +from rest_framework.generics import CreateAPIView, GenericAPIView, ListAPIView +from rest_framework.permissions import AllowAny, IsAuthenticated +from rest_framework.response import Response +from rest_framework.views import APIView -from allauth.account.adapter import get_adapter -from allauth.account.views import ConfirmEmailView -from allauth.account.utils import complete_signup -from allauth.account import app_settings as allauth_settings -from allauth.socialaccount import signals -from allauth.socialaccount.adapter import get_adapter as get_social_adapter -from allauth.socialaccount.models import SocialAccount - -from dj_rest_auth.app_settings import (TokenSerializer, - JWTSerializer, - create_token) -from dj_rest_auth.models import TokenModel -from dj_rest_auth.registration.serializers import (VerifyEmailSerializer, - SocialLoginSerializer, - SocialAccountSerializer, - SocialConnectSerializer) -from dj_rest_auth.utils import jwt_encode -from dj_rest_auth.views import LoginView from .app_settings import RegisterSerializer, register_permission_classes sensitive_post_parameters_m = method_decorator( diff --git a/dj_rest_auth/serializers.py b/dj_rest_auth/serializers.py index da8fef7..07783da 100644 --- a/dj_rest_auth/serializers.py +++ b/dj_rest_auth/serializers.py @@ -1,12 +1,11 @@ -from django.contrib.auth import get_user_model, authenticate from django.conf import settings +from django.contrib.auth import authenticate, get_user_model from django.contrib.auth.forms import PasswordResetForm, SetPasswordForm from django.contrib.auth.tokens import default_token_generator +from django.utils.encoding import force_text from django.utils.http import urlsafe_base64_decode as uid_decoder from django.utils.translation import ugettext_lazy as _ -from django.utils.encoding import force_text - -from rest_framework import serializers, exceptions +from rest_framework import exceptions, serializers from rest_framework.exceptions import ValidationError from .models import TokenModel diff --git a/dj_rest_auth/social_serializers.py b/dj_rest_auth/social_serializers.py index 6f8a449..60b0c2c 100644 --- a/dj_rest_auth/social_serializers.py +++ b/dj_rest_auth/social_serializers.py @@ -1,6 +1,7 @@ from django.conf import settings from django.http import HttpRequest from rest_framework import serializers + # Import is needed only if we are using social login, in which # case the allauth.socialaccount will be declared if 'allauth.socialaccount' in settings.INSTALLED_APPS: diff --git a/dj_rest_auth/tests/mixins.py b/dj_rest_auth/tests/mixins.py index 30b3d58..06d776d 100644 --- a/dj_rest_auth/tests/mixins.py +++ b/dj_rest_auth/tests/mixins.py @@ -1,11 +1,9 @@ import json from django.conf import settings -from django.test.client import Client, MULTIPART_CONTENT +from django.test.client import MULTIPART_CONTENT, Client from django.utils.encoding import force_text - -from rest_framework import status -from rest_framework import permissions +from rest_framework import permissions, status try: from django.urls import reverse diff --git a/dj_rest_auth/tests/settings.py b/dj_rest_auth/tests/settings.py index f683ce0..d18f41e 100644 --- a/dj_rest_auth/tests/settings.py +++ b/dj_rest_auth/tests/settings.py @@ -1,6 +1,6 @@ +import logging import os import sys -import logging PROJECT_ROOT = os.path.abspath(os.path.split(os.path.split(__file__)[0])[0]) diff --git a/dj_rest_auth/tests/test_api.py b/dj_rest_auth/tests/test_api.py index b6d84f2..585b38d 100644 --- a/dj_rest_auth/tests/test_api.py +++ b/dj_rest_auth/tests/test_api.py @@ -1,17 +1,15 @@ -from django.test import TestCase, override_settings +from allauth.account import app_settings as account_app_settings +from dj_rest_auth.registration.app_settings import register_permission_classes +from dj_rest_auth.registration.views import RegisterView +from django.conf import settings from django.contrib.auth import get_user_model from django.core import mail -from django.conf import settings +from django.test import TestCase, override_settings from django.utils.encoding import force_text - -from allauth.account import app_settings as account_app_settings from rest_framework import status from rest_framework.test import APIRequestFactory -from dj_rest_auth.registration.views import RegisterView -from dj_rest_auth.registration.app_settings import register_permission_classes - -from .mixins import TestsMixin, CustomPermissionClass +from .mixins import CustomPermissionClass, TestsMixin try: from django.urls import reverse diff --git a/dj_rest_auth/tests/test_social.py b/dj_rest_auth/tests/test_social.py index 830e631..47c8d93 100644 --- a/dj_rest_auth/tests/test_social.py +++ b/dj_rest_auth/tests/test_social.py @@ -1,22 +1,23 @@ import json -from django.test import TestCase +import responses +from allauth.socialaccount.models import SocialApp +from allauth.socialaccount.providers.facebook.provider import GRAPH_API_URL from django.contrib.auth import get_user_model -from django.test.utils import override_settings from django.contrib.sites.models import Site +from django.test import TestCase +from django.test.utils import override_settings +from rest_framework import status + +from .mixins import TestsMixin try: from django.urls import reverse except ImportError: from django.core.urlresolvers import reverse -from allauth.socialaccount.models import SocialApp -from allauth.socialaccount.providers.facebook.provider import GRAPH_API_URL -import responses -from rest_framework import status -from .mixins import TestsMixin @override_settings(ROOT_URLCONF="tests.urls") diff --git a/dj_rest_auth/tests/urls.py b/dj_rest_auth/tests/urls.py index 5aa37c1..be199a5 100644 --- a/dj_rest_auth/tests/urls.py +++ b/dj_rest_auth/tests/urls.py @@ -1,20 +1,18 @@ -from django.conf.urls import url, include -from django.views.generic import TemplateView -from . import django_urls - -from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter +from allauth.socialaccount.providers.facebook.views import \ + FacebookOAuth2Adapter from allauth.socialaccount.providers.twitter.views import TwitterOAuthAdapter - +from dj_rest_auth.registration.views import (SocialAccountDisconnectView, + SocialAccountListView, + SocialConnectView, + SocialLoginView) +from dj_rest_auth.social_serializers import (TwitterConnectSerializer, + TwitterLoginSerializer) +from dj_rest_auth.urls import urlpatterns +from django.conf.urls import include, url +from django.views.generic import TemplateView from rest_framework.decorators import api_view -from dj_rest_auth.urls import urlpatterns -from dj_rest_auth.registration.views import ( - SocialLoginView, SocialConnectView, SocialAccountListView, - SocialAccountDisconnectView -) -from dj_rest_auth.social_serializers import ( - TwitterLoginSerializer, TwitterConnectSerializer -) +from . import django_urls class FacebookLogin(SocialLoginView): diff --git a/dj_rest_auth/urls.py b/dj_rest_auth/urls.py index 6664722..d3bfc42 100644 --- a/dj_rest_auth/urls.py +++ b/dj_rest_auth/urls.py @@ -1,10 +1,8 @@ +from dj_rest_auth.views import (LoginView, LogoutView, PasswordChangeView, + PasswordResetConfirmView, PasswordResetView, + UserDetailsView) from django.conf.urls import url -from dj_rest_auth.views import ( - LoginView, LogoutView, UserDetailsView, PasswordChangeView, - PasswordResetView, PasswordResetConfirmView -) - urlpatterns = [ # URLs that do not require a session or valid token url(r'^password/reset/$', PasswordResetView.as_view(), diff --git a/dj_rest_auth/utils.py b/dj_rest_auth/utils.py index b69dae0..b4858c8 100644 --- a/dj_rest_auth/utils.py +++ b/dj_rest_auth/utils.py @@ -1,3 +1,14 @@ +from importlib import import_module + + +def import_callable(path_or_callable): + if hasattr(path_or_callable, '__call__'): + return path_or_callable + else: + assert isinstance(path_or_callable, str) + package, attr = path_or_callable.rsplit('.', 1) + return getattr(import_module(package), attr) + def default_create_token(token_model, user, serializer): token, _ = token_model.objects.get_or_create(user=user) diff --git a/dj_rest_auth/views.py b/dj_rest_auth/views.py index 0a27f8e..b3b8f22 100644 --- a/dj_rest_auth/views.py +++ b/dj_rest_auth/views.py @@ -1,25 +1,22 @@ -from django.contrib.auth import ( - login as django_login, - logout as django_logout -) from django.conf import settings from django.contrib.auth import get_user_model +from django.contrib.auth import login as django_login +from django.contrib.auth import logout as django_logout from django.core.exceptions import ObjectDoesNotExist from django.utils.decorators import method_decorator from django.utils.translation import ugettext_lazy as _ from django.views.decorators.debug import sensitive_post_parameters - from rest_framework import status -from rest_framework.views import APIView -from rest_framework.response import Response from rest_framework.generics import GenericAPIView, RetrieveUpdateAPIView -from rest_framework.permissions import IsAuthenticated, AllowAny +from rest_framework.permissions import AllowAny, IsAuthenticated +from rest_framework.response import Response +from rest_framework.views import APIView -from .app_settings import ( - TokenSerializer, UserDetailsSerializer, LoginSerializer, - PasswordResetSerializer, PasswordResetConfirmSerializer, - PasswordChangeSerializer, JWTSerializer, create_token -) +from .app_settings import (JWTSerializer, LoginSerializer, + PasswordChangeSerializer, + PasswordResetConfirmSerializer, + PasswordResetSerializer, TokenSerializer, + UserDetailsSerializer, create_token) from .models import TokenModel from .utils import jwt_encode diff --git a/docs/conf.py b/docs/conf.py index dd54bb6..7730562 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -12,8 +12,8 @@ # All configuration values have a default; values that are commented out # serve to show the default. -import sys import os +import sys # 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 diff --git a/runtests.py b/runtests.py index 04dfa7c..3cb605b 100644 --- a/runtests.py +++ b/runtests.py @@ -3,13 +3,14 @@ import os import sys +import django +from django.conf import settings +from django.test.utils import get_runner + os.environ['DJANGO_SETTINGS_MODULE'] = 'dj_rest_auth.tests.settings' test_dir = os.path.join(os.path.dirname(__file__), 'dj_rest_auth') sys.path.insert(0, test_dir) -import django -from django.test.utils import get_runner -from django.conf import settings def runtests(): diff --git a/setup.py b/setup.py index b00ff9e..4bbfac8 100644 --- a/setup.py +++ b/setup.py @@ -1,8 +1,8 @@ #!/usr/bin/env python import os -from setuptools import setup, find_packages +from setuptools import find_packages, setup here = os.path.dirname(os.path.abspath(__file__)) f = open(os.path.join(here, 'README.md')) @@ -12,7 +12,7 @@ f.close() setup( name='dj-rest-auth', - version='0.1.2', + version='0.1.3', author='iMerica', author_email='imichael@pm.me', url='http://github.com/iMerica/dj-rest-auth',