aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/authtoken/serializers.py
diff options
context:
space:
mode:
authorTom Christie2013-01-07 21:04:52 +0000
committerTom Christie2013-01-07 21:04:52 +0000
commit36fa722ebb1b438b710b90fe470fbdbf82fd676e (patch)
tree9a837478ff46ebeed0b03fe9a430d72695cc2784 /rest_framework/authtoken/serializers.py
parent873a142af2f63084fd10bf35c13e79131837da07 (diff)
parente429f702e00ed807d68e90cd6a6af2749eb0b73e (diff)
downloaddjango-rest-framework-36fa722ebb1b438b710b90fe470fbdbf82fd676e.tar.bz2
Merged to latest master
Diffstat (limited to 'rest_framework/authtoken/serializers.py')
-rw-r--r--rest_framework/authtoken/serializers.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/rest_framework/authtoken/serializers.py b/rest_framework/authtoken/serializers.py
new file mode 100644
index 00000000..60a3740e
--- /dev/null
+++ b/rest_framework/authtoken/serializers.py
@@ -0,0 +1,24 @@
+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"')