Ticket #19125: ticket19125.diff

File ticket19125.diff, 5.1 KB (added by Łukasz Rekucki, 12 years ago)
  • django/core/management/commands/startapp.py

    diff --git a/django/core/management/commands/startapp.py b/django/core/management/commands/startapp.py
    index 5581331..692ad09 100644
    a b 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

    diff --git a/django/core/management/commands/startproject.py b/django/core/management/commands/startproject.py
    index a1a9b33..b143e6c 100644
    a b 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

    diff --git a/django/core/management/templates.py b/django/core/management/templates.py
    index d34a0de..e75befc 100644
    a b class TemplateCommand(BaseCommand):  
    6464    # The supported URL schemes
    6565    url_schemes = ['http', 'https', 'ftp']
    6666
     67
    6768    def handle(self, app_or_project, name, target=None, **options):
    6869        self.app_or_project = app_or_project
    6970        self.paths_to_remove = []
    7071        self.verbosity = int(options.get('verbosity'))
    7172
    72         # If it's not a valid directory name.
    73         if not re.search(r'^[_a-zA-Z]\w*$', name):
    74             # Provide a smart error message, depending on the error.
    75             if not re.search(r'^[_a-zA-Z]', name):
    76                 message = ('make sure the name begins '
    77                            'with a letter or underscore')
    78             else:
    79                 message = 'use only numbers, letters and underscores'
    80             raise CommandError("%r is not a valid %s name. Please %s." %
    81                                (name, app_or_project, message))
     73        self.validate_name(name, app_or_project)
    8274
    8375        # if some directory is given, make sure it's nicely expanded
    8476        if target is None:
    class TemplateCommand(BaseCommand):  
    211203        raise CommandError("couldn't handle %s template %s." %
    212204                           (self.app_or_project, template))
    213205
     206    def validate_name(self, name, app_or_project):
     207        if name is None:
     208            raise CommandError("you must provide %s %s name" % ("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 '
     214                           'with a letter or underscore')
     215            else:
     216                message = 'use only numbers, letters and underscores'
     217            raise CommandError("%r is not a valid %s name. Please %s." %
     218                               (name, app_or_project, message))
     219
    214220    def download(self, url):
    215221        """
    216222        Downloads the given URL and returns the file name.
  • tests/regressiontests/admin_scripts/tests.py

    diff --git a/tests/regressiontests/admin_scripts/tests.py b/tests/regressiontests/admin_scripts/tests.py
    index 6f524be..a9a9728 100644
    a b class StartProject(LiveServerTestCase, AdminScriptTestCase):  
    14281428
    14291429        args = ['startproject', '7testproject']
    14301430        testproject_dir = os.path.join(test_dir, '7testproject')
     1431        self.addCleanup(cleanup, testproject_dir)
    14311432
    14321433        out, err = self.run_django_admin(args)
    1433         self.addCleanup(cleanup, testproject_dir)
    14341434        self.assertOutput(err, "Error: '7testproject' is not a valid project name. Please make sure the name begins with a letter or underscore.")
    14351435        self.assertFalse(os.path.exists(testproject_dir))
    14361436
     1437    def test_path_as_project_name(self):
     1438        "Make sure the startproject management command validates a project name"
     1439
     1440        def cleanup(p):
     1441            if os.path.exists(p):
     1442                shutil.rmtree(p)
     1443
     1444        args = ['startproject', '../testproject']
     1445        testproject_dir = os.path.join(test_dir, '../testproject')
     1446        self.addCleanup(cleanup, testproject_dir)
     1447
     1448        out, err = self.run_django_admin(args)
     1449        self.assertOutput(err, "Error: '../testproject' is not a valid project name. Please make sure the name begins with a letter or underscore.")
     1450        self.assertFalse(os.path.exists(testproject_dir))
     1451
    14371452    def test_simple_project_different_directory(self):
    14381453        "Make sure the startproject management command creates a project in a specific directory"
    14391454        args = ['startproject', 'testproject', 'othertestproject']
Back to Top