Skip to content

Commit d03555b

Browse files
committed
New checker for hard-coded auth.User. Fix #244
Original implementation by myself from https://github.com/kiwitcms/Kiwi/
1 parent 502125c commit d03555b

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

pylint_django/checkers/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pylint_django.checkers.models import ModelChecker
44
from pylint_django.checkers.json_response import JsonResponseChecker
55
from pylint_django.checkers.forms import FormChecker
6+
from pylint_django.checkers.auth_user import AuthUserChecker
67

78

89
def register_checkers(linter):
@@ -11,3 +12,4 @@ def register_checkers(linter):
1112
linter.register_checker(DjangoInstalledChecker(linter))
1213
linter.register_checker(JsonResponseChecker(linter))
1314
linter.register_checker(FormChecker(linter))
15+
linter.register_checker(AuthUserChecker(linter))

pylint_django/checkers/auth_user.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from pylint import interfaces
2+
from pylint import checkers
3+
from pylint.checkers import utils
4+
5+
from pylint_django.__pkginfo__ import BASE_ID
6+
7+
8+
class AuthUserChecker(checkers.BaseChecker):
9+
__implements__ = (interfaces.IAstroidChecker,)
10+
11+
name = 'auth-user-checker'
12+
13+
msgs = {'E%d41' % BASE_ID: ("Hard-coded 'auth.User'",
14+
'hard-coded-auth-user',
15+
"Don't hard-code the auth.User model. "
16+
"Use settings.AUTH_USER_MODEL instead!"),
17+
'E%d42' % BASE_ID: ("User model imported from django.contrib.auth.models",
18+
'imported-auth-user',
19+
"Don't import django.contrib.auth.models.User model. "
20+
"Use django.contrib.auth.get_user_model() instead!")}
21+
22+
@utils.check_messages('hard-coded-auth-user')
23+
def visit_const(self, node):
24+
# for now we don't check if the parent is a ForeignKey field
25+
# because the user model should not be hard-coded anywhere
26+
if node.value == 'auth.User':
27+
self.add_message('hard-coded-auth-user', node=node)
28+
29+
def visit_importfrom(self, node):
30+
if node.modname == 'django.contrib.auth.models':
31+
for imported_names in node.names:
32+
if imported_names[0] in ['*', 'User']:
33+
self.add_message('imported-auth-user', node=node)
34+
break
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# pylint: disable=missing-docstring, wildcard-import, unused-wildcard-import
2+
# flake8: noqa=F401, F403
3+
4+
from django.db import models
5+
from django.contrib.auth.models import * # [imported-auth-user]
6+
from django.contrib.auth.models import User # [imported-auth-user]
7+
8+
9+
class PullRequest(models.Model):
10+
author = models.ForeignKey("auth.User", models.CASCADE) # [hard-coded-auth-user]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
imported-auth-user:5::User model imported from django.contrib.auth.models
2+
imported-auth-user:6::User model imported from django.contrib.auth.models
3+
hard-coded-auth-user:10:PullRequest:Hard-coded 'auth.User'

pylint_django/tests/input/func_noerror_string_foreignkey.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Checks that PyLint correctly handles string foreign keys
33
https://github.com/PyCQA/pylint-django/issues/243
44
"""
5-
# pylint: disable=missing-docstring
5+
# pylint: disable=missing-docstring, hard-coded-auth-user
66
from django.db import models
77

88

0 commit comments

Comments
 (0)