Passed
Push — master ( aa7a49...bfcb4c )
by Piotr
01:19
created

UserCreateSerializer.validate_password()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
1
from django.contrib.auth import authenticate, get_user_model
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
introduced by
Unable to import 'django.contrib.auth'
Loading history...
2
from django.contrib.auth.password_validation import validate_password
0 ignored issues
show
introduced by
Unable to import 'django.contrib.auth.password_validation'
Loading history...
3
from django.db import IntegrityError, transaction
0 ignored issues
show
introduced by
Unable to import 'django.db'
Loading history...
4
5
from rest_framework import exceptions, serializers
0 ignored issues
show
introduced by
Unable to import 'rest_framework'
Loading history...
6
7
from djoser import constants, utils
8
from djoser.compat import get_user_email, get_user_email_field_name
9
from djoser.conf import settings
10
11
User = get_user_model()
0 ignored issues
show
Coding Style Naming introduced by
The name User does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
12
13
14
class UserSerializer(serializers.ModelSerializer):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
15
    class Meta:
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
introduced by
Old-style class defined.
Loading history...
Coding Style introduced by
This class has no __init__ method.
Loading history...
16
        model = User
17
        fields = tuple(User.REQUIRED_FIELDS) + (
18
            User._meta.pk.name,
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _meta was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
19
            User.USERNAME_FIELD,
20
        )
21
        read_only_fields = (User.USERNAME_FIELD,)
22
23
    def update(self, instance, validated_data):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
24
        email_field = get_user_email_field_name(User)
25
        if settings.SEND_ACTIVATION_EMAIL and email_field in validated_data:
26
            instance_email = get_user_email(instance)
27
            if instance_email != validated_data[email_field]:
28
                instance.is_active = False
29
                instance.save(update_fields=['is_active'])
30
        return super(UserSerializer, self).update(instance, validated_data)
31
32
33
class UserCreateSerializer(serializers.ModelSerializer):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
34
    password = serializers.CharField(
35
        style={'input_type': 'password'},
36
        write_only=True
37
    )
38
39
    default_error_messages = {
40
        'cannot_create_user': constants.CANNOT_CREATE_USER_ERROR,
41
    }
42
43
    class Meta:
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
introduced by
Old-style class defined.
Loading history...
Coding Style introduced by
This class has no __init__ method.
Loading history...
44
        model = User
45
        fields = tuple(User.REQUIRED_FIELDS) + (
46
            User.USERNAME_FIELD, User._meta.pk.name, 'password',
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _meta was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
47
        )
48
49
    def validate_password(self, value):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
50
        validate_password(value)
51
        return value
52
53
    def create(self, validated_data):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
54
        try:
55
            user = self.perform_create(validated_data)
56
        except IntegrityError:
57
            self.fail('cannot_create_user')
58
59
        return user
60
61
    def perform_create(self, validated_data):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
62
        with transaction.atomic():
63
            user = User.objects.create_user(**validated_data)
64
            if settings.SEND_ACTIVATION_EMAIL:
65
                user.is_active = False
66
                user.save(update_fields=['is_active'])
67
        return user
68
69
70
class TokenCreateSerializer(serializers.Serializer):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
71
    password = serializers.CharField(
72
        required=False, style={'input_type': 'password'}
73
    )
74
75
    default_error_messages = {
76
        'invalid_credentials': constants.INVALID_CREDENTIALS_ERROR,
77
        'inactive_account': constants.INACTIVE_ACCOUNT_ERROR,
78
    }
79
80
    def __init__(self, *args, **kwargs):
81
        super(TokenCreateSerializer, self).__init__(*args, **kwargs)
82
        self.user = None
83
        self.fields[User.USERNAME_FIELD] = serializers.CharField(
84
            required=False
85
        )
86
87
    def validate(self, attrs):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
88
        self.user = authenticate(
89
            username=attrs.get(User.USERNAME_FIELD),
90
            password=attrs.get('password')
91
        )
92
93
        self._validate_user_exists(self.user)
94
        self._validate_user_is_active(self.user)
95
        return attrs
96
97
    def _validate_user_exists(self, user):
98
        if not user:
99
            self.fail('invalid_credentials')
100
101
    def _validate_user_is_active(self, user):
102
        if not user.is_active:
103
            self.fail('inactive_account')
104
105
106
class PasswordResetSerializer(serializers.Serializer):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
107
    email = serializers.EmailField()
108
109
    default_error_messages = {'email_not_found': constants.EMAIL_NOT_FOUND}
110
111
    def validate_email(self, value):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
112
        users = self.context['view'].get_users(value)
113
        if settings.PASSWORD_RESET_SHOW_EMAIL_NOT_FOUND and not users:
114
            self.fail('email_not_found')
115
        else:
116
            return value
117
118
119
class UidAndTokenSerializer(serializers.Serializer):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
120
    uid = serializers.CharField()
121
    token = serializers.CharField()
122
123
    default_error_messages = {
124
        'invalid_token': constants.INVALID_TOKEN_ERROR,
125
        'invalid_uid': constants.INVALID_UID_ERROR,
126
    }
127
128
    def validate_uid(self, value):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
129
        try:
130
            uid = utils.decode_uid(value)
131
            self.user = User.objects.get(pk=uid)
0 ignored issues
show
Coding Style introduced by
The attribute user was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
132
        except (User.DoesNotExist, ValueError, TypeError, OverflowError):
133
            self.fail('invalid_uid')
134
135
        return value
136
137
    def validate(self, attrs):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
138
        attrs = super(UidAndTokenSerializer, self).validate(attrs)
139
        is_token_valid = self.context['view'].token_generator.check_token(
140
            self.user, attrs['token']
141
        )
142
        if is_token_valid:
143
            return attrs
144
        else:
145
            self.fail('invalid_token')
146
147
148
class ActivationSerializer(UidAndTokenSerializer):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
149
    default_error_messages = {'stale_token': constants.STALE_TOKEN_ERROR}
150
151
    def validate(self, attrs):
152
        attrs = super(ActivationSerializer, self).validate(attrs)
153
        if not self.user.is_active:
154
            return attrs
155
        raise exceptions.PermissionDenied(self.error_messages['stale_token'])
156
157
158
class PasswordSerializer(serializers.Serializer):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
159
    new_password = serializers.CharField(style={'input_type': 'password'})
160
161
    def validate_new_password(self, value):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
162
        validate_password(value)
163
        return value
164
165
166
class PasswordRetypeSerializer(PasswordSerializer):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
167
    re_new_password = serializers.CharField(style={'input_type': 'password'})
168
169
    default_error_messages = {
170
        'password_mismatch': constants.PASSWORD_MISMATCH_ERROR,
171
    }
172
173
    def validate(self, attrs):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
174
        attrs = super(PasswordRetypeSerializer, self).validate(attrs)
175
        if attrs['new_password'] == attrs['re_new_password']:
176
            return attrs
177
        else:
178
            self.fail('password_mismatch')
179
180
181
class CurrentPasswordSerializer(serializers.Serializer):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
182
    current_password = serializers.CharField(style={'input_type': 'password'})
183
184
    default_error_messages = {
185
        'invalid_password': constants.INVALID_PASSWORD_ERROR,
186
    }
187
188
    def validate_current_password(self, value):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
189
        is_password_valid = self.context['request'].user.check_password(value)
190
        if is_password_valid:
191
            return value
192
        else:
193
            self.fail('invalid_password')
194
195
196
class SetPasswordSerializer(PasswordSerializer, CurrentPasswordSerializer):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
197
    pass
198
199
200
class SetPasswordRetypeSerializer(PasswordRetypeSerializer,
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
201
                                  CurrentPasswordSerializer):
202
    pass
203
204
205
class PasswordResetConfirmSerializer(UidAndTokenSerializer,
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
206
                                     PasswordSerializer):
207
    pass
208
209
210
class PasswordResetConfirmRetypeSerializer(UidAndTokenSerializer,
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
211
                                           PasswordRetypeSerializer):
212
    pass
213
214
215
class UserDeleteSerializer(CurrentPasswordSerializer):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
216
    pass
217
218
219
class SetUsernameSerializer(serializers.ModelSerializer,
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
220
                            CurrentPasswordSerializer):
221
222
    class Meta(object):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
223
        model = User
224
        fields = (User.USERNAME_FIELD, 'current_password')
225
226
    def __init__(self, *args, **kwargs):
227
        """
228
        This method should probably be replaced by a better solution.
229
        Its purpose is to replace USERNAME_FIELD with 'new_' + USERNAME_FIELD
230
        so that the new field is being assigned a field for USERNAME_FIELD
231
        """
232
        super(SetUsernameSerializer, self).__init__(*args, **kwargs)
233
        username_field = User.USERNAME_FIELD
234
        self.fields['new_' + username_field] = self.fields.pop(username_field)
235
236
237
class SetUsernameRetypeSerializer(SetUsernameSerializer):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
238
    default_error_messages = {
239
        'username_mismatch': constants.USERNAME_MISMATCH_ERROR.format(
240
            User.USERNAME_FIELD
241
        ),
242
    }
243
244
    def __init__(self, *args, **kwargs):
245
        super(SetUsernameRetypeSerializer, self).__init__(*args, **kwargs)
246
        self.fields['re_new_' + User.USERNAME_FIELD] = serializers.CharField()
247
248
    def validate(self, attrs):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
249
        attrs = super(SetUsernameRetypeSerializer, self).validate(attrs)
250
        new_username = attrs[User.USERNAME_FIELD]
251
        if new_username != attrs['re_new_' + User.USERNAME_FIELD]:
252
            self.fail('username_mismatch')
253
        else:
254
            return attrs
255
256
257
class TokenSerializer(serializers.ModelSerializer):
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
258
    auth_token = serializers.CharField(source='key')
259
260
    class Meta:
0 ignored issues
show
Coding Style introduced by
This class should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
introduced by
Old-style class defined.
Loading history...
Coding Style introduced by
This class has no __init__ method.
Loading history...
261
        model = settings.TOKEN_MODEL
262
        fields = ('auth_token',)
263