From 1019c07027b27bc6cb6b616f2f2428c8012ec403 Mon Sep 17 00:00:00 2001 From: iliasgal Date: Wed, 25 Oct 2017 15:15:46 +0100 Subject: [PATCH] Fix is_active error message --- rest_framework_jwt/serializers.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/rest_framework_jwt/serializers.py b/rest_framework_jwt/serializers.py index 12b10a44..aa2aa2bf 100644 --- a/rest_framework_jwt/serializers.py +++ b/rest_framework_jwt/serializers.py @@ -5,6 +5,7 @@ from django.contrib.auth import authenticate, get_user_model from django.utils.translation import ugettext as _ +from django.core.exceptions import ObjectDoesNotExist from rest_framework import serializers from .compat import Serializer @@ -50,10 +51,6 @@ def validate(self, attrs): user = authenticate(**credentials) if user: - if not user.is_active: - msg = _('User account is disabled.') - raise serializers.ValidationError(msg) - payload = jwt_payload_handler(user) return { @@ -61,8 +58,14 @@ def validate(self, attrs): 'user': user } else: - msg = _('Unable to log in with provided credentials.') - raise serializers.ValidationError(msg) + try: + user = User.objects.get(**{self.username_field: attrs.get(self.username_field)}) + if not user.is_active: + msg = _('User account is disabled.') + raise serializers.ValidationError(msg) + except ObjectDoesNotExist: + msg = _('Unable to log in with provided credentials.') + raise serializers.ValidationError(msg) else: msg = _('Must include "{username_field}" and "password".') msg = msg.format(username_field=self.username_field)