Passed
Push — master ( ac3317...527fbd )
by Piotr
01:06
created

UserDeleteView   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get_object() 0 2 1
A destroy() 0 9 1
1
from django.contrib.auth import 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.tokens import default_token_generator
0 ignored issues
show
introduced by
Unable to import 'django.contrib.auth.tokens'
Loading history...
3
4
from rest_framework import generics, permissions, status, views
0 ignored issues
show
introduced by
Unable to import 'rest_framework'
Loading history...
5
from rest_framework.response import Response
0 ignored issues
show
introduced by
Unable to import 'rest_framework.response'
Loading history...
6
from rest_framework.reverse import reverse
0 ignored issues
show
introduced by
Unable to import 'rest_framework.reverse'
Loading history...
7
8
from djoser.conf import settings
9
from djoser.compat import get_user_email, get_user_email_field_name
10
11
from djoser import email, utils, signals
12
from djoser.compat import NoReverseMatch
13
14
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...
15
16
17
class RootView(views.APIView):
18
    """
19
    Root endpoint - use one of sub endpoints.
20
    """
21
    permission_classes = [permissions.AllowAny]
22
23
    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...
24
        from djoser.urls import base, authtoken
25
        urlpattern_names = [pattern.name for pattern in base.urlpatterns]
26
        urlpattern_names += [pattern.name for pattern in authtoken.urlpatterns]
27
        urlpattern_names += self._get_jwt_urlpatterns()
28
29
        return urlpattern_names
30
31
    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...
32
        urls_map = {}
33
        for urlpattern_name in urlpattern_names:
34
            try:
35
                url = reverse(urlpattern_name, request=request, format=fmt)
36
            except NoReverseMatch:
37
                url = ''
38
            urls_map[urlpattern_name] = url
39
        return urls_map
40
41
    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...
42
        urlpattern_names = self.aggregate_djoser_urlpattern_names()
43
        urls_map = self.get_urls_map(request, urlpattern_names, fmt)
44
        return Response(urls_map)
45
46
    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...
47
        try:
48
            from djoser.urls import jwt
49
            return [pattern.name for pattern in jwt.urlpatterns]
50
        except ImportError:
51
            return []
52
53
54
class UserCreateView(generics.CreateAPIView):
55 View Code Duplication
    """
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
56
    Use this endpoint to register new user.
57
    """
58
    serializer_class = settings.SERIALIZERS.user_registration
59
    permission_classes = (permissions.AllowAny,)
60
61
    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...
62
        user = serializer.save()
63
        signals.user_registered.send(
64
            sender=self.__class__, user=user, request=self.request
65
        )
66
67
        context = {'user': user}
68
        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...
69
        if settings.SEND_ACTIVATION_EMAIL:
70
            email.ActivationEmail(self.request, context).send(to)
71
        elif settings.SEND_CONFIRMATION_EMAIL:
72
            email.ConfirmationEmail(self.request, context).send(to)
73
74
75
class UserDeleteView(generics.DestroyAPIView):
76
    """
77
    Use this endpoint to remove actually authenticated user
78
    """
79
    serializer_class = settings.SERIALIZERS.user_delete
80
    permission_classes = (permissions.IsAuthenticated,)
81
82
    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...
83
        return self.request.user
84
85
    def destroy(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...
86
        instance = self.get_object()
87
        serializer = self.get_serializer(instance, data=request.data)
88
        serializer.is_valid(raise_exception=True)
89
90
        utils.logout_user(self.request)
91
        instance.delete()
92
93
        return Response(status=status.HTTP_204_NO_CONTENT)
94
95
96
class TokenCreateView(utils.ActionViewMixin, generics.GenericAPIView):
97
    """
98
    Use this endpoint to obtain user authentication token.
99
    """
100
    serializer_class = settings.SERIALIZERS.login
101
    permission_classes = (
102
        permissions.AllowAny,
103
    )
104
105
    def _action(self, serializer):
106
        token = utils.login_user(self.request, serializer.user)
107
        token_serializer_class = settings.SERIALIZERS.token
108
        return Response(
109
            data=token_serializer_class(token).data,
110
            status=status.HTTP_200_OK,
111
        )
112
113
114
class TokenDestroyView(views.APIView):
115
    """
116
    Use this endpoint to logout user (remove user authentication token).
117
    """
118
    permission_classes = (
119
        permissions.IsAuthenticated,
120
    )
121
122
    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...
123
        utils.logout_user(request)
124
        return Response(status=status.HTTP_204_NO_CONTENT)
125
126
127
class PasswordResetView(utils.ActionViewMixin, generics.GenericAPIView):
128
    """
129
    Use this endpoint to send email to user with password reset link.
130
    """
131
    serializer_class = settings.SERIALIZERS.password_reset
132
    permission_classes = (
133
        permissions.AllowAny,
134
    )
135
136
    _users = None
137
138
    def _action(self, serializer):
139
        for user in self.get_users(serializer.data['email']):
140
            self.send_password_reset_email(user)
141
        return Response(status=status.HTTP_204_NO_CONTENT)
142
143
    def get_users(self, email):
0 ignored issues
show
Comprehensibility Bug introduced by
email is re-defining a name which is already available in the outer-scope (previously defined on line 11).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
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...
144
        if self._users is None:
145
            email_field_name = get_user_email_field_name(User)
146
            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...
147
                email_field_name + '__iexact': email
148
            })
149
            self._users = [
150
                u for u in users if u.is_active and u.has_usable_password()
151
            ]
152
        return self._users
153
154
    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...
155
        context = {'user': user}
156
        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...
157
        email.PasswordResetEmail(self.request, context).send(to)
158
159
160
class SetPasswordView(utils.ActionViewMixin, generics.GenericAPIView):
161
    """
162
    Use this endpoint to change user password.
163
    """
164
    permission_classes = (
165
        permissions.IsAuthenticated,
166
    )
167
168
    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...
169
        if settings.SET_PASSWORD_RETYPE:
170
            return settings.SERIALIZERS.set_password_retype
171
        return settings.SERIALIZERS.set_password
172
173
    def _action(self, serializer):
174
        self.request.user.set_password(serializer.data['new_password'])
175
        self.request.user.save()
176
177
        if settings.LOGOUT_ON_PASSWORD_CHANGE:
178
            utils.logout_user(self.request)
179
180
        return Response(status=status.HTTP_204_NO_CONTENT)
181
182
183
class PasswordResetConfirmView(utils.ActionViewMixin, generics.GenericAPIView):
184
    """
185
    Use this endpoint to finish reset password process.
186 View Code Duplication
    """
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
187
    permission_classes = (
188
        permissions.AllowAny,
189
    )
190
    token_generator = default_token_generator
191
192
    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...
193
        if settings.PASSWORD_RESET_CONFIRM_RETYPE:
194
            return settings.SERIALIZERS.password_reset_confirm_retype
195
        return settings.SERIALIZERS.password_reset_confirm
196
197
    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...
198
        serializer.user.set_password(serializer.data['new_password'])
199
        serializer.user.save()
200
        return Response(status=status.HTTP_204_NO_CONTENT)
201
202
203
class ActivationView(utils.ActionViewMixin, generics.GenericAPIView):
204
    """
205
    Use this endpoint to activate user account.
206
    """
207
    serializer_class = settings.SERIALIZERS.activation
208
    permission_classes = (
209
        permissions.AllowAny,
210
    )
211
    token_generator = default_token_generator
212
213
    def _action(self, serializer):
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
            email.ConfirmationEmail(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
            email.ActivationEmail(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
            email.ActivationEmail(self.request, context).send(to)
274