mirror of
https://github.com/Tivix/django-rest-auth.git
synced 2024-11-22 09:06:40 +03:00
PEP8 cleanup and small text fixes
This commit is contained in:
parent
5bcf31f545
commit
aa677d51c0
|
@ -7,7 +7,7 @@
|
||||||
<meta name="description" content="Django-rest-auth demo">
|
<meta name="description" content="Django-rest-auth demo">
|
||||||
<meta name="author" content="Tivix, Inc.">
|
<meta name="author" content="Tivix, Inc.">
|
||||||
|
|
||||||
<title>Starter Template for Bootstrap</title>
|
<title>django-rest-auth demo</title>
|
||||||
|
|
||||||
<!-- Latest compiled and minified CSS -->
|
<!-- Latest compiled and minified CSS -->
|
||||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
|
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
|
||||||
|
|
2
flake8
2
flake8
|
@ -1,4 +1,4 @@
|
||||||
[flake8]
|
[flake8]
|
||||||
max-line-length = 160
|
max-line-length = 120
|
||||||
exclude = docs/*,demo/*
|
exclude = docs/*,demo/*
|
||||||
ignore = F403
|
ignore = F403
|
|
@ -9,7 +9,7 @@ try:
|
||||||
from allauth.account.adapter import get_adapter
|
from allauth.account.adapter import get_adapter
|
||||||
from allauth.account.utils import setup_user_email
|
from allauth.account.utils import setup_user_email
|
||||||
except ImportError:
|
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 rest_framework import serializers
|
||||||
from requests.exceptions import HTTPError
|
from requests.exceptions import HTTPError
|
||||||
|
@ -31,12 +31,13 @@ class SocialLoginSerializer(serializers.Serializer):
|
||||||
|
|
||||||
def get_social_login(self, adapter, app, token, response):
|
def get_social_login(self, adapter, app, token, response):
|
||||||
"""
|
"""
|
||||||
|
:param adapter: allauth.socialaccount Adapter subclass.
|
||||||
:param adapter: allauth.socialaccount Adapter subclass. Usually OAuthAdapter or Auth2Adapter
|
Usually OAuthAdapter or Auth2Adapter
|
||||||
:param app: `allauth.socialaccount.SocialApp` instance
|
:param app: `allauth.socialaccount.SocialApp` instance
|
||||||
:param token: `allauth.socialaccount.SocialToken` instance
|
:param token: `allauth.socialaccount.SocialToken` instance
|
||||||
:param response: Provider's response for OAuth1. Not used in the
|
: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()
|
request = self._get_request()
|
||||||
social_login = adapter.complete_login(request, app, token, response=response)
|
social_login = adapter.complete_login(request, app, token, response=response)
|
||||||
|
@ -49,12 +50,12 @@ class SocialLoginSerializer(serializers.Serializer):
|
||||||
|
|
||||||
if not view:
|
if not view:
|
||||||
raise serializers.ValidationError(
|
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)
|
adapter_class = getattr(view, 'adapter_class', None)
|
||||||
if not adapter_class:
|
if not adapter_class:
|
||||||
raise serializers.ValidationError(_('Define adapter_class in view'))
|
raise serializers.ValidationError(_("Define adapter_class in view"))
|
||||||
|
|
||||||
adapter = adapter_class(request)
|
adapter = adapter_class(request)
|
||||||
app = adapter.get_provider().get_app(request)
|
app = adapter.get_provider().get_app(request)
|
||||||
|
@ -73,11 +74,11 @@ class SocialLoginSerializer(serializers.Serializer):
|
||||||
|
|
||||||
if not self.callback_url:
|
if not self.callback_url:
|
||||||
raise serializers.ValidationError(
|
raise serializers.ValidationError(
|
||||||
_('Define callback_url in view')
|
_("Define callback_url in view")
|
||||||
)
|
)
|
||||||
if not self.client_class:
|
if not self.client_class:
|
||||||
raise serializers.ValidationError(
|
raise serializers.ValidationError(
|
||||||
_('Define client_class in view')
|
_("Define client_class in view")
|
||||||
)
|
)
|
||||||
|
|
||||||
code = attrs.get('code')
|
code = attrs.get('code')
|
||||||
|
@ -97,7 +98,8 @@ class SocialLoginSerializer(serializers.Serializer):
|
||||||
access_token = token['access_token']
|
access_token = token['access_token']
|
||||||
|
|
||||||
else:
|
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 = adapter.parse_token({'access_token': access_token})
|
||||||
social_token.app = app
|
social_token.app = app
|
||||||
|
|
|
@ -32,7 +32,7 @@ class RegisterView(CreateAPIView):
|
||||||
def get_response_data(self, user):
|
def get_response_data(self, user):
|
||||||
if allauth_settings.EMAIL_VERIFICATION == \
|
if allauth_settings.EMAIL_VERIFICATION == \
|
||||||
allauth_settings.EmailVerificationMethod.MANDATORY:
|
allauth_settings.EmailVerificationMethod.MANDATORY:
|
||||||
return {"detail": _("Verification e-mail sent")}
|
return {"detail": _("Verification e-mail sent.")}
|
||||||
|
|
||||||
if getattr(settings, 'REST_USE_JWT', False):
|
if getattr(settings, 'REST_USE_JWT', False):
|
||||||
data = {
|
data = {
|
||||||
|
@ -49,7 +49,9 @@ class RegisterView(CreateAPIView):
|
||||||
user = self.perform_create(serializer)
|
user = self.perform_create(serializer)
|
||||||
headers = self.get_success_headers(serializer.data)
|
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):
|
def perform_create(self, serializer):
|
||||||
user = serializer.save(self.request)
|
user = serializer.save(self.request)
|
||||||
|
@ -65,7 +67,6 @@ class RegisterView(CreateAPIView):
|
||||||
|
|
||||||
|
|
||||||
class VerifyEmailView(APIView, ConfirmEmailView):
|
class VerifyEmailView(APIView, ConfirmEmailView):
|
||||||
|
|
||||||
permission_classes = (AllowAny,)
|
permission_classes = (AllowAny,)
|
||||||
allowed_methods = ('POST', 'OPTIONS', 'HEAD')
|
allowed_methods = ('POST', 'OPTIONS', 'HEAD')
|
||||||
|
|
||||||
|
|
|
@ -121,7 +121,6 @@ class TokenSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
|
|
||||||
class UserDetailsSerializer(serializers.ModelSerializer):
|
class UserDetailsSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
User model w/o password
|
User model w/o password
|
||||||
"""
|
"""
|
||||||
|
@ -148,18 +147,15 @@ class JWTSerializer(serializers.Serializer):
|
||||||
|
|
||||||
|
|
||||||
class PasswordResetSerializer(serializers.Serializer):
|
class PasswordResetSerializer(serializers.Serializer):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Serializer for requesting a password reset e-mail.
|
Serializer for requesting a password reset e-mail.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
email = serializers.EmailField()
|
email = serializers.EmailField()
|
||||||
|
|
||||||
password_reset_form_class = PasswordResetForm
|
password_reset_form_class = PasswordResetForm
|
||||||
|
|
||||||
def get_email_options(self):
|
def get_email_options(self):
|
||||||
""" Override this method to change default e-mail options
|
"""Override this method to change default e-mail options"""
|
||||||
"""
|
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
def validate_email(self, value):
|
def validate_email(self, value):
|
||||||
|
@ -187,7 +183,6 @@ class PasswordResetConfirmSerializer(serializers.Serializer):
|
||||||
"""
|
"""
|
||||||
Serializer for requesting a password reset e-mail.
|
Serializer for requesting a password reset e-mail.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
new_password1 = serializers.CharField(max_length=128)
|
new_password1 = serializers.CharField(max_length=128)
|
||||||
new_password2 = serializers.CharField(max_length=128)
|
new_password2 = serializers.CharField(max_length=128)
|
||||||
uid = serializers.CharField()
|
uid = serializers.CharField()
|
||||||
|
@ -225,7 +220,6 @@ class PasswordResetConfirmSerializer(serializers.Serializer):
|
||||||
|
|
||||||
|
|
||||||
class PasswordChangeSerializer(serializers.Serializer):
|
class PasswordChangeSerializer(serializers.Serializer):
|
||||||
|
|
||||||
old_password = serializers.CharField(max_length=128)
|
old_password = serializers.CharField(max_length=128)
|
||||||
new_password1 = serializers.CharField(max_length=128)
|
new_password1 = serializers.CharField(max_length=128)
|
||||||
new_password2 = serializers.CharField(max_length=128)
|
new_password2 = serializers.CharField(max_length=128)
|
||||||
|
|
|
@ -21,15 +21,17 @@ class TwitterLoginSerializer(serializers.Serializer):
|
||||||
|
|
||||||
def get_social_login(self, adapter, app, token, response):
|
def get_social_login(self, adapter, app, token, response):
|
||||||
"""
|
"""
|
||||||
|
:param adapter: allauth.socialaccount Adapter subclass.
|
||||||
:param adapter: allauth.socialaccount Adapter subclass. Usually OAuthAdapter or Auth2Adapter
|
Usually OAuthAdapter or Auth2Adapter
|
||||||
:param app: `allauth.socialaccount.SocialApp` instance
|
:param app: `allauth.socialaccount.SocialApp` instance
|
||||||
:param token: `allauth.socialaccount.SocialToken` instance
|
:param token: `allauth.socialaccount.SocialToken` instance
|
||||||
:param response: Provider's response for OAuth1. Not used in the
|
: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()
|
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
|
social_login.token = token
|
||||||
return social_login
|
return social_login
|
||||||
|
|
||||||
|
@ -39,12 +41,12 @@ class TwitterLoginSerializer(serializers.Serializer):
|
||||||
|
|
||||||
if not view:
|
if not view:
|
||||||
raise serializers.ValidationError(
|
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)
|
adapter_class = getattr(view, 'adapter_class', None)
|
||||||
if not adapter_class:
|
if not adapter_class:
|
||||||
raise serializers.ValidationError('Define adapter_class in view')
|
raise serializers.ValidationError("Define adapter_class in view")
|
||||||
|
|
||||||
adapter = adapter_class(request)
|
adapter = adapter_class(request)
|
||||||
app = adapter.get_provider().get_app(request)
|
app = adapter.get_provider().get_app(request)
|
||||||
|
|
|
@ -23,7 +23,6 @@ from .utils import jwt_encode
|
||||||
|
|
||||||
|
|
||||||
class LoginView(GenericAPIView):
|
class LoginView(GenericAPIView):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Check the credentials and return the REST Token
|
Check the credentials and return the REST Token
|
||||||
if the credentials are valid and authenticated.
|
if the credentials are valid and authenticated.
|
||||||
|
@ -53,7 +52,8 @@ class LoginView(GenericAPIView):
|
||||||
if getattr(settings, 'REST_USE_JWT', False):
|
if getattr(settings, 'REST_USE_JWT', False):
|
||||||
self.token = jwt_encode(self.user)
|
self.token = jwt_encode(self.user)
|
||||||
else:
|
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):
|
if getattr(settings, 'REST_SESSION_LOGIN', True):
|
||||||
self.process_login()
|
self.process_login()
|
||||||
|
@ -66,9 +66,11 @@ class LoginView(GenericAPIView):
|
||||||
'user': self.user,
|
'user': self.user,
|
||||||
'token': self.token
|
'token': self.token
|
||||||
}
|
}
|
||||||
serializer = serializer_class(instance=data, context={'request': self.request})
|
serializer = serializer_class(instance=data,
|
||||||
|
context={'request': self.request})
|
||||||
else:
|
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)
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
@ -82,7 +84,6 @@ class LoginView(GenericAPIView):
|
||||||
|
|
||||||
|
|
||||||
class LogoutView(APIView):
|
class LogoutView(APIView):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Calls Django logout method and delete the Token object
|
Calls Django logout method and delete the Token object
|
||||||
assigned to the current User object.
|
assigned to the current User object.
|
||||||
|
@ -141,14 +142,12 @@ class UserDetailsView(RetrieveUpdateAPIView):
|
||||||
|
|
||||||
|
|
||||||
class PasswordResetView(GenericAPIView):
|
class PasswordResetView(GenericAPIView):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Calls Django Auth PasswordResetForm save method.
|
Calls Django Auth PasswordResetForm save method.
|
||||||
|
|
||||||
Accepts the following POST parameters: email
|
Accepts the following POST parameters: email
|
||||||
Returns the success/fail message.
|
Returns the success/fail message.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
serializer_class = PasswordResetSerializer
|
serializer_class = PasswordResetSerializer
|
||||||
permission_classes = (AllowAny,)
|
permission_classes = (AllowAny,)
|
||||||
|
|
||||||
|
@ -174,7 +173,6 @@ class PasswordResetConfirmView(GenericAPIView):
|
||||||
new_password1, new_password2
|
new_password1, new_password2
|
||||||
Returns the success/fail message.
|
Returns the success/fail message.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
serializer_class = PasswordResetConfirmSerializer
|
serializer_class = PasswordResetConfirmSerializer
|
||||||
permission_classes = (AllowAny,)
|
permission_classes = (AllowAny,)
|
||||||
|
|
||||||
|
@ -182,7 +180,9 @@ class PasswordResetConfirmView(GenericAPIView):
|
||||||
serializer = self.get_serializer(data=request.data)
|
serializer = self.get_serializer(data=request.data)
|
||||||
serializer.is_valid(raise_exception=True)
|
serializer.is_valid(raise_exception=True)
|
||||||
serializer.save()
|
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):
|
class PasswordChangeView(GenericAPIView):
|
||||||
|
@ -192,7 +192,6 @@ class PasswordChangeView(GenericAPIView):
|
||||||
Accepts the following POST parameters: new_password1, new_password2
|
Accepts the following POST parameters: new_password1, new_password2
|
||||||
Returns the success/fail message.
|
Returns the success/fail message.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
serializer_class = PasswordChangeSerializer
|
serializer_class = PasswordChangeSerializer
|
||||||
permission_classes = (IsAuthenticated,)
|
permission_classes = (IsAuthenticated,)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user