2018-07-06 13:14:31 +03:00
|
|
|
from django.contrib.auth import authenticate
|
Replace all usage ugettext functions with the non-u versions (#6634)
On Python 3, the ugettext functions are a simple aliases of their non-u
counterparts (the 'u' represents Python 2 unicode type). Starting with
Django 3.0, the u versions will be deprecated.
https://docs.djangoproject.com/en/dev/releases/3.0/#id2
> django.utils.translation.ugettext(), ugettext_lazy(), ugettext_noop(),
> ungettext(), and ungettext_lazy() are deprecated in favor of the
> functions that they’re aliases for:
> django.utils.translation.gettext(), gettext_lazy(), gettext_noop(),
> ngettext(), and ngettext_lazy().
2019-05-01 08:49:54 +03:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2014-05-01 13:18:16 +04:00
|
|
|
|
2015-10-01 05:09:37 +03:00
|
|
|
from rest_framework import serializers
|
2012-11-11 04:09:14 +04:00
|
|
|
|
2012-12-08 02:25:16 +04:00
|
|
|
|
2012-11-11 04:09:14 +04:00
|
|
|
class AuthTokenSerializer(serializers.Serializer):
|
2020-03-03 15:27:34 +03:00
|
|
|
username = serializers.CharField(
|
|
|
|
label=_("Username"),
|
|
|
|
write_only=True
|
|
|
|
)
|
2017-05-17 22:17:55 +03:00
|
|
|
password = serializers.CharField(
|
|
|
|
label=_("Password"),
|
|
|
|
style={'input_type': 'password'},
|
2020-03-03 15:27:34 +03:00
|
|
|
trim_whitespace=False,
|
|
|
|
write_only=True
|
|
|
|
)
|
|
|
|
token = serializers.CharField(
|
|
|
|
label=_("Token"),
|
|
|
|
read_only=True
|
2017-05-17 22:17:55 +03:00
|
|
|
)
|
2012-11-11 04:09:14 +04:00
|
|
|
|
|
|
|
def validate(self, attrs):
|
|
|
|
username = attrs.get('username')
|
|
|
|
password = attrs.get('password')
|
|
|
|
|
|
|
|
if username and password:
|
2017-10-05 12:43:49 +03:00
|
|
|
user = authenticate(request=self.context.get('request'),
|
|
|
|
username=username, password=password)
|
2012-11-11 04:09:14 +04:00
|
|
|
|
2017-11-20 11:35:54 +03:00
|
|
|
# The authenticate call simply returns None for is_active=False
|
|
|
|
# users. (Assuming the default ModelBackend authentication
|
|
|
|
# backend.)
|
|
|
|
if not user:
|
2015-01-07 15:46:23 +03:00
|
|
|
msg = _('Unable to log in with provided credentials.')
|
2016-10-11 12:25:21 +03:00
|
|
|
raise serializers.ValidationError(msg, code='authorization')
|
2012-11-11 04:09:14 +04:00
|
|
|
else:
|
2015-01-07 15:46:23 +03:00
|
|
|
msg = _('Must include "username" and "password".')
|
2016-10-11 12:25:21 +03:00
|
|
|
raise serializers.ValidationError(msg, code='authorization')
|
2014-09-02 20:41:23 +04:00
|
|
|
|
|
|
|
attrs['user'] = user
|
|
|
|
return attrs
|