Cleaned up LoginSerializer codebase

This commit is contained in:
mario 2016-01-06 01:18:13 +01:00
parent b12ed79bb1
commit 54eb54ad65
2 changed files with 51 additions and 27 deletions

View File

@ -7,6 +7,7 @@ Basic
- /rest-auth/login/ (POST)
- username (string)
- email (string)
- password (string)

View File

@ -19,29 +19,31 @@ class LoginSerializer(serializers.Serializer):
email = serializers.EmailField(required=False, allow_blank=True)
password = serializers.CharField(style={'input_type': 'password'})
def validate(self, attrs):
username = attrs.get('username')
email = attrs.get('email')
password = attrs.get('password')
def _validate_email(self, email, password):
user = None
if 'allauth' in settings.INSTALLED_APPS:
from allauth.account import app_settings
# Authentication through email
if app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod.EMAIL:
if email and password:
user = authenticate(email=email, password=password)
else:
msg = _('Must include "email" and "password".')
raise exceptions.ValidationError(msg)
# Authentication through username
elif app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod.USERNAME:
return user
def _validate_username(self, username, password):
user = None
if username and password:
user = authenticate(username=username, password=password)
else:
msg = _('Must include "username" and "password".')
raise exceptions.ValidationError(msg)
# Authentication through either username or email
else:
return user
def _validate_username_email(self, username, email, password):
user = None
if email and password:
user = authenticate(email=email, password=password)
elif username and password:
@ -50,19 +52,40 @@ class LoginSerializer(serializers.Serializer):
msg = _('Must include either "username" or "email" and "password".')
raise exceptions.ValidationError(msg)
elif username or email and password:
# Try get username if we have in request email
if email and not username:
return user
def validate(self, attrs):
username = attrs.get('username')
email = attrs.get('email')
password = attrs.get('password')
user = None
if 'allauth' in settings.INSTALLED_APPS:
from allauth.account import app_settings
# Authentication through email
if app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod.EMAIL:
user = self._validate_email(email, password)
# Authentication through username
if app_settings.AUTHENTICATION_METHOD == app_settings.AuthenticationMethod.USERNAME:
user = self._validate_username(username, password)
# Authentication through either username or email
else:
user = self._validate_username_email(username, email, password)
else:
# Authentication without using allauth
if email:
try:
username = UserModel.objects.get(email__iexact=email).username
except UserModel.DoesNotExist:
user = None
if username:
user = authenticate(username=username, password=password)
pass
else:
msg = _('Must include either "username" or "email" and "password".')
raise exceptions.ValidationError(msg)
if username:
user = self._validate_username_email(username, '', password)
# Did we get back an active user?
if user: