Passed
Pull Request — master (#259)
by Piotr
01:58
created

UserActivateSerializer   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 8
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 5 2
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.contrib.auth.tokens import default_token_generator
0 ignored issues
show
introduced by
Unable to import 'django.contrib.auth.tokens'
Loading history...
4
from django.core import exceptions as django_exceptions
0 ignored issues
show
introduced by
Unable to import 'django.core'
Loading history...
5
6
from rest_framework import serializers
0 ignored issues
show
introduced by
Unable to import 'rest_framework'
Loading history...
7
8
from djoser import constants, utils
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 = [User._meta.pk.name, User.USERNAME_FIELD]
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...
18
        read_only_fields = [User.USERNAME_FIELD]
19
20
21
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...
22
    password = serializers.CharField(
23
        style={'input_type': 'password'},
24
        write_only=True
25
    )
26
27
    default_error_messages = {
28
        'cannot_create_user': constants.CANNOT_CREATE_USER_ERROR,
29
    }
30
31
    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...
32
        model = User
33
        fields = [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...
34
35
    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...
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...
36
        user = User(**attrs)
37
        password = attrs.get('password')
38
39
        try:
40
            validate_password(password, user)
41
        except django_exceptions.ValidationError as e:
0 ignored issues
show
Coding Style Naming introduced by
The name e does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[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...
42
            raise serializers.ValidationError({'password': list(e.messages)})
43
44
        return attrs
45
46
47
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...
48
    password = serializers.CharField(
49
        required=False, style={'input_type': 'password'}
50
    )
51
52
    default_error_messages = {
53
        'invalid_credentials': constants.INVALID_CREDENTIALS_ERROR,
54
        'inactive_account': constants.INACTIVE_ACCOUNT_ERROR,
55
    }
56
57
    def __init__(self, *args, **kwargs):
58
        super(TokenCreateSerializer, self).__init__(*args, **kwargs)
59
        self.fields[User.USERNAME_FIELD] = serializers.CharField(
60
            required=False
61
        )
62
63
    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...
64
        attrs['user'] = authenticate(
65
            username=attrs.get(User.USERNAME_FIELD),
66
            password=attrs.get('password')
67
        )
68
69
        self._validate_user_exists(attrs['user'])
70
        self._validate_user_is_active(attrs['user'])
71
        return attrs
72
73
    def _validate_user_exists(self, user):
74
        if not user:
75
            self.fail('invalid_credentials')
76
77
    def _validate_user_is_active(self, user):
78
        if not user.is_active:
79
            self.fail('inactive_account')
80
81
82
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...
83
    email = serializers.EmailField()
84
85
    default_error_messages = {'email_not_found': constants.EMAIL_NOT_FOUND}
86
87
    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...
88
        users = utils.get_users_for_email(value)
89
        if settings.PASSWORD_RESET_SHOW_EMAIL_NOT_FOUND and not users:
90
            self.fail('email_not_found')
91
        else:
92
            return value
93
94
95
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...
96
    uid = serializers.CharField()
97
    token = serializers.CharField()
98
99
    default_error_messages = {
100
        'invalid_token': constants.INVALID_TOKEN_ERROR,
101
        'invalid_uid': constants.INVALID_UID_ERROR,
102
    }
103
104
    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...
105
        attrs = super(UidAndTokenSerializer, self).validate(attrs)
106
        try:
107
            uid = utils.decode_uid(attrs['uid'])
108
            user = User.objects.get(pk=uid)
109
        except (User.DoesNotExist, ValueError, TypeError, OverflowError):
110
            self.fail('invalid_uid')
111
112
        is_token_valid = default_token_generator.check_token(
113
            user, attrs['token']
114
        )
115
        if is_token_valid:
116
            attrs['user'] = user
117
            return attrs
118
        else:
119
            self.fail('invalid_token')
120
121
122
class UserActivateSerializer(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...
123
    default_error_messages = {'stale_token': constants.STALE_TOKEN_ERROR}
124
125
    def validate(self, attrs):
126
        attrs = super(UserActivateSerializer, self).validate(attrs)
127
        if not attrs['user'].is_active:
128
            return attrs
129
        raise self.fail('stale_token')
130
131
132
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...
133
    new_password = serializers.CharField(style={'input_type': 'password'})
134
135
    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...
136
        user = self.context['request'].user or self.user
137
        assert user is not None
138
139
        try:
140
            validate_password(attrs['new_password'], user)
141
        except django_exceptions.ValidationError as e:
0 ignored issues
show
Coding Style Naming introduced by
The name e does not conform to the variable naming conventions ((([a-z][a-z0-9_]{2,30})|(_[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...
142
            raise serializers.ValidationError({
143
                'new_password': list(e.messages)
144
            })
145
        return super(PasswordSerializer, self).validate(attrs)
146
147
148
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...
149
    re_new_password = serializers.CharField(style={'input_type': 'password'})
150
151
    default_error_messages = {
152
        'password_mismatch': constants.PASSWORD_MISMATCH_ERROR,
153
    }
154
155
    def validate(self, attrs):
156
        attrs = super(PasswordRetypeSerializer, self).validate(attrs)
157
        if attrs['new_password'] == attrs['re_new_password']:
158
            return attrs
159
        else:
160
            self.fail('password_mismatch')
161
162
163
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...
164
    current_password = serializers.CharField(style={'input_type': 'password'})
165
166
    default_error_messages = {
167
        'invalid_password': constants.INVALID_PASSWORD_ERROR,
168
    }
169
170
    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...
171
        is_password_valid = self.context['request'].user.check_password(value)
172
        if is_password_valid:
173
            return value
174
        else:
175
            self.fail('invalid_password')
176
177
178
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...
179
    pass
180
181
182
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...
183
                                  CurrentPasswordSerializer):
184
    pass
185
186
187
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...
188
                                     PasswordSerializer):
189
    pass
190
191
192
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...
193
                                           PasswordRetypeSerializer):
194
    pass
195
196
197
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...
198
    pass
199
200
201
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...
202
                            CurrentPasswordSerializer):
203
204
    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...
205
        model = User
206
        fields = (User.USERNAME_FIELD, 'current_password')
207
208
209
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...
210
    default_error_messages = {
211
        'username_mismatch': constants.USERNAME_MISMATCH_ERROR.format(
212
            User.USERNAME_FIELD
213
        ),
214
    }
215
216
    def __init__(self, *args, **kwargs):
217
        super(SetUsernameRetypeSerializer, self).__init__(*args, **kwargs)
218
        self.fields['re_' + User.USERNAME_FIELD] = serializers.CharField()
219
220
    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...
221
        attrs = super(SetUsernameRetypeSerializer, self).validate(attrs)
222
        new_username = attrs[User.USERNAME_FIELD]
223
        if new_username != attrs['re_' + User.USERNAME_FIELD]:
224
            self.fail('username_mismatch')
225
        else:
226
            return attrs
227
228
229
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...
230
    auth_token = serializers.CharField(source='key')
231
232
    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...
233
        model = settings.TOKEN_MODEL
234
        fields = ('auth_token',)
235