-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored TokenCreateSerializer added validation errors
- Loading branch information
Showing
5 changed files
with
53 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from django.contrib.auth import authenticate, get_user_model | ||
from djoser.conf import settings | ||
from djoser.serializers import TokenCreateSerializer | ||
from rest_framework.validators import ValidationError | ||
|
||
User = get_user_model() | ||
|
||
|
||
class CustomTokenCreateSerializer(TokenCreateSerializer): | ||
default_error_messages = { | ||
'invalid_credentials': ( | ||
settings.CONSTANTS.messages.INVALID_CREDENTIALS_ERROR | ||
), | ||
'inactive_account': settings.CONSTANTS.messages.INACTIVE_ACCOUNT_ERROR, | ||
'missing_account': settings.CONSTANTS.messages.EMAIL_NOT_FOUND, | ||
'wrong_password': settings.CONSTANTS.messages.INVALID_PASSWORD_ERROR, | ||
} | ||
|
||
def validate(self, attrs): | ||
password = attrs.get('password') | ||
params = {settings.LOGIN_FIELD: attrs.get(settings.LOGIN_FIELD)} | ||
self.user = User.objects.filter(**params).first() | ||
if not self.user: | ||
raise ValidationError( | ||
{ | ||
settings.LOGIN_FIELD: | ||
self.default_error_messages.get('missing_account') | ||
}, | ||
) | ||
elif not self.user.check_password(password): | ||
raise ValidationError( | ||
{ | ||
'password': | ||
self.default_error_messages.get('wrong_password') | ||
}, | ||
) | ||
self.user = authenticate( | ||
request=self.context.get('request'), **params, password=password | ||
) | ||
if self.user and self.user.is_active: | ||
return attrs | ||
raise ValidationError( | ||
{ | ||
'not_active': | ||
self.default_error_messages.get('inactive_account') | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters