2014-01-20 10:45:21 +08:00
|
|
|
from itertools import chain
|
|
|
|
|
|
|
|
from django.utils.itercompat import is_iterable
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Tags:
|
2014-04-10 20:45:48 +08:00
|
|
|
"""
|
|
|
|
Built-in tags for internal checks.
|
|
|
|
"""
|
|
|
|
admin = 'admin'
|
2020-03-21 04:20:48 +08:00
|
|
|
async_support = 'async_support'
|
2015-09-02 20:20:46 +08:00
|
|
|
caches = 'caches'
|
2014-04-10 20:45:48 +08:00
|
|
|
compatibility = 'compatibility'
|
2016-03-19 19:15:09 +08:00
|
|
|
database = 'database'
|
2014-04-10 20:45:48 +08:00
|
|
|
models = 'models'
|
2014-09-13 02:50:36 +08:00
|
|
|
security = 'security'
|
2014-04-10 20:45:48 +08:00
|
|
|
signals = 'signals'
|
2015-06-04 23:55:58 +08:00
|
|
|
templates = 'templates'
|
2018-09-03 16:43:55 +08:00
|
|
|
translation = 'translation'
|
2015-09-17 06:07:39 +08:00
|
|
|
urls = 'urls'
|
2014-04-10 20:45:48 +08:00
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class CheckRegistry:
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
def __init__(self):
|
2017-08-24 17:45:04 +08:00
|
|
|
self.registered_checks = set()
|
|
|
|
self.deployment_checks = set()
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2014-11-05 14:01:49 +08:00
|
|
|
def register(self, check=None, *tags, **kwargs):
|
2014-01-20 10:45:21 +08:00
|
|
|
"""
|
2014-11-05 14:01:49 +08:00
|
|
|
Can be used as a function or a decorator. Register given function
|
|
|
|
`f` labeled with given `tags`. The function should receive **kwargs
|
|
|
|
and return list of Errors and Warnings.
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
Example::
|
|
|
|
|
|
|
|
registry = CheckRegistry()
|
|
|
|
@registry.register('mytag', 'anothertag')
|
|
|
|
def my_check(apps, **kwargs):
|
|
|
|
# ... perform checks and collect `errors` ...
|
|
|
|
return errors
|
2014-11-05 14:01:49 +08:00
|
|
|
# or
|
|
|
|
registry.register(my_check, 'mytag', 'anothertag')
|
2014-01-20 10:45:21 +08:00
|
|
|
"""
|
|
|
|
def inner(check):
|
|
|
|
check.tags = tags
|
2018-02-26 21:54:55 +08:00
|
|
|
checks = self.deployment_checks if kwargs.get('deploy') else self.registered_checks
|
2017-08-24 17:45:04 +08:00
|
|
|
checks.add(check)
|
2014-01-20 10:45:21 +08:00
|
|
|
return check
|
|
|
|
|
2014-11-05 14:01:49 +08:00
|
|
|
if callable(check):
|
|
|
|
return inner(check)
|
|
|
|
else:
|
|
|
|
if check:
|
2017-12-29 04:07:29 +08:00
|
|
|
tags += (check,)
|
2014-11-05 14:01:49 +08:00
|
|
|
return inner
|
2014-01-20 10:45:21 +08:00
|
|
|
|
2020-02-07 15:46:13 +08:00
|
|
|
def run_checks(self, app_configs=None, tags=None, include_deployment_checks=False, databases=None):
|
2015-09-01 04:14:35 +08:00
|
|
|
"""
|
|
|
|
Run all registered checks and return list of Errors and Warnings.
|
2014-01-20 10:45:21 +08:00
|
|
|
"""
|
|
|
|
errors = []
|
2014-09-13 02:50:36 +08:00
|
|
|
checks = self.get_checks(include_deployment_checks)
|
|
|
|
|
2014-01-20 10:45:21 +08:00
|
|
|
if tags is not None:
|
2017-08-22 03:34:20 +08:00
|
|
|
checks = [check for check in checks if not set(check.tags).isdisjoint(tags)]
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
for check in checks:
|
2020-02-07 15:46:13 +08:00
|
|
|
new_errors = check(app_configs=app_configs, databases=databases)
|
2014-01-20 10:45:21 +08:00
|
|
|
assert is_iterable(new_errors), (
|
|
|
|
"The function %r did not return a list. All functions registered "
|
|
|
|
"with the checks registry must return a list." % check)
|
|
|
|
errors.extend(new_errors)
|
|
|
|
return errors
|
|
|
|
|
2014-09-13 02:50:36 +08:00
|
|
|
def tag_exists(self, tag, include_deployment_checks=False):
|
|
|
|
return tag in self.tags_available(include_deployment_checks)
|
|
|
|
|
|
|
|
def tags_available(self, deployment_checks=False):
|
2017-04-26 12:04:10 +08:00
|
|
|
return set(chain.from_iterable(
|
|
|
|
check.tags for check in self.get_checks(deployment_checks)
|
|
|
|
))
|
2014-04-10 21:43:55 +08:00
|
|
|
|
2014-09-13 02:50:36 +08:00
|
|
|
def get_checks(self, include_deployment_checks=False):
|
|
|
|
checks = list(self.registered_checks)
|
|
|
|
if include_deployment_checks:
|
|
|
|
checks.extend(self.deployment_checks)
|
|
|
|
return checks
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
registry = CheckRegistry()
|
|
|
|
register = registry.register
|
|
|
|
run_checks = registry.run_checks
|
|
|
|
tag_exists = registry.tag_exists
|