From c61d1361d027a729d07d277879950ff133c19f4c Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Tue, 30 May 2017 08:55:38 +0100 Subject: [PATCH] DRY'd startapp and startproject management commands. --- django/core/management/commands/startapp.py | 19 ++--------- .../core/management/commands/startproject.py | 18 ++--------- django/core/management/templates.py | 32 ++++++++++++++++--- 3 files changed, 31 insertions(+), 38 deletions(-) diff --git a/django/core/management/commands/startapp.py b/django/core/management/commands/startapp.py index 63ecba4372..bba9f3dee0 100644 --- a/django/core/management/commands/startapp.py +++ b/django/core/management/commands/startapp.py @@ -1,6 +1,3 @@ -from importlib import import_module - -from django.core.management.base import CommandError from django.core.management.templates import TemplateCommand @@ -12,18 +9,6 @@ class Command(TemplateCommand): missing_args_message = "You must provide an application name." def handle(self, **options): - app_name, target = options.pop('name'), options.pop('directory') - self.validate_name(app_name, "app") - - # Check that the app_name cannot be imported. - try: - import_module(app_name) - except ImportError: - pass - else: - 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 - ) - + app_name = options.pop('name') + target = options.pop('directory') super().handle('app', app_name, target, **options) diff --git a/django/core/management/commands/startproject.py b/django/core/management/commands/startproject.py index bc64faa680..7e09a25e91 100644 --- a/django/core/management/commands/startproject.py +++ b/django/core/management/commands/startproject.py @@ -1,6 +1,3 @@ -from importlib import import_module - -from django.core.management.base import CommandError from django.core.management.templates import TemplateCommand from ..utils import get_random_secret_key @@ -14,19 +11,8 @@ class Command(TemplateCommand): missing_args_message = "You must provide a project name." def handle(self, **options): - project_name, target = options.pop('name'), options.pop('directory') - self.validate_name(project_name, "project") - - # Check that the project_name cannot be imported. - try: - import_module(project_name) - except ImportError: - pass - else: - 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 - ) + project_name = options.pop('name') + target = options.pop('directory') # Create a random SECRET_KEY to put it in the main settings. options['secret_key'] = get_random_secret_key() diff --git a/django/core/management/templates.py b/django/core/management/templates.py index 3aecd10a42..eb4baafd62 100644 --- a/django/core/management/templates.py +++ b/django/core/management/templates.py @@ -7,6 +7,7 @@ import shutil import stat import sys import tempfile +from importlib import import_module from os import path from urllib.request import urlretrieve @@ -210,14 +211,35 @@ class TemplateCommand(BaseCommand): (self.app_or_project, template)) def validate_name(self, name, app_or_project): + a_or_an = 'an' if app_or_project == 'app' else 'a' 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. + raise CommandError('you must provide {an} {app} name'.format( + an=a_or_an, + app=app_or_project, + )) + # Check it's a valid directory name. if not name.isidentifier(): raise CommandError( - "%r is not a valid %s name. Please make sure the name is " - "a valid identifier." % (name, app_or_project) + "'{name}' is not a valid {app} name. Please make sure the " + "name is a valid identifier.".format( + name=name, + app=app_or_project, + ) + ) + # Check it cannot be imported. + try: + import_module(name) + except ImportError: + pass + else: + raise CommandError( + "'{name}' conflicts with the name of an existing Python " + "module and cannot be used as {an} {app} name. Please try " + "another name.".format( + name=name, + an=a_or_an, + app=app_or_project, + ) ) def download(self, url):