DRY'd startapp and startproject management commands.

This commit is contained in:
Adam Johnson 2017-05-30 08:55:38 +01:00 committed by Tim Graham
parent b3e55109bd
commit c61d1361d0
3 changed files with 31 additions and 38 deletions

View File

@ -1,6 +1,3 @@
from importlib import import_module
from django.core.management.base import CommandError
from django.core.management.templates import TemplateCommand from django.core.management.templates import TemplateCommand
@ -12,18 +9,6 @@ class Command(TemplateCommand):
missing_args_message = "You must provide an application name." missing_args_message = "You must provide an application name."
def handle(self, **options): def handle(self, **options):
app_name, target = options.pop('name'), options.pop('directory') app_name = options.pop('name')
self.validate_name(app_name, "app") target = options.pop('directory')
# 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
)
super().handle('app', app_name, target, **options) super().handle('app', app_name, target, **options)

View File

@ -1,6 +1,3 @@
from importlib import import_module
from django.core.management.base import CommandError
from django.core.management.templates import TemplateCommand from django.core.management.templates import TemplateCommand
from ..utils import get_random_secret_key from ..utils import get_random_secret_key
@ -14,19 +11,8 @@ class Command(TemplateCommand):
missing_args_message = "You must provide a project name." missing_args_message = "You must provide a project name."
def handle(self, **options): def handle(self, **options):
project_name, target = options.pop('name'), options.pop('directory') project_name = options.pop('name')
self.validate_name(project_name, "project") target = options.pop('directory')
# 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
)
# Create a random SECRET_KEY to put it in the main settings. # Create a random SECRET_KEY to put it in the main settings.
options['secret_key'] = get_random_secret_key() options['secret_key'] = get_random_secret_key()

View File

@ -7,6 +7,7 @@ import shutil
import stat import stat
import sys import sys
import tempfile import tempfile
from importlib import import_module
from os import path from os import path
from urllib.request import urlretrieve from urllib.request import urlretrieve
@ -210,14 +211,35 @@ class TemplateCommand(BaseCommand):
(self.app_or_project, template)) (self.app_or_project, template))
def validate_name(self, name, app_or_project): def validate_name(self, name, app_or_project):
a_or_an = 'an' if app_or_project == 'app' else 'a'
if name is None: if name is None:
raise CommandError("you must provide %s %s name" % ( raise CommandError('you must provide {an} {app} name'.format(
"an" if app_or_project == "app" else "a", app_or_project)) an=a_or_an,
# If it's not a valid directory name. app=app_or_project,
))
# Check it's a valid directory name.
if not name.isidentifier(): if not name.isidentifier():
raise CommandError( raise CommandError(
"%r is not a valid %s name. Please make sure the name is " "'{name}' is not a valid {app} name. Please make sure the "
"a valid identifier." % (name, app_or_project) "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): def download(self, url):