This commit is contained in:
Mojca Rojko 2016-06-18 00:52:04 +00:00 committed by GitHub
commit e66bea7814

View File

@ -1,19 +1,23 @@
from django.contrib.auth import authenticate
from django.contrib.auth import authenticate, get_user_model
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers
from rest_framework.fields import empty
class AuthTokenSerializer(serializers.Serializer):
username = serializers.CharField(label=_("Username"))
password = serializers.CharField(label=_("Password"), style={'input_type': 'password'})
def __init__(self, data=empty, **kwargs):
self.fields[get_user_model().USERNAME_FIELD] = serializers.CharField(label=_(get_user_model().USERNAME_FIELD))
super(self.__class__, self).__init__(self, data, **kwargs)
def validate(self, attrs):
username = attrs.get('username')
username = attrs.get('%s' % get_user_model().USERNAME_FIELD)
password = attrs.get('password')
if username and password:
user = authenticate(username=username, password=password)
user = authenticate(**{'%s' % get_user_model().USERNAME_FIELD: username, 'password': password})
if user:
if not user.is_active:
@ -23,7 +27,7 @@ class AuthTokenSerializer(serializers.Serializer):
msg = _('Unable to log in with provided credentials.')
raise serializers.ValidationError(msg)
else:
msg = _('Must include "username" and "password".')
msg = _('Must include "%s" and "password".' % get_user_model().USERNAME_FIELD)
raise serializers.ValidationError(msg)
attrs['user'] = user