diff options
| author | Stephan Groß | 2012-11-21 11:56:34 +0100 |
|---|---|---|
| committer | Stephan Groß | 2012-11-21 11:56:34 +0100 |
| commit | 6ba4df8a27016fd5e60a3852eea6c97231a03281 (patch) | |
| tree | 935bb3d51a9c3f4307d2d83d4b4c2e4d884886e3 /rest_framework/authtoken/serializers.py | |
| parent | ed713d0354b67bdc64de9346b9a72e1adfced76e (diff) | |
| parent | 3268c67343f6fc6364a0127a7bfabeb907a4751d (diff) | |
| download | django-rest-framework-6ba4df8a27016fd5e60a3852eea6c97231a03281.tar.bz2 | |
Merge remote-tracking branch 'upstream/master' into regex_field
Conflicts:
docs/topics/release-notes.md
Diffstat (limited to 'rest_framework/authtoken/serializers.py')
| -rw-r--r-- | rest_framework/authtoken/serializers.py | 24 |
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..a5ed6e6d --- /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"') + |
