2012-11-11 04:09:14 +04:00
|
|
|
from django.contrib.auth import authenticate
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
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:
|
|
|
|
raise serializers.ValidationError('User account is disabled.')
|
|
|
|
attrs['user'] = user
|
|
|
|
return attrs
|
|
|
|
else:
|
|
|
|
raise serializers.ValidationError('Unable to login with provided credentials.')
|
|
|
|
else:
|
|
|
|
raise serializers.ValidationError('Must include "username" and "password"')
|