web/lib/django/core/management/commands/startapp.py
changeset 38 77b6da96e6f1
parent 0 0d40e90630ef
equal deleted inserted replaced
37:8d941af65caf 38:77b6da96e6f1
       
     1 import os
       
     2 
       
     3 from django.core.management.base import copy_helper, CommandError, LabelCommand
       
     4 from django.utils.importlib import import_module
       
     5 
       
     6 class Command(LabelCommand):
       
     7     help = "Creates a Django app directory structure for the given app name in the current directory."
       
     8     args = "[appname]"
       
     9     label = 'application name'
       
    10 
       
    11     requires_model_validation = False
       
    12     # Can't import settings during this command, because they haven't
       
    13     # necessarily been created.
       
    14     can_import_settings = False
       
    15 
       
    16     def handle_label(self, app_name, directory=None, **options):
       
    17         if directory is None:
       
    18             directory = os.getcwd()
       
    19 
       
    20         # Determine the project_name by using the basename of directory,
       
    21         # which should be the full path of the project directory (or the
       
    22         # current directory if no directory was passed).
       
    23         project_name = os.path.basename(directory)
       
    24         if app_name == project_name:
       
    25             raise CommandError("You cannot create an app with the same name"
       
    26                                " (%r) as your project." % app_name)
       
    27 
       
    28         # Check that the app_name cannot be imported.
       
    29         try:
       
    30             import_module(app_name)
       
    31         except ImportError:
       
    32             pass
       
    33         else:
       
    34             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)
       
    35 
       
    36         copy_helper(self.style, 'app', app_name, directory, project_name)
       
    37 
       
    38 class ProjectCommand(Command):
       
    39     help = ("Creates a Django app directory structure for the given app name"
       
    40             " in this project's directory.")
       
    41 
       
    42     def __init__(self, project_directory):
       
    43         super(ProjectCommand, self).__init__()
       
    44         self.project_directory = project_directory
       
    45 
       
    46     def handle_label(self, app_name, **options):
       
    47         super(ProjectCommand, self).handle_label(app_name, self.project_directory, **options)