Debian Codegraph @

django/core/management/commands/check.py

raw
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 from django.db import connections
6
7
8 class Command(BaseCommand):
9 help = "Checks the entire Django project for potential problems."
10
11 requires_system_checks = []
12
13 def add_arguments(self, parser):
14 parser.add_argument("args", metavar="app_label", nargs="*")
15 parser.add_argument(
16 "--tag",
17 "-t",
18 action="append",
19 dest="tags",
20 help="Run only checks labeled with given tag.",
21 )
22 parser.add_argument(
23 "--list-tags",
24 action="store_true",
25 help=(
26 "List available tags. Specify --deploy to include available deployment "
27 "tags."
28 ),
29 )
30 parser.add_argument(
31 "--deploy",
32 action="store_true",
33 help="Check deployment settings.",
34 )
35 parser.add_argument(
36 "--fail-level",
37 default="ERROR",
38 choices=["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG"],
39 help=(
40 "Message level that will cause the command to exit with a "
41 "non-zero status. Default is ERROR."
42 ),
43 )
44 parser.add_argument(
45 "--database",
46 action="append",
47 choices=tuple(connections),
48 dest="databases",
49 help="Run database related checks against these aliases.",
50 )
51
52 def handle(self, *app_labels, **options):
53 include_deployment_checks = options["deploy"]
54 if options["list_tags"]:
55 self.stdout.write(
56 "\n".join(sorted(registry.tags_available(include_deployment_checks)))
57 )
58 return
59
60 if app_labels:
61 app_configs = [apps.get_app_config(app_label) for app_label in app_labels]
62 else:
63 app_configs = None
64
65 tags = options["tags"]
66 if tags:
67 try:
68 invalid_tag = next(
69 tag
70 for tag in tags
71 if not checks.tag_exists(tag, include_deployment_checks)
72 )
73 except StopIteration:
74 # no invalid tags
75 pass
76 else:
77 raise CommandError(
78 'There is no system check with the "%s" tag.' % invalid_tag
79 )
80
81 self.check(
82 app_configs=app_configs,
83 tags=tags,
84 display_num_errors=True,
85 include_deployment_checks=include_deployment_checks,
86 fail_level=getattr(checks, options["fail_level"]),
87 databases=options["databases"],
88 )