Debian Codegraph @

django/core/management/commands/testserver.py

raw
1 from django.core.management import call_command
2 from django.core.management.base import BaseCommand
3 from django.db import connection
4
5
6 class Command(BaseCommand):
7 help = "Runs a development server with data from the given fixture(s)."
8
9 requires_system_checks = []
10
11 def add_arguments(self, parser):
12 parser.add_argument(
13 "args",
14 metavar="fixture",
15 nargs="*",
16 help="Path(s) to fixtures to load before running the server.",
17 )
18 parser.add_argument(
19 "--noinput",
20 "--no-input",
21 action="store_false",
22 dest="interactive",
23 help="Tells Django to NOT prompt the user for input of any kind.",
24 )
25 parser.add_argument(
26 "--addrport",
27 default="",
28 help="Port number or ipaddr:port to run the server on.",
29 )
30 parser.add_argument(
31 "--ipv6",
32 "-6",
33 action="store_true",
34 dest="use_ipv6",
35 help="Tells Django to use an IPv6 address.",
36 )
37
38 def handle(self, *fixture_labels, **options):
39 verbosity = options["verbosity"]
40 interactive = options["interactive"]
41
42 # Create a test database.
43 db_name = connection.creation.create_test_db(
44 verbosity=verbosity, autoclobber=not interactive, serialize=False
45 )
46
47 # Import the fixture data into the test database.
48 call_command("loaddata", *fixture_labels, verbosity=verbosity)
49
50 # Run the development server. Turn off auto-reloading because it causes
51 # a strange error -- it causes this handle() method to be called
52 # multiple times.
53 shutdown_message = (
54 "\nServer stopped.\nNote that the test database, %r, has not been "
55 "deleted. You can explore it on your own." % db_name
56 )
57 use_threading = connection.features.test_db_allows_multiple_connections
58 call_command(
59 "runserver",
60 addrport=options["addrport"],
61 shutdown_message=shutdown_message,
62 use_reloader=False,
63 use_ipv6=options["use_ipv6"],
64 use_threading=use_threading,
65 )