Passed
Push — master ( a3a157...5d4c79 )
by Alexander
02:21
created

AuthUserChecker.visit_importfrom()   A

Complexity

Conditions 4

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 4
nop 2
1
# Copyright (c) 2018 Alexander Todorov <[email protected]>
2
3
# Licensed under the GPL 2.0: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
4
5
from pylint import interfaces
6
from pylint import checkers
7
from pylint.checkers import utils
8
9
10
class AuthUserChecker(checkers.BaseChecker):
11
    __implements__ = (interfaces.IAstroidChecker,)
12
13
    name = 'auth-user-checker'
14
15
    msgs = {'E4441': ("Hard-coded 'auth.User'",
16
                      'hard-coded-auth-user',
17
                      "Don't hard-code the auth.User model. "
18
                      "Use settings.AUTH_USER_MODEL instead!"),
19
            'E4442': ("User model imported from django.contrib.auth.models",
20
                      'imported-auth-user',
21
                      "Don't import django.contrib.auth.models.User model. "
22
                      "Use django.contrib.auth.get_user_model() instead!")}
23
24
    @utils.check_messages('hard-coded-auth-user')
25
    def visit_const(self, node):
26
        # for now we don't check if the parent is a ForeignKey field
27
        # because the user model should not be hard-coded anywhere
28
        if node.value == 'auth.User':
29
            self.add_message('hard-coded-auth-user', node=node)
30
31
    def visit_importfrom(self, node):
32
        if node.modname == 'django.contrib.auth.models':
33
            for imported_names in node.names:
34
                if imported_names[0] in ['*', 'User']:
35
                    self.add_message('imported-auth-user', node=node)
36
                    break
37