~canonical-django/canonical-django/project-template

« back to all changes in this revision

Viewing changes to trunk/python-packages/django/core/management/commands/startapp.py

  • Committer: Matthew Nuzum
  • Date: 2008-11-13 05:46:03 UTC
  • Revision ID: matthew.nuzum@canonical.com-20081113054603-v0kvr6z6xyexvqt3
adding to version control

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
 
 
3
from django.core.management.base import copy_helper, CommandError, LabelCommand
 
4
 
 
5
class Command(LabelCommand):
 
6
    help = "Creates a Django app directory structure for the given app name in the current directory."
 
7
    args = "[appname]"
 
8
    label = 'application name'
 
9
 
 
10
    requires_model_validation = False
 
11
    # Can't import settings during this command, because they haven't
 
12
    # necessarily been created.
 
13
    can_import_settings = False
 
14
 
 
15
    def handle_label(self, app_name, directory=None, **options):
 
16
        if directory is None:
 
17
            directory = os.getcwd()
 
18
 
 
19
        # Determine the project_name by using the basename of directory,
 
20
        # which should be the full path of the project directory (or the
 
21
        # current directory if no directory was passed).
 
22
        project_name = os.path.basename(directory)
 
23
        if app_name == project_name:
 
24
            raise CommandError("You cannot create an app with the same name"
 
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
 
 
35
        copy_helper(self.style, 'app', app_name, directory, project_name)
 
36
 
 
37
class ProjectCommand(Command):
 
38
    help = ("Creates a Django app directory structure for the given app name"
 
39
            " in this project's directory.")
 
40
 
 
41
    def __init__(self, project_directory):
 
42
        super(ProjectCommand, self).__init__()
 
43
        self.project_directory = project_directory
 
44
 
 
45
    def handle_label(self, app_name, **options):
 
46
        super(ProjectCommand, self).handle_label(app_name, self.project_directory, **options)