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

DjoserRouter._insert_routes()   A

Complexity

Conditions 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 19
rs 9.4285
1
from django.conf.urls import include, url
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
from django.contrib.auth import get_user_model
3
4
from rest_framework.routers import DefaultRouter, DynamicListRoute, Route
0 ignored issues
show
Unused Code introduced by
Unused DynamicListRoute imported from rest_framework.routers
Loading history...
5
6
from djoser import views
7
8
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...
9
10
11
class DjoserRouter(DefaultRouter):
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...
12
    def __init__(self, include_token_urls=False):
13
        super(DjoserRouter, self).__init__()
14
        self.trailing_slash = '/?'
15
16
        self._insert_routes()
17
        self._register_urls(include_token_urls)
18
19
    def _insert_routes(self):
20
        self.routes.insert(0, Route(
21
            url=r'^{prefix}{trailing_slash}$',
22
            mapping={
23
                'delete': 'remove_user',
24
                'get': 'me',
25
                'put': 'update',
26
            },
27
            name='{basename}-instance',
28
            initkwargs={'suffix': 'Instance'}
29
        ))
30
        self.routes.insert(0, Route(
31
            url=r'^{prefix}{trailing_slash}$',
32
            mapping={
33
                'delete': 'remove_token',
34
                'post': 'create',
35
            },
36
            name='{basename}-instance',
37
            initkwargs={'suffix': 'Instance'}
38
        ))
39
40
    def _register_urls(self, include_token_urls):
41
        if include_token_urls:
42
            self.register(
43
                r'^token',
44
                views.TokenViewSet,
45
                base_name='token',
46
            )
47
48
        self.register(
49
            r'^user/{}'.format(User.USERNAME_FIELD),
50
            views.UsernameUpdateViewSet,
51
            base_name='username-update',
52
        )
53
        self.register(
54
            r'^user/password',
55
            views.PasswordUpdateViewSet,
56
            base_name='password-update',
57
        )
58
        self.register(
59
            r'^password/reset',
60
            views.PasswordResetViewSet,
61
            base_name='password-reset',
62
        )
63
        self.register(
64
            r'^password/reset/confirm',
65
            views.PasswordResetConfirmViewSet,
66
            base_name='password-reset-confirm',
67
        )
68
        self.register(
69
            r'^user',
70
            views.UserViewSet,
71
            base_name='user',
72
        )
73
        self.register(
74
            r'^users',
75
            views.UsersViewSet,
76
            base_name='users'
77
        )
78
        self.register(
79
            r'^users/activate',
80
            views.UserActivateViewSet,
81
            base_name='user-activate',
82
        )
83
84
85
router = DjoserRouter()
0 ignored issues
show
Coding Style Naming introduced by
The name router 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...
86
87
88
urlpatterns = [
0 ignored issues
show
Coding Style Naming introduced by
The name urlpatterns 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...
89
    url(r'^', include(router.urls)),
90
]
91