1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-09 02:47:51 +00:00
seahub/thirdpart/rest_framework/authtoken/serializers.py

25 lines
860 B
Python
Raw Normal View History

2013-03-01 05:37:19 +00:00
from django.contrib.auth import authenticate
from rest_framework import serializers
class AuthTokenSerializer(serializers.Serializer):
username = serializers.CharField()
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"')