Skip to content

Commit

Permalink
Moved validation of project names to an earlier spot so no directory …
Browse files Browse the repository at this point in the history
…with invalid name is created.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17288 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
ramiro committed Dec 29, 2011
1 parent 588f5af commit c5fae63
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
22 changes: 11 additions & 11 deletions django/core/management/templates.py
Expand Up @@ -60,6 +60,17 @@ def handle(self, app_or_project, name, target=None, **options):
self.paths_to_remove = []
self.verbosity = int(options.get('verbosity'))

# 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))

# if some directory is given, make sure it's nicely expanded
if target is None:
target = os.getcwd()
Expand Down Expand Up @@ -88,17 +99,6 @@ def handle(self, app_or_project, name, target=None, **options):
base_directory: top_dir,
}))

# 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))

# Setup a stub settings environment for template rendering
from django.conf import settings
if not settings.configured:
Expand Down
14 changes: 14 additions & 0 deletions tests/regressiontests/admin_scripts/tests.py
Expand Up @@ -1386,6 +1386,20 @@ def test_simple_project(self):
self.assertNoOutput(out)
self.assertOutput(err, "File exists")

def test_invalid_project_name(self):
def cleanup(p):
if os.path.exists(p):
shutil.rmtree(p)

"Make sure the startproject management command validates a project name"
args = ['startproject', '7testproject']
testproject_dir = os.path.join(test_dir, '7testproject')

out, err = self.run_django_admin(args)
self.addCleanup(cleanup, testproject_dir)
self.assertOutput(err, "Error: '7testproject' is not a valid project name. Please make sure the name begins with a letter or underscore.")
self.assertFalse(os.path.exists(testproject_dir))

def test_simple_project_different_directory(self):
"Make sure the startproject management command creates a project in a specific directory"
args = ['startproject', 'testproject', 'othertestproject']
Expand Down

0 comments on commit c5fae63

Please sign in to comment.