/usr/lib/python2.7/dist-packages/djoser/serializers.py is in python-djoser 0.4.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | from django.contrib.auth import authenticate, get_user_model
from rest_framework import serializers
from rest_framework.authtoken.models import Token
from . import constants, utils
User = get_user_model()
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = tuple(User.REQUIRED_FIELDS) + (
User._meta.pk.name,
User.USERNAME_FIELD,
)
read_only_fields = (
User.USERNAME_FIELD,
)
class UserRegistrationSerializer(serializers.ModelSerializer):
password = serializers.CharField(style={'input_type': 'password'}, write_only=True)
class Meta:
model = User
fields = tuple(User.REQUIRED_FIELDS) + (
User.USERNAME_FIELD,
User._meta.pk.name,
'password',
)
def create(self, validated_data):
return User.objects.create_user(**validated_data)
class LoginSerializer(serializers.Serializer):
password = serializers.CharField(required=False, style={'input_type': 'password'})
default_error_messages = {
'inactive_account': constants.INACTIVE_ACCOUNT_ERROR,
'invalid_credentials': constants.INVALID_CREDENTIALS_ERROR,
}
def __init__(self, *args, **kwargs):
super(LoginSerializer, self).__init__(*args, **kwargs)
self.user = None
self.fields[User.USERNAME_FIELD] = serializers.CharField(required=False)
def validate(self, attrs):
self.user = authenticate(username=attrs.get(User.USERNAME_FIELD), password=attrs.get('password'))
if self.user:
if not self.user.is_active:
raise serializers.ValidationError(self.error_messages['inactive_account'])
return attrs
else:
raise serializers.ValidationError(self.error_messages['invalid_credentials'])
class PasswordResetSerializer(serializers.Serializer):
email = serializers.EmailField()
class UidAndTokenSerializer(serializers.Serializer):
uid = serializers.CharField()
token = serializers.CharField()
default_error_messages = {
'invalid_token': constants.INVALID_TOKEN_ERROR
}
def validate_uid(self, value):
try:
uid = utils.decode_uid(value)
self.user = User.objects.get(pk=uid)
except (User.DoesNotExist, ValueError, TypeError, OverflowError) as error:
raise serializers.ValidationError(error)
return value
def validate(self, attrs):
attrs = super(UidAndTokenSerializer, self).validate(attrs)
if not self.context['view'].token_generator.check_token(self.user, attrs['token']):
raise serializers.ValidationError(self.error_messages['invalid_token'])
return attrs
class PasswordSerializer(serializers.Serializer):
new_password = serializers.CharField(style={'input_type': 'password'})
class PasswordRetypeSerializer(PasswordSerializer):
re_new_password = serializers.CharField(style={'input_type': 'password'})
default_error_messages = {
'password_mismatch': constants.PASSWORD_MISMATCH_ERROR,
}
def validate(self, attrs):
attrs = super(PasswordRetypeSerializer, self).validate(attrs)
if attrs['new_password'] != attrs['re_new_password']:
raise serializers.ValidationError(self.error_messages['password_mismatch'])
return attrs
class CurrentPasswordSerializer(serializers.Serializer):
current_password = serializers.CharField(style={'input_type': 'password'})
default_error_messages = {
'invalid_password': constants.INVALID_PASSWORD_ERROR,
}
def validate_current_password(self, value):
if not self.context['request'].user.check_password(value):
raise serializers.ValidationError(self.error_messages['invalid_password'])
return value
class SetPasswordSerializer(PasswordSerializer, CurrentPasswordSerializer):
pass
class SetPasswordRetypeSerializer(PasswordRetypeSerializer, CurrentPasswordSerializer):
pass
class PasswordResetConfirmSerializer(UidAndTokenSerializer, PasswordSerializer):
pass
class PasswordResetConfirmRetypeSerializer(UidAndTokenSerializer, PasswordRetypeSerializer):
pass
class SetUsernameSerializer(serializers.ModelSerializer, CurrentPasswordSerializer):
class Meta(object):
model = User
fields = (
User.USERNAME_FIELD,
'current_password',
)
def __init__(self, *args, **kwargs):
super(SetUsernameSerializer, self).__init__(*args, **kwargs)
self.fields['new_' + User.USERNAME_FIELD] = self.fields[User.USERNAME_FIELD]
del self.fields[User.USERNAME_FIELD]
class SetUsernameRetypeSerializer(SetUsernameSerializer):
default_error_messages = {
'username_mismatch': constants.USERNAME_MISMATCH_ERROR.format(User.USERNAME_FIELD),
}
def __init__(self, *args, **kwargs):
super(SetUsernameRetypeSerializer, self).__init__(*args, **kwargs)
self.fields['re_new_' + User.USERNAME_FIELD] = serializers.CharField()
def validate(self, attrs):
attrs = super(SetUsernameRetypeSerializer, self).validate(attrs)
new_username = attrs[User.USERNAME_FIELD]
if new_username != attrs['re_new_' + User.USERNAME_FIELD]:
raise serializers.ValidationError(self.error_messages['username_mismatch'].format(User.USERNAME_FIELD))
return attrs
class TokenSerializer(serializers.ModelSerializer):
auth_token = serializers.CharField(source='key')
class Meta:
model = Token
fields = (
'auth_token',
)
|