mirror of https://github.com/django/django.git
Fixed #19125 -- The startproject command should validate the name earlier
Thanks Łukasz Rekucki for the report and the patch.
This commit is contained in:
parent
93e79b45bc
commit
9893fa12b7
|
@ -9,8 +9,7 @@ class Command(TemplateCommand):
|
||||||
"directory.")
|
"directory.")
|
||||||
|
|
||||||
def handle(self, app_name=None, target=None, **options):
|
def handle(self, app_name=None, target=None, **options):
|
||||||
if app_name is None:
|
self.validate_name(app_name, "app")
|
||||||
raise CommandError("you must provide an app name")
|
|
||||||
|
|
||||||
# Check that the app_name cannot be imported.
|
# Check that the app_name cannot be imported.
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -10,8 +10,7 @@ class Command(TemplateCommand):
|
||||||
"given directory.")
|
"given directory.")
|
||||||
|
|
||||||
def handle(self, project_name=None, target=None, *args, **options):
|
def handle(self, project_name=None, target=None, *args, **options):
|
||||||
if project_name is None:
|
self.validate_name(project_name, "project")
|
||||||
raise CommandError("you must provide a project name")
|
|
||||||
|
|
||||||
# Check that the project_name cannot be imported.
|
# Check that the project_name cannot be imported.
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -67,16 +67,7 @@ class TemplateCommand(BaseCommand):
|
||||||
self.paths_to_remove = []
|
self.paths_to_remove = []
|
||||||
self.verbosity = int(options.get('verbosity'))
|
self.verbosity = int(options.get('verbosity'))
|
||||||
|
|
||||||
# If it's not a valid directory name.
|
self.validate_name(name, app_or_project)
|
||||||
if not re.search(r'^[_a-zA-Z]\w*$', name):
|
|
||||||
# Provide a smart error message, depending on the error.
|
|
||||||
if not re.search(r'^[_a-zA-Z]', name):
|
|
||||||
message = ('make sure the name begins '
|
|
||||||
'with a letter or underscore')
|
|
||||||
else:
|
|
||||||
message = 'use only numbers, letters and underscores'
|
|
||||||
raise CommandError("%r is not a valid %s name. Please %s." %
|
|
||||||
(name, app_or_project, message))
|
|
||||||
|
|
||||||
# if some directory is given, make sure it's nicely expanded
|
# if some directory is given, make sure it's nicely expanded
|
||||||
if target is None:
|
if target is None:
|
||||||
|
@ -211,6 +202,20 @@ class TemplateCommand(BaseCommand):
|
||||||
raise CommandError("couldn't handle %s template %s." %
|
raise CommandError("couldn't handle %s template %s." %
|
||||||
(self.app_or_project, template))
|
(self.app_or_project, template))
|
||||||
|
|
||||||
|
def validate_name(self, name, app_or_project):
|
||||||
|
if name is None:
|
||||||
|
raise CommandError("you must provide %s %s name" % (
|
||||||
|
"an" if app_or_project == "app" else "a", app_or_project))
|
||||||
|
# If it's not a valid directory name.
|
||||||
|
if not re.search(r'^[_a-zA-Z]\w*$', name):
|
||||||
|
# Provide a smart error message, depending on the error.
|
||||||
|
if not re.search(r'^[_a-zA-Z]', name):
|
||||||
|
message = 'make sure the name begins with a letter or underscore'
|
||||||
|
else:
|
||||||
|
message = 'use only numbers, letters and underscores'
|
||||||
|
raise CommandError("%r is not a valid %s name. Please %s." %
|
||||||
|
(name, app_or_project, message))
|
||||||
|
|
||||||
def download(self, url):
|
def download(self, url):
|
||||||
"""
|
"""
|
||||||
Downloads the given URL and returns the file name.
|
Downloads the given URL and returns the file name.
|
||||||
|
|
|
@ -1430,12 +1430,14 @@ class StartProject(LiveServerTestCase, AdminScriptTestCase):
|
||||||
|
|
||||||
def test_invalid_project_name(self):
|
def test_invalid_project_name(self):
|
||||||
"Make sure the startproject management command validates a project name"
|
"Make sure the startproject management command validates a project name"
|
||||||
args = ['startproject', '7testproject']
|
for bad_name in ('7testproject', '../testproject'):
|
||||||
testproject_dir = os.path.join(test_dir, '7testproject')
|
args = ['startproject', bad_name]
|
||||||
|
testproject_dir = os.path.join(test_dir, bad_name)
|
||||||
self.addCleanup(shutil.rmtree, testproject_dir, True)
|
self.addCleanup(shutil.rmtree, testproject_dir, True)
|
||||||
|
|
||||||
out, err = self.run_django_admin(args)
|
out, err = self.run_django_admin(args)
|
||||||
self.assertOutput(err, "Error: '7testproject' is not a valid project name. Please make sure the name begins with a letter or underscore.")
|
self.assertOutput(err, "Error: '%s' is not a valid project name. "
|
||||||
|
"Please make sure the name begins with a letter or underscore." % bad_name)
|
||||||
self.assertFalse(os.path.exists(testproject_dir))
|
self.assertFalse(os.path.exists(testproject_dir))
|
||||||
|
|
||||||
def test_simple_project_different_directory(self):
|
def test_simple_project_different_directory(self):
|
||||||
|
|
Loading…
Reference in New Issue