From 559fd536e65b0a207c38afe75fcecc82b7b494c7 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Mon, 7 Sep 2009 15:22:38 +0200 Subject: [PATCH] use local_settings, create random secret on first launch --- .bzrignore | 2 ++ settings.py | 40 +++++++++++++++++++++++++--------------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/.bzrignore b/.bzrignore index 65e29cc..342847f 100644 --- a/.bzrignore +++ b/.bzrignore @@ -2,3 +2,5 @@ host_settings/* media/* dev.sqlite *.pyc +local_settings.py +secret.txt diff --git a/settings.py b/settings.py index 738c638..91f6f68 100644 --- a/settings.py +++ b/settings.py @@ -6,7 +6,7 @@ import os from os.path import join from django.conf import global_settings -PROJECT_PATH = os.path.normpath(os.path.dirname(__file__)) +PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__)) DEBUG = True TEMPLATE_DEBUG = DEBUG @@ -43,8 +43,8 @@ USE_I18N = True # Absolute path to the directory that holds media. # Example: "/home/media/media.lawrence.com/" -MEDIA_ROOT = join(PROJECT_PATH, 'media') -STATIC_ROOT = join(PROJECT_PATH, 'static') +MEDIA_ROOT = join(PROJECT_ROOT, 'media') +STATIC_ROOT = join(PROJECT_ROOT, 'static') # URL that handles the media served from MEDIA_ROOT. Make sure to use a # trailing slash if there is a path component (optional in other cases). @@ -56,9 +56,6 @@ MEDIA_URL = '/texts/' # Examples: "http://foo.com/media/", "/media/". ADMIN_MEDIA_PREFIX = '/admin/media/' -# Make this unique, and don't share it with anybody. -SECRET_KEY = '@8+=n)(@(gv0ogqm6pnvs6ag@&qa3syb^qy8@#x7f68)cyrs(*' - # List of callables that know how to import templates from various sources. TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.load_template_source', @@ -76,7 +73,7 @@ MIDDLEWARE_CLASSES = ( ROOT_URLCONF = 'texts.urls' TEMPLATE_DIRS = ( - join(PROJECT_PATH, 'templates'), + join(PROJECT_ROOT, 'templates'), ) INSTALLED_APPS = ( @@ -88,13 +85,26 @@ INSTALLED_APPS = ( 'texts.text', ) +#overwrite default settings with local settings try: - import socket - # hostname = socket.gethostname().replace('.','_') - # exec "from host_settings.%s import *" % hostname - local_settings_module = socket.gethostname().split(".")[0] - if local_settings_module: - execfile(os.path.join(PROJECT_PATH, "host_settings", "%s.py" % local_settings_module)) -except ImportError, e: - raise e + from local_settings import * +except ImportError: + pass + +# Make this unique, creates random key first at first time. +try: + SECRET_KEY +except NameError: + SECRET_FILE = os.path.join(PROJECT_ROOT, 'secret.txt') + try: + SECRET_KEY = open(SECRET_FILE).read().strip() + except IOError: + try: + from random import choice + SECRET_KEY = ''.join([choice('abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)') for i in range(50)]) + secret = file(SECRET_FILE, 'w') + secret.write(SECRET_KEY) + secret.close() + except IOError: + Exception('Please create a %s file with random characters to generate your secret key!' % SECRET_FILE)