2007-08-16 22:34:01 +08:00
from django . core . management . base import copy_helper , CommandError , LabelCommand
2007-08-16 14:06:55 +08:00
import os
import re
from random import choice
2007-08-16 22:34:01 +08:00
class Command ( LabelCommand ) :
2007-08-16 14:06:55 +08:00
help = " Creates a Django project directory structure for the given project name in the current directory. "
args = " [projectname] "
2007-08-16 22:34:01 +08:00
label = ' project name '
2007-08-16 14:06:55 +08:00
requires_model_validation = False
# Can't import settings during this command, because they haven't
# necessarily been created.
can_import_settings = False
2007-08-16 22:34:01 +08:00
def handle_label ( self , project_name , * * options ) :
2007-08-16 14:06:55 +08:00
# Determine the project_name a bit naively -- by looking at the name of
# the parent directory.
directory = os . getcwd ( )
2008-06-16 11:56:48 +08:00
# Check that the project_name cannot be imported.
2008-03-19 23:10:31 +08:00
try :
2008-06-16 11:56:48 +08:00
__import__ ( project_name )
2008-03-19 23:10:31 +08:00
except ImportError :
2008-06-16 11:56:48 +08:00
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 )
2007-08-16 14:06:55 +08:00
2007-08-17 03:14:09 +08:00
copy_helper ( self . style , ' project ' , project_name , directory )
2007-08-16 14:06:55 +08:00
# 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 ( )