|
| 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 |
0 commit comments