From aa677d51c0d4eb46c60a20c8beea152348b582d5 Mon Sep 17 00:00:00 2001 From: Maxim Kukhtenkov Date: Wed, 21 Dec 2016 14:08:56 -0800 Subject: [PATCH] PEP8 cleanup and small text fixes --- demo/templates/base.html | 2 +- flake8 | 2 +- rest_auth/registration/serializers.py | 20 +++++++++++--------- rest_auth/registration/views.py | 7 ++++--- rest_auth/serializers.py | 8 +------- rest_auth/social_serializers.py | 14 ++++++++------ rest_auth/views.py | 19 +++++++++---------- 7 files changed, 35 insertions(+), 37 deletions(-) diff --git a/demo/templates/base.html b/demo/templates/base.html index a697cc3..cbf02b6 100644 --- a/demo/templates/base.html +++ b/demo/templates/base.html @@ -7,7 +7,7 @@ - Starter Template for Bootstrap + django-rest-auth demo diff --git a/flake8 b/flake8 index 20c1f5b..401fdf4 100644 --- a/flake8 +++ b/flake8 @@ -1,4 +1,4 @@ [flake8] -max-line-length = 160 +max-line-length = 120 exclude = docs/*,demo/* ignore = F403 \ No newline at end of file diff --git a/rest_auth/registration/serializers.py b/rest_auth/registration/serializers.py index b5c43fa..c6b5d5b 100644 --- a/rest_auth/registration/serializers.py +++ b/rest_auth/registration/serializers.py @@ -9,7 +9,7 @@ try: from allauth.account.adapter import get_adapter from allauth.account.utils import setup_user_email except ImportError: - raise ImportError('allauth needs to be added to INSTALLED_APPS.') + raise ImportError("allauth needs to be added to INSTALLED_APPS.") from rest_framework import serializers from requests.exceptions import HTTPError @@ -31,12 +31,13 @@ class SocialLoginSerializer(serializers.Serializer): def get_social_login(self, adapter, app, token, response): """ - - :param adapter: allauth.socialaccount Adapter subclass. Usually OAuthAdapter or Auth2Adapter + :param adapter: allauth.socialaccount Adapter subclass. + Usually OAuthAdapter or Auth2Adapter :param app: `allauth.socialaccount.SocialApp` instance :param token: `allauth.socialaccount.SocialToken` instance :param response: Provider's response for OAuth1. Not used in the - :returns: A populated instance of the `allauth.socialaccount.SocialLoginView` instance + :returns: A populated instance of the + `allauth.socialaccount.SocialLoginView` instance """ request = self._get_request() social_login = adapter.complete_login(request, app, token, response=response) @@ -49,12 +50,12 @@ class SocialLoginSerializer(serializers.Serializer): if not view: raise serializers.ValidationError( - _('View is not defined, pass it as a context variable') + _("View is not defined, pass it as a context variable") ) adapter_class = getattr(view, 'adapter_class', None) if not adapter_class: - raise serializers.ValidationError(_('Define adapter_class in view')) + raise serializers.ValidationError(_("Define adapter_class in view")) adapter = adapter_class(request) app = adapter.get_provider().get_app(request) @@ -73,11 +74,11 @@ class SocialLoginSerializer(serializers.Serializer): if not self.callback_url: raise serializers.ValidationError( - _('Define callback_url in view') + _("Define callback_url in view") ) if not self.client_class: raise serializers.ValidationError( - _('Define client_class in view') + _("Define client_class in view") ) code = attrs.get('code') @@ -97,7 +98,8 @@ class SocialLoginSerializer(serializers.Serializer): access_token = token['access_token'] else: - raise serializers.ValidationError(_('Incorrect input. access_token or code is required.')) + raise serializers.ValidationError( + _("Incorrect input. access_token or code is required.")) social_token = adapter.parse_token({'access_token': access_token}) social_token.app = app diff --git a/rest_auth/registration/views.py b/rest_auth/registration/views.py index 427a0eb..afa7d58 100644 --- a/rest_auth/registration/views.py +++ b/rest_auth/registration/views.py @@ -32,7 +32,7 @@ class RegisterView(CreateAPIView): def get_response_data(self, user): if allauth_settings.EMAIL_VERIFICATION == \ allauth_settings.EmailVerificationMethod.MANDATORY: - return {"detail": _("Verification e-mail sent")} + return {"detail": _("Verification e-mail sent.")} if getattr(settings, 'REST_USE_JWT', False): data = { @@ -49,7 +49,9 @@ class RegisterView(CreateAPIView): user = self.perform_create(serializer) headers = self.get_success_headers(serializer.data) - return Response(self.get_response_data(user), status=status.HTTP_201_CREATED, headers=headers) + return Response(self.get_response_data(user), + status=status.HTTP_201_CREATED, + headers=headers) def perform_create(self, serializer): user = serializer.save(self.request) @@ -65,7 +67,6 @@ class RegisterView(CreateAPIView): class VerifyEmailView(APIView, ConfirmEmailView): - permission_classes = (AllowAny,) allowed_methods = ('POST', 'OPTIONS', 'HEAD') diff --git a/rest_auth/serializers.py b/rest_auth/serializers.py index 49b5a5d..d42242a 100644 --- a/rest_auth/serializers.py +++ b/rest_auth/serializers.py @@ -121,7 +121,6 @@ class TokenSerializer(serializers.ModelSerializer): class UserDetailsSerializer(serializers.ModelSerializer): - """ User model w/o password """ @@ -148,18 +147,15 @@ class JWTSerializer(serializers.Serializer): class PasswordResetSerializer(serializers.Serializer): - """ Serializer for requesting a password reset e-mail. """ - email = serializers.EmailField() password_reset_form_class = PasswordResetForm def get_email_options(self): - """ Override this method to change default e-mail options - """ + """Override this method to change default e-mail options""" return {} def validate_email(self, value): @@ -187,7 +183,6 @@ class PasswordResetConfirmSerializer(serializers.Serializer): """ Serializer for requesting a password reset e-mail. """ - new_password1 = serializers.CharField(max_length=128) new_password2 = serializers.CharField(max_length=128) uid = serializers.CharField() @@ -225,7 +220,6 @@ class PasswordResetConfirmSerializer(serializers.Serializer): class PasswordChangeSerializer(serializers.Serializer): - old_password = serializers.CharField(max_length=128) new_password1 = serializers.CharField(max_length=128) new_password2 = serializers.CharField(max_length=128) diff --git a/rest_auth/social_serializers.py b/rest_auth/social_serializers.py index fb1d3dc..665b98d 100644 --- a/rest_auth/social_serializers.py +++ b/rest_auth/social_serializers.py @@ -21,15 +21,17 @@ class TwitterLoginSerializer(serializers.Serializer): def get_social_login(self, adapter, app, token, response): """ - - :param adapter: allauth.socialaccount Adapter subclass. Usually OAuthAdapter or Auth2Adapter + :param adapter: allauth.socialaccount Adapter subclass. + Usually OAuthAdapter or Auth2Adapter :param app: `allauth.socialaccount.SocialApp` instance :param token: `allauth.socialaccount.SocialToken` instance :param response: Provider's response for OAuth1. Not used in the - :returns: A populated instance of the `allauth.socialaccount.SocialLoginView` instance + :returns: A populated instance of the + `allauth.socialaccount.SocialLoginView` instance """ request = self._get_request() - social_login = adapter.complete_login(request, app, token, response=response) + social_login = adapter.complete_login(request, app, token, + response=response) social_login.token = token return social_login @@ -39,12 +41,12 @@ class TwitterLoginSerializer(serializers.Serializer): if not view: raise serializers.ValidationError( - 'View is not defined, pass it as a context variable' + "View is not defined, pass it as a context variable" ) adapter_class = getattr(view, 'adapter_class', None) if not adapter_class: - raise serializers.ValidationError('Define adapter_class in view') + raise serializers.ValidationError("Define adapter_class in view") adapter = adapter_class(request) app = adapter.get_provider().get_app(request) diff --git a/rest_auth/views.py b/rest_auth/views.py index 5aa7a70..3725418 100644 --- a/rest_auth/views.py +++ b/rest_auth/views.py @@ -23,7 +23,6 @@ from .utils import jwt_encode class LoginView(GenericAPIView): - """ Check the credentials and return the REST Token if the credentials are valid and authenticated. @@ -53,7 +52,8 @@ class LoginView(GenericAPIView): if getattr(settings, 'REST_USE_JWT', False): self.token = jwt_encode(self.user) else: - self.token = create_token(self.token_model, self.user, self.serializer) + self.token = create_token(self.token_model, self.user, + self.serializer) if getattr(settings, 'REST_SESSION_LOGIN', True): self.process_login() @@ -66,9 +66,11 @@ class LoginView(GenericAPIView): 'user': self.user, 'token': self.token } - serializer = serializer_class(instance=data, context={'request': self.request}) + serializer = serializer_class(instance=data, + context={'request': self.request}) else: - serializer = serializer_class(instance=self.token, context={'request': self.request}) + serializer = serializer_class(instance=self.token, + context={'request': self.request}) return Response(serializer.data, status=status.HTTP_200_OK) @@ -82,7 +84,6 @@ class LoginView(GenericAPIView): class LogoutView(APIView): - """ Calls Django logout method and delete the Token object assigned to the current User object. @@ -141,14 +142,12 @@ class UserDetailsView(RetrieveUpdateAPIView): class PasswordResetView(GenericAPIView): - """ Calls Django Auth PasswordResetForm save method. Accepts the following POST parameters: email Returns the success/fail message. """ - serializer_class = PasswordResetSerializer permission_classes = (AllowAny,) @@ -174,7 +173,6 @@ class PasswordResetConfirmView(GenericAPIView): new_password1, new_password2 Returns the success/fail message. """ - serializer_class = PasswordResetConfirmSerializer permission_classes = (AllowAny,) @@ -182,7 +180,9 @@ class PasswordResetConfirmView(GenericAPIView): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) serializer.save() - return Response({"detail": _("Password has been reset with the new password.")}) + return Response( + {"detail": _("Password has been reset with the new password.")} + ) class PasswordChangeView(GenericAPIView): @@ -192,7 +192,6 @@ class PasswordChangeView(GenericAPIView): Accepts the following POST parameters: new_password1, new_password2 Returns the success/fail message. """ - serializer_class = PasswordChangeSerializer permission_classes = (IsAuthenticated,)