Passed
Pull Request — master (#250)
by
unknown
59s
created

PasswordResetView   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 11
c 5
b 0
f 0
dl 0
loc 40
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A _action() 0 4 2
A get_origin_domain() 0 7 3
A send_password_reset_email() 0 7 1
B get_users() 0 10 5
1
from urlparse import urlparse
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...
2
3
from django.contrib.auth import get_user_model
0 ignored issues
show
introduced by
Unable to import 'django.contrib.auth'
Loading history...
4
from django.contrib.auth.tokens import default_token_generator
0 ignored issues
show
introduced by
Unable to import 'django.contrib.auth.tokens'
Loading history...
5
from django.urls.exceptions import NoReverseMatch
0 ignored issues
show
introduced by
Unable to import 'django.urls.exceptions'
Loading history...
6
7
from rest_framework import generics, permissions, status, views
0 ignored issues
show
introduced by
Unable to import 'rest_framework'
Loading history...
8
from rest_framework.response import Response
0 ignored issues
show
introduced by
Unable to import 'rest_framework.response'
Loading history...
9
from rest_framework.reverse import reverse
0 ignored issues
show
introduced by
Unable to import 'rest_framework.reverse'
Loading history...
10
11
from djoser import utils, signals
12
from djoser.compat import get_user_email, get_user_email_field_name
13
from djoser.conf import settings
14
15
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...
16
17
18
class RootView(views.APIView):
19
    """
20
    Root endpoint - use one of sub endpoints.
21
    """
22
    permission_classes = [permissions.AllowAny]
23
24
    def aggregate_djoser_urlpattern_names(self):
0 ignored issues
show
Coding Style Naming introduced by
The name aggregate_djoser_urlpattern_names does not conform to the method 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...
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...
25
        from djoser.urls import base, authtoken
26
        urlpattern_names = [pattern.name for pattern in base.urlpatterns]
27
        urlpattern_names += [pattern.name for pattern in authtoken.urlpatterns]
28
        urlpattern_names += self._get_jwt_urlpatterns()
29
30
        return urlpattern_names
31
32
    def get_urls_map(self, request, urlpattern_names, fmt):
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...
33
        urls_map = {}
34
        for urlpattern_name in urlpattern_names:
35
            try:
36
                url = reverse(urlpattern_name, request=request, format=fmt)
37
            except NoReverseMatch:
38
                url = ''
39
            urls_map[urlpattern_name] = url
40
        return urls_map
41
42
    def get(self, request, fmt=None):
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...
43
        urlpattern_names = self.aggregate_djoser_urlpattern_names()
44
        urls_map = self.get_urls_map(request, urlpattern_names, fmt)
45
        return Response(urls_map)
46
47
    def _get_jwt_urlpatterns(self):
0 ignored issues
show
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...
48
        try:
49
            from djoser.urls import jwt
50
            return [pattern.name for pattern in jwt.urlpatterns]
51
        except ImportError:
52
            return []
53
54
55
class UserCreateView(generics.CreateAPIView):
56
    """
57
    Use this endpoint to register new user.
58
    """
59
    serializer_class = settings.SERIALIZERS.user_create
60
    permission_classes = [permissions.AllowAny]
61
62 View Code Duplication
    def perform_create(self, serializer):
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...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
63
        user = serializer.save()
64
        signals.user_registered.send(
65
            sender=self.__class__, user=user, request=self.request
66
        )
67
68
        context = {'user': user}
69
        to = [get_user_email(user)]
0 ignored issues
show
Coding Style Naming introduced by
The name to 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...
70
        if settings.SEND_ACTIVATION_EMAIL:
71
            settings.EMAIL.activation(self.request, context).send(to)
72
        elif settings.SEND_CONFIRMATION_EMAIL:
73
            settings.EMAIL.confirmation(self.request, context).send(to)
74
75
76
class UserDeleteView(generics.CreateAPIView):
77
    """
78
    Use this endpoint to remove actually authenticated user
79
    """
80
    serializer_class = settings.SERIALIZERS.user_delete
81
    permission_classes = [permissions.IsAuthenticated]
82
83
    def get_object(self):
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...
84
        return self.request.user
85
86
    def post(self, request, *args, **kwargs):
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...
Unused Code introduced by
The argument kwargs seems to be unused.
Loading history...
Unused Code introduced by
The argument args seems to be unused.
Loading history...
87
        instance = self.get_object()
88
        serializer = self.get_serializer(instance, data=request.data)
89
        serializer.is_valid(raise_exception=True)
90
91
        utils.logout_user(self.request)
92
        instance.delete()
93
94
        return Response(status=status.HTTP_204_NO_CONTENT)
95
96
97
class TokenCreateView(utils.ActionViewMixin, generics.GenericAPIView):
98
    """
99
    Use this endpoint to obtain user authentication token.
100
    """
101
    serializer_class = settings.SERIALIZERS.token_create
102
    permission_classes = [permissions.AllowAny]
103
104
    def _action(self, serializer):
105
        token = utils.login_user(self.request, serializer.user)
106
        token_serializer_class = settings.SERIALIZERS.token
107
        return Response(
108
            data=token_serializer_class(token).data,
109
            status=status.HTTP_200_OK,
110
        )
111
112
113
class TokenDestroyView(views.APIView):
114
    """
115
    Use this endpoint to logout user (remove user authentication token).
116
    """
117
    permission_classes = [permissions.IsAuthenticated]
118
119
    def post(self, request):
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...
120
        utils.logout_user(request)
121
        return Response(status=status.HTTP_204_NO_CONTENT)
122
123
124
class PasswordResetView(utils.ActionViewMixin, generics.GenericAPIView):
125
    """
126
    Use this endpoint to send email to user with password reset link.
127
    """
128
    serializer_class = settings.SERIALIZERS.password_reset
129
    permission_classes = [permissions.AllowAny]
130
131
    _users = None
132
133
    def _action(self, serializer):
134
        for user in self.get_users(serializer.data['email']):
135
            self.send_password_reset_email(user)
136
        return Response(status=status.HTTP_204_NO_CONTENT)
137
138
    def get_origin_domain(self):
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...
139
        domain = self.request.META.get('HTTP_ORIGIN')
140
        if domain:
141
            domain = urlparse(domain)
142
            if domain in settings.PASSWORD_RESET_DOMAINS:
143
                return domain
144
        return None
145
        
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
146
    def get_users(self, email):
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...
147
        if self._users is None:
148
            email_field_name = get_user_email_field_name(User)
149
            users = User._default_manager.filter(**{
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _default_manager 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...
150
                email_field_name + '__iexact': email
151
            })
152
            self._users = [
153
                u for u in users if u.is_active and u.has_usable_password()
154
            ]
155
        return self._users
156
157
    def send_password_reset_email(self, user):
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...
158
        context = {
159
            'domain': self.get_origin_domain(),
160
            'user': user,
161
        }
162
        to = [get_user_email(user)]
0 ignored issues
show
Coding Style Naming introduced by
The name to 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...
163
        settings.EMAIL.password_reset(self.request, context).send(to)
164
165
166
class SetPasswordView(utils.ActionViewMixin, generics.GenericAPIView):
167
    """
168
    Use this endpoint to change user password.
169
    """
170
    permission_classes = [permissions.IsAuthenticated]
171
172
    def get_serializer_class(self):
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...
173
        if settings.SET_PASSWORD_RETYPE:
174
            return settings.SERIALIZERS.set_password_retype
175
        return settings.SERIALIZERS.set_password
176
177
    def _action(self, serializer):
178
        self.request.user.set_password(serializer.data['new_password'])
179
        self.request.user.save()
180
181
        if settings.LOGOUT_ON_PASSWORD_CHANGE:
182
            utils.logout_user(self.request)
183
184
        return Response(status=status.HTTP_204_NO_CONTENT)
185
186
187
class PasswordResetConfirmView(utils.ActionViewMixin, generics.GenericAPIView):
188
    """
189
    Use this endpoint to finish reset password process.
190
    """
191
    permission_classes = [permissions.AllowAny]
192
    token_generator = default_token_generator
193
194
    def get_serializer_class(self):
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...
195
        if settings.PASSWORD_RESET_CONFIRM_RETYPE:
196
            return settings.SERIALIZERS.password_reset_confirm_retype
197
        return settings.SERIALIZERS.password_reset_confirm
198
199
    def _action(self, serializer):
0 ignored issues
show
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...
200
        serializer.user.set_password(serializer.data['new_password'])
201
        serializer.user.save()
202
        return Response(status=status.HTTP_204_NO_CONTENT)
203
204
205
class ActivationView(utils.ActionViewMixin, generics.GenericAPIView):
206
    """
207
    Use this endpoint to activate user account.
208
    """
209
    serializer_class = settings.SERIALIZERS.activation
210
    permission_classes = [permissions.AllowAny]
211
    token_generator = default_token_generator
212
213 View Code Duplication
    def _action(self, serializer):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
214
        user = serializer.user
215
        user.is_active = True
216
        user.save()
217
218
        signals.user_activated.send(
219
            sender=self.__class__, user=user, request=self.request
220
        )
221
222
        if settings.SEND_CONFIRMATION_EMAIL:
223
            context = {'user': user}
224
            to = [get_user_email(user)]
0 ignored issues
show
Coding Style Naming introduced by
The name to 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...
225
            settings.EMAIL.confirmation(self.request, context).send(to)
226
227
        return Response(status=status.HTTP_204_NO_CONTENT)
228
229
230
class SetUsernameView(utils.ActionViewMixin, generics.GenericAPIView):
231
    """
232
    Use this endpoint to change user username.
233
    """
234
    permission_classes = [permissions.IsAuthenticated]
235
236
    def get_serializer_class(self):
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...
237
        if settings.SET_USERNAME_RETYPE:
238
            return settings.SERIALIZERS.set_username_retype
239
        return settings.SERIALIZERS.set_username
240
241
    def _action(self, serializer):
242
        user = self.request.user
243
        new_username = serializer.data['new_' + User.USERNAME_FIELD]
244
245
        setattr(user, User.USERNAME_FIELD, new_username)
246
        if settings.SEND_ACTIVATION_EMAIL:
247
            user.is_active = False
248
            context = {'user': user}
249
            to = [get_user_email(user)]
0 ignored issues
show
Coding Style Naming introduced by
The name to 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...
250
            settings.EMAIL.activation(self.request, context).send(to)
251
        user.save()
252
253
        return Response(status=status.HTTP_204_NO_CONTENT)
254
255
256
class UserView(generics.RetrieveUpdateAPIView):
257
    """
258
    Use this endpoint to retrieve/update user.
259
    """
260
    model = User
261
    serializer_class = settings.SERIALIZERS.user
262
    permission_classes = [permissions.IsAuthenticated]
263
264
    def get_object(self, *args, **kwargs):
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...
Unused Code introduced by
The argument args seems to be unused.
Loading history...
Unused Code introduced by
The argument kwargs seems to be unused.
Loading history...
265
        return self.request.user
266
267
    def perform_update(self, serializer):
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...
268
        super(UserView, self).perform_update(serializer)
269
        user = serializer.instance
270
        if settings.SEND_ACTIVATION_EMAIL and not user.is_active:
271
            context = {'user': user}
272
            to = [get_user_email(user)]
0 ignored issues
show
Coding Style Naming introduced by
The name to 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...
273
            settings.EMAIL.activation(self.request, context).send(to)
274