| 1 | import django |
| 2 | from django.core.management.base import copy_helper, CommandError, LabelCommand |
| 3 | from django.utils.importlib import import_module |
| 4 | import os |
| 5 | import re |
| 6 | from random import choice |
| 7 | |
| 8 | class Command(LabelCommand): |
| 9 | help = "Generates a basic Django settings file." |
| 10 | args = "/path/to/settings.py" |
| 11 | label = 'path' |
| 12 | |
| 13 | requires_model_validation = False |
| 14 | # Can't import settings during this command, because they haven't |
| 15 | # necessarily been created. |
| 16 | can_import_settings = False |
| 17 | |
| 18 | def handle_label(self, path, **options): |
| 19 | # Create a random SECRET_KEY hash, and put it in the main settings. |
| 20 | main_settings_file = os.path.join(django.__path__[0], 'conf', 'project_template', 'settings.py') |
| 21 | settings_contents = open(main_settings_file, 'r').read() |
| 22 | fp = open(path, 'w') |
| 23 | secret_key = ''.join([choice('abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)') for i in range(50)]) |
| 24 | settings_contents = re.sub(r"(?<=SECRET_KEY = ')'", secret_key + "'", settings_contents) |
| 25 | fp.write(settings_contents) |
| 26 | fp.close() |