2012-11-11 04:09:14 +04:00
|
|
|
from django.contrib.auth import authenticate
|
2014-05-01 13:18:16 +04:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2014-10-10 17:16:09 +04:00
|
|
|
from rest_framework import exceptions, serializers
|
2012-11-11 04:09:14 +04:00
|
|
|
|
2012-12-08 02:25:16 +04:00
|
|
|
|
2012-11-11 04:09:14 +04:00
|
|
|
class AuthTokenSerializer(serializers.Serializer):
|
2012-11-13 03:16:53 +04:00
|
|
|
username = serializers.CharField()
|
2012-11-11 04:09:14 +04:00
|
|
|
password = serializers.CharField()
|
|
|
|
|
|
|
|
def validate(self, attrs):
|
|
|
|
username = attrs.get('username')
|
|
|
|
password = attrs.get('password')
|
|
|
|
|
|
|
|
if username and password:
|
|
|
|
user = authenticate(username=username, password=password)
|
|
|
|
|
|
|
|
if user:
|
|
|
|
if not user.is_active:
|
2014-05-01 13:18:16 +04:00
|
|
|
msg = _('User account is disabled.')
|
2014-10-10 17:16:09 +04:00
|
|
|
raise exceptions.ValidationFailed(msg)
|
2012-11-11 04:09:14 +04:00
|
|
|
else:
|
2014-09-11 07:27:52 +04:00
|
|
|
msg = _('Unable to log in with provided credentials.')
|
2014-10-10 17:16:09 +04:00
|
|
|
raise exceptions.ValidationFailed(msg)
|
2012-11-11 04:09:14 +04:00
|
|
|
else:
|
2014-05-01 13:18:16 +04:00
|
|
|
msg = _('Must include "username" and "password"')
|
2014-10-10 17:16:09 +04:00
|
|
|
raise exceptions.ValidationFailed(msg)
|
2014-09-02 20:41:23 +04:00
|
|
|
|
|
|
|
attrs['user'] = user
|
|
|
|
return attrs
|