Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #6654 -- Slightly refactored the way 'startproject' and 'starta…

…pp' check for existing Python modules. Thanks, i_i

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7652 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a0a06d1a89465e7800686eec905e4c6f16911684 1 parent cc64abf
Adrian Holovaty authored
13  django/core/management/commands/startapp.py
@@ -3,8 +3,7 @@
3 3
 from django.core.management.base import copy_helper, CommandError, LabelCommand
4 4
 
5 5
 class Command(LabelCommand):
6  
-    help = ("Creates a Django app directory structure for the given app name"
7  
-            " in the current directory.")
  6
+    help = "Creates a Django app directory structure for the given app name in the current directory."
8 7
     args = "[appname]"
9 8
     label = 'application name'
10 9
 
@@ -16,6 +15,7 @@ class Command(LabelCommand):
16 15
     def handle_label(self, app_name, directory=None, **options):
17 16
         if directory is None:
18 17
             directory = os.getcwd()
  18
+
19 19
         # Determine the project_name by using the basename of directory,
20 20
         # which should be the full path of the project directory (or the
21 21
         # current directory if no directory was passed).
@@ -23,6 +23,15 @@ def handle_label(self, app_name, directory=None, **options):
23 23
         if app_name == project_name:
24 24
             raise CommandError("You cannot create an app with the same name"
25 25
                                " (%r) as your project." % app_name)
  26
+
  27
+        # Check that the app_name cannot be imported.
  28
+        try:
  29
+            __import__(app_name)
  30
+        except ImportError:
  31
+            pass
  32
+        else:
  33
+            raise CommandError("%r conflicts with the name of an existing Python module and cannot be used as an app name. Please try another name." % app_name)
  34
+
26 35
         copy_helper(self.style, 'app', app_name, directory, project_name)
27 36
 
28 37
 class ProjectCommand(Command):
12  django/core/management/commands/startproject.py
@@ -3,8 +3,6 @@
3 3
 import re
4 4
 from random import choice
5 5
 
6  
-INVALID_PROJECT_NAMES = ('django', 'site', 'test')
7  
-
8 6
 class Command(LabelCommand):
9 7
     help = "Creates a Django project directory structure for the given project name in the current directory."
10 8
     args = "[projectname]"
@@ -20,13 +18,13 @@ def handle_label(self, project_name, **options):
20 18
         # the parent directory.
21 19
         directory = os.getcwd()
22 20
 
  21
+        # Check that the project_name cannot be imported.
23 22
         try:
24  
-            proj_name = __import__(project_name)
25  
-            if proj_name:
26  
-                raise CommandError("%r conflicts with the name of an existing Python module and cannot be used as a project name. Please try another name." % project_name)
  23
+            __import__(project_name)
27 24
         except ImportError:
28  
-            if project_name in INVALID_PROJECT_NAMES:
29  
-                raise CommandError("%r contains an invalid project name. Please try another name." % project_name)
  25
+            pass
  26
+        else:
  27
+            raise CommandError("%r conflicts with the name of an existing Python module and cannot be used as a project name. Please try another name." % project_name)
30 28
 
31 29
         copy_helper(self.style, 'project', project_name, directory)
32 30
 

0 notes on commit a0a06d1

Please sign in to comment.
Something went wrong with that request. Please try again.