From cae77f53c9f839ecf79e27c76b94a62c70697852 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Sat, 14 Dec 2013 03:41:04 +0100 Subject: [PATCH] make settings a package, split settings into base / dev / prod settings --- CHANGES.rst | 9 ++++-- docs/admin.rst | 6 ++-- nsupdate/settings/__init__.py | 0 nsupdate/{settings.py => settings/base.py} | 34 ++-------------------- nsupdate/settings/dev.py | 30 +++++++++++++++++++ nsupdate/settings/prod.py | 24 +++++++++++++++ setup.cfg | 2 +- 7 files changed, 67 insertions(+), 38 deletions(-) create mode 100644 nsupdate/settings/__init__.py rename nsupdate/{settings.py => settings/base.py} (91%) create mode 100644 nsupdate/settings/dev.py create mode 100644 nsupdate/settings/prod.py diff --git a/CHANGES.rst b/CHANGES.rst index d75a304..8eb2604 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -11,8 +11,13 @@ New Features: Other changes: -* remove .local_settings import from settings.py, improve docs about a sane - settings setup +* important: in your local_settings.py, please do your imports like this: + from nsupdate.settings.dev import * # for development + from nsupdate.settings.prod import * # for production + after that, override whatever you need to override. + importing from nsupdate.settings does not work any more, nor does the + nsupdate.local_settings hack work any more. +* improved docs about a sane settings setup * document postgreSQL setup * also support Python 2.6.x * for debugging, add django-debug-toolbar diff --git a/docs/admin.rst b/docs/admin.rst index 8a74324..a1210e4 100644 --- a/docs/admin.rst +++ b/docs/admin.rst @@ -28,10 +28,10 @@ Configuration nsupdate.info Service --------------------- -Use a local_settings.py (do not modify the nsupdate/settings.py file directly):: +Use a local_settings.py (do not modify the nsupdate/settings/*.py files directly):: - from nsupdate.settings import * - # override whatever you need to override here (read nsupdate/settings.py + from nsupdate.settings.prod import * + # override whatever you need to override here (read nsupdate/settings/*.py # to get an overview over what you might need to customize): SECRET_KEY='S3CR3T' diff --git a/nsupdate/settings/__init__.py b/nsupdate/settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/nsupdate/settings.py b/nsupdate/settings/base.py similarity index 91% rename from nsupdate/settings.py rename to nsupdate/settings/base.py index d7d73c8..3134d02 100644 --- a/nsupdate/settings.py +++ b/nsupdate/settings/base.py @@ -1,5 +1,7 @@ """ Django settings for nsupdate project + +Note: do not directly use these settings, rather use "dev" or "prod". """ # Note: django internally first loads its own defaults and then loads the @@ -8,10 +10,6 @@ Django settings for nsupdate project import os import django.conf.global_settings as DEFAULT_SETTINGS -# set this to False for production (see the docs for important hints) -DEBUG = True -TEMPLATE_DEBUG = DEBUG - # To make this work, put a unique, long, random, secret string into your environment. # E.g. in ~/.bashrc: export SECRET_KEY="..." try: @@ -43,26 +41,10 @@ DATABASES = { } } -WE_HAVE_SSL = True # True if you run a https site also, suggest that site to users if they work on the http site. SERVICE_CONTACT = 'info AT nsupdate DOT info' # shown on "about" page -# these are the service host names we deal with -BASEDOMAIN = 'nsupdate.info' -WWW_HOST = BASEDOMAIN # a host with a ipv4 and a ipv6 address -# hosts to enforce a v4 / v6 connection (to determine the respective ip) -WWW_IPV4_HOST = 'ipv4.' + BASEDOMAIN # a host with ONLY a ipv4 address -WWW_IPV6_HOST = 'ipv6.' + BASEDOMAIN # a host with ONLY a ipv6 address - -# for debugging IP detection on localhost, use this: -#WWW_IPV4_HOST = 'localhost:8000' -#WWW_IPV6_HOST = 'ip6-localhost:8000' - BAD_AGENTS = set() # useragent blacklist for /nic/update service -# Hosts/domain names that are valid for this site; required if DEBUG is False -# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts -ALLOWED_HOSTS = [WWW_HOST, WWW_IPV4_HOST, WWW_IPV6_HOST] - # Local time zone for this installation. Choices can be found here: # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name # although not all choices may be available on all operating systems. @@ -137,11 +119,6 @@ MIDDLEWARE_CLASSES = ( # Uncomment the next line for simple clickjacking protection: # 'django.middleware.clickjacking.XFrameOptionsMiddleware', ) -if DEBUG: - MIDDLEWARE_CLASSES = ( - 'debug_toolbar.middleware.DebugToolbarMiddleware', - ) + MIDDLEWARE_CLASSES - INTERNAL_IPS = ['127.0.0.1', '::1', ] # needed for DebugToolbar! TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + ( 'django.core.context_processors.request', @@ -181,11 +158,6 @@ INSTALLED_APPS = ( 'registration', 'django_extensions', ) -if DEBUG: - DEBUG_TOOLBAR_PATCH_SETTINGS = False - INSTALLED_APPS += ( - 'debug_toolbar', - ) # A sample logging configuration. # Sends an email to the site admins on every HTTP 500 error when DEBUG=False. @@ -262,13 +234,11 @@ CSRF_FAILURE_VIEW = 'nsupdate.main.views.csrf_failure_view' # Settings for CSRF cookie. CSRF_COOKIE_NAME = 'csrftoken' CSRF_COOKIE_PATH = '/' -CSRF_COOKIE_SECURE = False # use True here if you have set WE_HAVE_SSL = True CSRF_COOKIE_HTTPONLY = False # Settings for session cookie. SESSION_COOKIE_NAME = 'sessionid' SESSION_COOKIE_PATH = '/' -SESSION_COOKIE_SECURE = False # use True here if you have set WE_HAVE_SSL = True SESSION_COOKIE_HTTPONLY = True SESSION_COOKIE_AGE = 14 * 24 * 60 * 60 # 14 days, in seconds (remember_me is True) SESSION_EXPIRE_AT_BROWSER_CLOSE = True # more safe (remember_me is False) diff --git a/nsupdate/settings/dev.py b/nsupdate/settings/dev.py new file mode 100644 index 0000000..e4c9cd3 --- /dev/null +++ b/nsupdate/settings/dev.py @@ -0,0 +1,30 @@ +""" +settings for development / unit tests +""" + +from .base import * + +DEBUG = True +TEMPLATE_DEBUG = DEBUG + +WE_HAVE_SSL = False # True if you run a https site also, suggest that site to users if they work on the http site. +CSRF_COOKIE_SECURE = WE_HAVE_SSL +SESSION_COOKIE_SECURE = WE_HAVE_SSL + +BASEDOMAIN = 'nsupdate.info' +WWW_HOST = 'localhost:8000' +# for debugging IP detection on localhost: +WWW_IPV4_HOST = 'localhost:8000' +WWW_IPV6_HOST = 'ip6-localhost:8000' + +#ALLOWED_HOSTS is not needed here, as DEBUG is True + +MIDDLEWARE_CLASSES = ( + 'debug_toolbar.middleware.DebugToolbarMiddleware', +) + MIDDLEWARE_CLASSES +INTERNAL_IPS = ['127.0.0.1', '::1', ] # needed for DebugToolbar! + +DEBUG_TOOLBAR_PATCH_SETTINGS = False +INSTALLED_APPS += ( + 'debug_toolbar', +) diff --git a/nsupdate/settings/prod.py b/nsupdate/settings/prod.py new file mode 100644 index 0000000..954447b --- /dev/null +++ b/nsupdate/settings/prod.py @@ -0,0 +1,24 @@ +""" +settings for production +""" + +from .base import * + +DEBUG = False +TEMPLATE_DEBUG = DEBUG + +WE_HAVE_SSL = True # True if you run a https site also, suggest that site to users if they work on the http site. +CSRF_COOKIE_SECURE = WE_HAVE_SSL +SESSION_COOKIE_SECURE = WE_HAVE_SSL + +# these are the service host names we deal with +BASEDOMAIN = 'nsupdate.info' +WWW_HOST = BASEDOMAIN # a host with a ipv4 and a ipv6 address +# hosts to enforce a v4 / v6 connection (to determine the respective ip) +WWW_IPV4_HOST = 'ipv4.' + BASEDOMAIN # a host with ONLY a ipv4 address +WWW_IPV6_HOST = 'ipv6.' + BASEDOMAIN # a host with ONLY a ipv6 address + +# Hosts/domain names that are valid for this site; required if DEBUG is False +# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts +ALLOWED_HOSTS = [WWW_HOST, WWW_IPV4_HOST, WWW_IPV6_HOST] + diff --git a/setup.cfg b/setup.cfg index 15a7241..56ad1dd 100644 --- a/setup.cfg +++ b/setup.cfg @@ -22,7 +22,7 @@ all_files = 1 upload-dir = docs/_build/html [pytest] -DJANGO_SETTINGS_MODULE = nsupdate.settings +DJANGO_SETTINGS_MODULE = nsupdate.settings.dev pep8maxlinelength = 120 norecursedirs = .git minversion = 2.3