You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

check.py 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from django.apps import apps
  2. from django.core import checks
  3. from django.core.checks.registry import registry
  4. from django.core.management.base import BaseCommand, CommandError
  5. class Command(BaseCommand):
  6. help = "Checks the entire Django project for potential problems."
  7. requires_system_checks = False
  8. def add_arguments(self, parser):
  9. parser.add_argument('args', metavar='app_label', nargs='*')
  10. parser.add_argument(
  11. '--tag', '-t', action='append', dest='tags',
  12. help='Run only checks labeled with given tag.',
  13. )
  14. parser.add_argument(
  15. '--list-tags', action='store_true',
  16. help='List available tags.',
  17. )
  18. parser.add_argument(
  19. '--deploy', action='store_true',
  20. help='Check deployment settings.',
  21. )
  22. parser.add_argument(
  23. '--fail-level',
  24. default='ERROR',
  25. choices=['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'],
  26. help=(
  27. 'Message level that will cause the command to exit with a '
  28. 'non-zero status. Default is ERROR.'
  29. ),
  30. )
  31. def handle(self, *app_labels, **options):
  32. include_deployment_checks = options['deploy']
  33. if options['list_tags']:
  34. self.stdout.write('\n'.join(sorted(registry.tags_available(include_deployment_checks))))
  35. return
  36. if app_labels:
  37. app_configs = [apps.get_app_config(app_label) for app_label in app_labels]
  38. else:
  39. app_configs = None
  40. tags = options['tags']
  41. if tags:
  42. try:
  43. invalid_tag = next(
  44. tag for tag in tags if not checks.tag_exists(tag, include_deployment_checks)
  45. )
  46. except StopIteration:
  47. # no invalid tags
  48. pass
  49. else:
  50. raise CommandError('There is no system check with the "%s" tag.' % invalid_tag)
  51. self.check(
  52. app_configs=app_configs,
  53. tags=tags,
  54. display_num_errors=True,
  55. include_deployment_checks=include_deployment_checks,
  56. fail_level=getattr(checks, options['fail_level']),
  57. )