Passed
Push — master ( ca07be...1379b2 )
by Piotr
01:29
created

RootView   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
dl 0
loc 27
rs 10

3 Methods

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