app/django/core/management/commands/startproject.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sun, 02 Nov 2008 22:50:22 +0000
changeset 432 1851d67a1004
parent 323 ff1a9aa48cfd
permissions -rw-r--r--
Add a generic method for getting a Keyname This patch will make logic/key_name.py obsolete in the future when everything is compliant with base.logic. This also makes the naming of already compliant logic modules generic and also fixes the _name field to be consistent with the naming in key_name.py. Note: This does not use Group/Sponsor, Group/School, Group/Organisation or Group/Club. Patch by: "Lennard de Rijk" <rijk0214@gmail.com> Reviewed by: Sverre Rabbelier

from django.core.management.base import copy_helper, CommandError, LabelCommand
import os
import re
from random import choice

class Command(LabelCommand):
    help = "Creates a Django project directory structure for the given project name in the current directory."
    args = "[projectname]"
    label = 'project name'

    requires_model_validation = False
    # Can't import settings during this command, because they haven't
    # necessarily been created.
    can_import_settings = False

    def handle_label(self, project_name, **options):
        # Determine the project_name a bit naively -- by looking at the name of
        # the parent directory.
        directory = os.getcwd()

        # Check that the project_name cannot be imported.
        try:
            __import__(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)

        copy_helper(self.style, 'project', project_name, directory)

        # Create a random SECRET_KEY hash, and put it in the main settings.
        main_settings_file = os.path.join(directory, project_name, 'settings.py')
        settings_contents = open(main_settings_file, 'r').read()
        fp = open(main_settings_file, 'w')
        secret_key = ''.join([choice('abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)') for i in range(50)])
        settings_contents = re.sub(r"(?<=SECRET_KEY = ')'", secret_key + "'", settings_contents)
        fp.write(settings_contents)
        fp.close()