|
0
|
1 |
from django.core.management.base import copy_helper, CommandError, LabelCommand |
|
|
2 |
from django.utils.importlib import import_module |
|
|
3 |
import os |
|
|
4 |
import re |
|
|
5 |
from random import choice |
|
|
6 |
|
|
|
7 |
class Command(LabelCommand): |
|
|
8 |
help = "Creates a Django project directory structure for the given project name in the current directory." |
|
|
9 |
args = "[projectname]" |
|
|
10 |
label = 'project name' |
|
|
11 |
|
|
|
12 |
requires_model_validation = False |
|
|
13 |
# Can't import settings during this command, because they haven't |
|
|
14 |
# necessarily been created. |
|
|
15 |
can_import_settings = False |
|
|
16 |
|
|
|
17 |
def handle_label(self, project_name, **options): |
|
|
18 |
# Determine the project_name a bit naively -- by looking at the name of |
|
|
19 |
# the parent directory. |
|
|
20 |
directory = os.getcwd() |
|
|
21 |
|
|
|
22 |
# Check that the project_name cannot be imported. |
|
|
23 |
try: |
|
|
24 |
import_module(project_name) |
|
|
25 |
except ImportError: |
|
|
26 |
pass |
|
|
27 |
else: |
|
|
28 |
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) |
|
|
29 |
|
|
|
30 |
copy_helper(self.style, 'project', project_name, directory) |
|
|
31 |
|
|
|
32 |
# Create a random SECRET_KEY hash, and put it in the main settings. |
|
|
33 |
main_settings_file = os.path.join(directory, project_name, 'settings.py') |
|
|
34 |
settings_contents = open(main_settings_file, 'r').read() |
|
|
35 |
fp = open(main_settings_file, 'w') |
|
|
36 |
secret_key = ''.join([choice('abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)') for i in range(50)]) |
|
|
37 |
settings_contents = re.sub(r"(?<=SECRET_KEY = ')'", secret_key + "'", settings_contents) |
|
|
38 |
fp.write(settings_contents) |
|
|
39 |
fp.close() |