Development of an internal social media platform with personalised dashboards for students
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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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', dest='list_tags',
  16. help='List available tags.',
  17. )
  18. parser.add_argument(
  19. '--deploy', action='store_true', dest='deploy',
  20. help='Check deployment settings.',
  21. )
  22. parser.add_argument(
  23. '--fail-level',
  24. default='ERROR',
  25. choices=['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'],
  26. dest='fail_level',
  27. help=(
  28. 'Message level that will cause the command to exit with a '
  29. 'non-zero status. Default is ERROR.'
  30. ),
  31. )
  32. def handle(self, *app_labels, **options):
  33. include_deployment_checks = options['deploy']
  34. if options['list_tags']:
  35. self.stdout.write('\n'.join(sorted(registry.tags_available(include_deployment_checks))))
  36. return
  37. if app_labels:
  38. app_configs = [apps.get_app_config(app_label) for app_label in app_labels]
  39. else:
  40. app_configs = None
  41. tags = options['tags']
  42. if tags:
  43. try:
  44. invalid_tag = next(
  45. tag for tag in tags if not checks.tag_exists(tag, include_deployment_checks)
  46. )
  47. except StopIteration:
  48. # no invalid tags
  49. pass
  50. else:
  51. raise CommandError('There is no system check with the "%s" tag.' % invalid_tag)
  52. self.check(
  53. app_configs=app_configs,
  54. tags=tags,
  55. display_num_errors=True,
  56. include_deployment_checks=include_deployment_checks,
  57. fail_level=getattr(checks, options['fail_level']),
  58. )