Skip to content

Commit 9893fa1

Browse files
committed
Fixed django#19125 -- The startproject command should validate the name earlier
Thanks Łukasz Rekucki for the report and the patch.
1 parent 93e79b4 commit 9893fa1

File tree

4 files changed

+26
-21
lines changed

4 files changed

+26
-21
lines changed

django/core/management/commands/startapp.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ class Command(TemplateCommand):
99
"directory.")
1010

1111
def handle(self, app_name=None, target=None, **options):
12-
if app_name is None:
13-
raise CommandError("you must provide an app name")
12+
self.validate_name(app_name, "app")
1413

1514
# Check that the app_name cannot be imported.
1615
try:

django/core/management/commands/startproject.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ class Command(TemplateCommand):
1010
"given directory.")
1111

1212
def handle(self, project_name=None, target=None, *args, **options):
13-
if project_name is None:
14-
raise CommandError("you must provide a project name")
13+
self.validate_name(project_name, "project")
1514

1615
# Check that the project_name cannot be imported.
1716
try:

django/core/management/templates.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,7 @@ def handle(self, app_or_project, name, target=None, **options):
6767
self.paths_to_remove = []
6868
self.verbosity = int(options.get('verbosity'))
6969

70-
# If it's not a valid directory name.
71-
if not re.search(r'^[_a-zA-Z]\w*$', name):
72-
# Provide a smart error message, depending on the error.
73-
if not re.search(r'^[_a-zA-Z]', name):
74-
message = ('make sure the name begins '
75-
'with a letter or underscore')
76-
else:
77-
message = 'use only numbers, letters and underscores'
78-
raise CommandError("%r is not a valid %s name. Please %s." %
79-
(name, app_or_project, message))
70+
self.validate_name(name, app_or_project)
8071

8172
# if some directory is given, make sure it's nicely expanded
8273
if target is None:
@@ -211,6 +202,20 @@ def handle_template(self, template, subdir):
211202
raise CommandError("couldn't handle %s template %s." %
212203
(self.app_or_project, template))
213204

205+
def validate_name(self, name, app_or_project):
206+
if name is None:
207+
raise CommandError("you must provide %s %s name" % (
208+
"an" if app_or_project == "app" else "a", app_or_project))
209+
# If it's not a valid directory name.
210+
if not re.search(r'^[_a-zA-Z]\w*$', name):
211+
# Provide a smart error message, depending on the error.
212+
if not re.search(r'^[_a-zA-Z]', name):
213+
message = 'make sure the name begins with a letter or underscore'
214+
else:
215+
message = 'use only numbers, letters and underscores'
216+
raise CommandError("%r is not a valid %s name. Please %s." %
217+
(name, app_or_project, message))
218+
214219
def download(self, url):
215220
"""
216221
Downloads the given URL and returns the file name.

tests/regressiontests/admin_scripts/tests.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,13 +1430,15 @@ def test_simple_project(self):
14301430

14311431
def test_invalid_project_name(self):
14321432
"Make sure the startproject management command validates a project name"
1433-
args = ['startproject', '7testproject']
1434-
testproject_dir = os.path.join(test_dir, '7testproject')
1435-
self.addCleanup(shutil.rmtree, testproject_dir, True)
1436-
1437-
out, err = self.run_django_admin(args)
1438-
self.assertOutput(err, "Error: '7testproject' is not a valid project name. Please make sure the name begins with a letter or underscore.")
1439-
self.assertFalse(os.path.exists(testproject_dir))
1433+
for bad_name in ('7testproject', '../testproject'):
1434+
args = ['startproject', bad_name]
1435+
testproject_dir = os.path.join(test_dir, bad_name)
1436+
self.addCleanup(shutil.rmtree, testproject_dir, True)
1437+
1438+
out, err = self.run_django_admin(args)
1439+
self.assertOutput(err, "Error: '%s' is not a valid project name. "
1440+
"Please make sure the name begins with a letter or underscore." % bad_name)
1441+
self.assertFalse(os.path.exists(testproject_dir))
14401442

14411443
def test_simple_project_different_directory(self):
14421444
"Make sure the startproject management command creates a project in a specific directory"

0 commit comments

Comments
 (0)