Passed
Push — master ( cb96a6...03dbbf )
by Piotr
01:10
created

RegistrationView.send_confirmation_email()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 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_field_name
10
11
from . import utils, signals
12
13
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...
14
15
16
class RootView(views.APIView):
17
    """
18
    Root endpoint - use one of sub endpoints.
19
    """
20
    permission_classes = (
21
        permissions.AllowAny,
22
    )
23
    urls_mapping = {
24
        'me': 'user',
25
        'register': 'register',
26
        'activate': 'activate',
27
        'change-' + User.USERNAME_FIELD: 'set_username',
28
        'change-password': 'set_password',
29
        'password-reset': 'password_reset',
30
        'password-reset-confirm': 'password_reset_confirm',
31
    }
32
    urls_extra_mapping = None
33
34
    def get_urls_mapping(self, **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...
35
        mapping = self.urls_mapping.copy()
36
        mapping.update(kwargs)
37
        if self.urls_extra_mapping:
38
            mapping.update(self.urls_extra_mapping)
39
        mapping.update(settings.ROOT_VIEW_URLS_MAPPING)
40
        return mapping
41
42
    def get(self, request, format=None):
0 ignored issues
show
Bug Best Practice introduced by
This seems to re-define the built-in format.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

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...
43
        return Response(
44
            dict([(key, reverse(url_name, request=request, format=format))
45
                  for key, url_name in self.get_urls_mapping().items()])
46
        )
47
48
49
class RegistrationView(generics.CreateAPIView):
50
    """
51
    Use this endpoint to register new user.
52
    """
53
    serializer_class = settings.SERIALIZERS.user_registration
54
    permission_classes = (
55
        permissions.AllowAny,
56
    )
57
58
    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...
59
        user = serializer.save()
60
        signals.user_registered.send(
61
            sender=self.__class__, user=user, request=self.request
62
        )
63
        email_factory_cls = None
64
        if settings.SEND_ACTIVATION_EMAIL:
65
            email_factory_cls = utils.UserActivationEmailFactory
66
        elif settings.SEND_CONFIRMATION_EMAIL:
67
            email_factory_cls = utils.UserConfirmationEmailFactory
68
69
        if email_factory_cls is not None:
70
            utils.send_email(self.request, email_factory_cls, user)
71
72
73
class LoginView(utils.ActionViewMixin, generics.GenericAPIView):
74
    """
75
    Use this endpoint to obtain user authentication token.
76
    """
77
    serializer_class = settings.SERIALIZERS.login
78
    permission_classes = (
79
        permissions.AllowAny,
80
    )
81
82
    def _action(self, serializer):
83
        token = utils.login_user(self.request, serializer.user)
84
        token_serializer_class = settings.SERIALIZERS.token
85
        return Response(
86
            data=token_serializer_class(token).data,
87
            status=status.HTTP_200_OK,
88
        )
89
90
91
class LogoutView(views.APIView):
92
    """
93
    Use this endpoint to logout user (remove user authentication token).
94
    """
95
    permission_classes = (
96
        permissions.IsAuthenticated,
97
    )
98
99
    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...
100
        utils.logout_user(request)
101
        return Response(status=status.HTTP_204_NO_CONTENT)
102
103
104
class PasswordResetView(utils.ActionViewMixin, generics.GenericAPIView):
105
    """
106
    Use this endpoint to send email to user with password reset link.
107
    """
108
    serializer_class = settings.SERIALIZERS.password_reset
109
    permission_classes = (
110
        permissions.AllowAny,
111
    )
112
113
    _users = None
114
115
    def _action(self, serializer):
116
        for user in self.get_users(serializer.data['email']):
117
            self.send_password_reset_email(user)
118
        return Response(status=status.HTTP_204_NO_CONTENT)
119
120
    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...
121
        if self._users is None:
122
            email_field_name = get_user_email_field_name(User)
123
            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...
124
                email_field_name + '__iexact': email
125
            })
126
            self._users = [
127
                u for u in users if u.is_active and u.has_usable_password()
128
            ]
129
        return self._users
130
131
    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...
132
        email_factory = utils.UserPasswordResetEmailFactory.from_request(
133
            self.request, user=user
134
        )
135
        email = email_factory.create()
136
        email.send()
137
138
139
class SetPasswordView(utils.ActionViewMixin, generics.GenericAPIView):
140
    """
141
    Use this endpoint to change user password.
142
    """
143
    permission_classes = (
144
        permissions.IsAuthenticated,
145
    )
146
147
    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...
148
        if settings.SET_PASSWORD_RETYPE:
149
            return settings.SERIALIZERS.set_password_retype
150
        return settings.SERIALIZERS.set_password
151
152
    def _action(self, serializer):
153
        self.request.user.set_password(serializer.data['new_password'])
154
        self.request.user.save()
155
156
        if settings.LOGOUT_ON_PASSWORD_CHANGE:
157
            utils.logout_user(self.request)
158
159
        return Response(status=status.HTTP_204_NO_CONTENT)
160
161
162
class PasswordResetConfirmView(utils.ActionViewMixin, generics.GenericAPIView):
163
    """
164
    Use this endpoint to finish reset password process.
165
    """
166
    permission_classes = (
167
        permissions.AllowAny,
168
    )
169
    token_generator = default_token_generator
170
171
    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...
172
        if settings.PASSWORD_RESET_CONFIRM_RETYPE:
173
            return settings.SERIALIZERS.password_reset_confirm_retype
174
        return settings.SERIALIZERS.password_reset_confirm
175
176
    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...
177
        serializer.user.set_password(serializer.data['new_password'])
178
        serializer.user.save()
179
        return Response(status=status.HTTP_204_NO_CONTENT)
180
181
182
class ActivationView(utils.ActionViewMixin, generics.GenericAPIView):
183
    """
184
    Use this endpoint to activate user account.
185
    """
186
    serializer_class = settings.SERIALIZERS.activation
187
    permission_classes = (
188
        permissions.AllowAny,
189
    )
190
    token_generator = default_token_generator
191
192
    def _action(self, serializer):
193
        serializer.user.is_active = True
194
        serializer.user.save()
195
        signals.user_activated.send(
196
            sender=self.__class__, user=serializer.user, request=self.request)
197
198
        if settings.SEND_CONFIRMATION_EMAIL:
199
            email_factory = utils.UserConfirmationEmailFactory.from_request(
200
                self.request, user=serializer.user)
201
            email = email_factory.create()
202
            email.send()
203
        return Response(status=status.HTTP_204_NO_CONTENT)
204
205
206
class SetUsernameView(utils.ActionViewMixin, generics.GenericAPIView):
207
    """
208
    Use this endpoint to change user username.
209
    """
210
    permission_classes = (permissions.IsAuthenticated,)
211
212
    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...
213
        if settings.SET_USERNAME_RETYPE:
214
            return settings.SERIALIZERS.set_username_retype
215
        return settings.SERIALIZERS.set_username
216
217
    def _action(self, serializer):
218
        user = self.request.user
219
        new_username = serializer.data['new_' + User.USERNAME_FIELD]
220
221
        setattr(user, User.USERNAME_FIELD, new_username)
222
        if settings.SEND_ACTIVATION_EMAIL:
223
            user.is_active = False
224
            email_factory_cls = utils.UserActivationEmailFactory
225
            utils.send_email(self.request, email_factory_cls, user)
226
        user.save()
227
228
        return Response(status=status.HTTP_204_NO_CONTENT)
229
230
231
class UserView(generics.RetrieveUpdateAPIView):
232
    """
233
    Use this endpoint to retrieve/update user.
234
    """
235
    model = User
236
    serializer_class = settings.SERIALIZERS.user
237
    permission_classes = (permissions.IsAuthenticated,)
238
239
    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...
240
        return self.request.user
241
242
    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...
243
        super(UserView, self).perform_update(serializer)
244
        user = serializer.instance
245
        if settings.SEND_ACTIVATION_EMAIL and not user.is_active:
246
            email_factory_cls = utils.UserActivationEmailFactory
247
            utils.send_email(self.request, email_factory_cls, user)
248