From: Diane Trout Date: Fri, 25 Jul 2014 22:14:08 +0000 (-0700) Subject: Use two-scoop style multiple settings file. X-Git-Url: http://woldlab.caltech.edu/gitweb/?a=commitdiff_plain;h=3f04c30f4760aa999e996daa47f22d4c800ffdba;p=htsworkflow.git Use two-scoop style multiple settings file. This also adds a config_helper to help create an ini-file that contains the randomly generated secret key. --- diff --git a/htsworkflow/settings.py b/htsworkflow/settings.py deleted file mode 100644 index f030a95..0000000 --- a/htsworkflow/settings.py +++ /dev/null @@ -1,125 +0,0 @@ -""" -Django settings for wsgiexample project. - -For more information on this file, see -https://docs.djangoproject.com/en/1.6/topics/settings/ - -For the full list of settings and their values, see -https://docs.djangoproject.com/en/1.6/ref/settings/ -""" - -# Build paths inside the project like this: os.path.join(BASE_DIR, ...) -import os -import sys - -DJANGO_ROOT = os.path.dirname(__file__) -PROJECT_ROOT = os.path.dirname(DJANGO_ROOT) - -# Quick-start development settings - unsuitable for production -# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/ - -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'c=5&609$7)bm_u+3$2bi=ida$*a)c1(cp_0siua7uyww!1qfg_' - -DEFAULT_API_KEY = 'n7HsXGHIi0vp9j5u4TIRJyqAlXYc4wrH' - -# SECURITY WARNING: don't run with debug turned on in production! -# Override in settings_local -DEBUG = True - -TEMPLATE_DEBUG = True - -ALLOWED_HOSTS = ['jumpgate.caltech.edu'] - -# Application definition -AUTHENTICATION_BACKENDS = ( - 'samples.auth_backend.HTSUserModelBackend', ) -CUSTOM_USER_MODEL = 'samples.HTSUser' - -INSTALLED_APPS = ( - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.messages', - 'django.contrib.staticfiles', - 'django.contrib.humanize', - - 'eland_config', - 'samples', - 'experiments', - 'bcmagic', - 'inventory', - 'labels', -) - -MIDDLEWARE_CLASSES = ( - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.common.CommonMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware', - 'django.middleware.clickjacking.XFrameOptionsMiddleware', -) - -TEMPLATE_DIRS = ( - os.path.join(DJANGO_ROOT, 'templates'), -) - -ROOT_URLCONF = 'htsworkflow.urls' - -WSGI_APPLICATION = 'wsgi.application' - -# Database -# https://docs.djangoproject.com/en/1.6/ref/settings/#databases - -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(PROJECT_ROOT, 'fctracker.db'), - } -} - -EMAIL_HOST = 'localhost' - -# Internationalization -# https://docs.djangoproject.com/en/1.6/topics/i18n/ - -LANGUAGE_CODE = 'en-us' - -TIME_ZONE = 'UTC' - -USE_I18N = True - -USE_L10N = True - -USE_TZ = True - -TIME_ZONE='America/Los_Angeles' - -# Static files (CSS, JavaScript, Images) -# https://docs.djangoproject.com/en/1.6/howto/static-files/ -STATICFILES_DIRS = ( - os.path.join(DJANGO_ROOT, 'static'), -) -STATIC_URL = '/static/' - - -##### -# Application specific settings -DEFAULT_PM = 5 - -# How often to recheck the result archive -RESCAN_DELAY=1 -# Update this in settings_local to point to your flowcell result directory -RESULT_HOME_DIR = os.path.join(PROJECT_ROOT, 'test', 'result', 'flowcells') - -# configure who is sending email and who should get BCCs of announcments -NOTIFICATION_SENDER = "noreply@example.com" -NOTIFICATION_BCC=[] - -try: - # allow local customizations - from settings_local import * -except ImportError as e: - pass diff --git a/htsworkflow/settings/__init__.py b/htsworkflow/settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/htsworkflow/settings/base.py b/htsworkflow/settings/base.py new file mode 100644 index 0000000..97518e0 --- /dev/null +++ b/htsworkflow/settings/base.py @@ -0,0 +1,108 @@ +""" +Django settings for wsgiexample project. + +For more information on this file, see +https://docs.djangoproject.com/en/1.6/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/1.6/ref/settings/ +""" + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +from os.path import abspath, basename, dirname, join, normpath +import sys +import logging + +from htsworkflow.util import config_helper + +DJANGO_ROOT = dirname(dirname(abspath(__file__))) +PROJECT_ROOT = dirname(DJANGO_ROOT) + +INI_OPTIONS = config_helper.HTSWConfig() + +SECRET_KEY = INI_OPTIONS.setdefaultsecret('frontend', 'secret_key') +DEFAULT_API_KEY = INI_OPTIONS.setdefaultsecret('frontend', 'api') + +# SECURITY WARNING: don't run with debug turned on in production! +# Override in settings_local +DEBUG = False + +TEMPLATE_DEBUG = False + +# Application definition +AUTHENTICATION_BACKENDS = ( + 'samples.auth_backend.HTSUserModelBackend', ) +CUSTOM_USER_MODEL = 'samples.HTSUser' + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'django.contrib.humanize', + + 'eland_config', + 'samples', + 'experiments', + 'bcmagic', + 'inventory', + 'labels', +] + +MIDDLEWARE_CLASSES = [ + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +TEMPLATE_DIRS = [ + join(DJANGO_ROOT, 'templates'), +] + +ROOT_URLCONF = 'htsworkflow.urls' + +WSGI_APPLICATION = 'wsgi.application' + +# Database +# https://docs.djangoproject.com/en/1.6/ref/settings/#databases + +EMAIL_HOST = 'localhost' + +# Internationalization +# https://docs.djangoproject.com/en/1.6/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE='America/Los_Angeles' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/1.6/howto/static-files/ +STATICFILES_DIRS = ( + join(DJANGO_ROOT, 'static'), +) +STATIC_URL = '/static/' + +### +# Application specific settings +DEFAULT_PM = 5 + +# How often to recheck the result archive +RESCAN_DELAY=1 + +# configure who is sending email and who should get BCCs of announcments +NOTIFICATION_SENDER = "noreply@example.com" +NOTIFICATION_BCC=[] + +# Update this in settings_local to point to your flowcell result directory +RESULT_HOME_DIR = join(PROJECT_ROOT, 'test', 'result', 'flowcells') diff --git a/htsworkflow/settings/local.py b/htsworkflow/settings/local.py new file mode 100644 index 0000000..dd5c3f7 --- /dev/null +++ b/htsworkflow/settings/local.py @@ -0,0 +1,26 @@ + +from .base import * + +LOGGING = { + 'version': 1, + 'disable_existing_loggers': False, + 'formatters': { + 'simple': { + 'format': '%(module)s %(levelname)s %(message)s', + } + }, + 'handlers': { + 'console': { + 'class': 'logging.StreamHandler', + 'formatter': 'simple', + 'level': 'DEBUG', + 'stream': 'ext://sys.stdout' + } + }, + 'loggers': { + 'htsworkflow': { + 'handlers': ['console'], + 'level': 'ERROR', + } + } +} diff --git a/htsworkflow/settings/myrada.py b/htsworkflow/settings/myrada.py new file mode 100644 index 0000000..aad83a8 --- /dev/null +++ b/htsworkflow/settings/myrada.py @@ -0,0 +1,27 @@ +# configure debugging +import os +from .local import * + +DEBUG=True +TEMPLATE_DEBUG = True + +INTERNAL_IPS = ('127.0.0.1',) + +MIDDLEWARE_CLASSES.extend([ + #'debug_toolbar.middleware.DebugToolbarMiddleware', +]) + +DATABASES = { + #'default': { + #'ENGINE': 'django.db.backends.sqlite3', + #'NAME': os.path.join(settings.BASE_DIR, '..', 'fctracker.db'), + #} + 'default': { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'HOST': 'felcat.caltech.edu', + 'USER': 'diane', + 'NAME': 'htsworkflow-django1.7', + } + +} + diff --git a/htsworkflow/settings/production.py b/htsworkflow/settings/production.py new file mode 100644 index 0000000..449ff32 --- /dev/null +++ b/htsworkflow/settings/production.py @@ -0,0 +1,10 @@ +from .base import * + +ALLOWED_HOSTS = ['jumpgate.caltech.edu'] + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(PROJECT_ROOT, 'fctracker.db'), + } +} diff --git a/htsworkflow/util/config_helper.py b/htsworkflow/util/config_helper.py new file mode 100644 index 0000000..162580f --- /dev/null +++ b/htsworkflow/util/config_helper.py @@ -0,0 +1,43 @@ +"""Helper functions to manage ini file settings. +""" +import logging +import os +import ConfigParser + +from htsworkflow.util.api import make_django_secret_key + +LOGGER = logging.getLogger(__name__) + +class HTSWConfig(ConfigParser.SafeConfigParser): + '''Customization of SafeConfigParser that can open and save itself. + ''' + def __init__(self, path=[os.path.expanduser("~/.htsworkflow.ini"), + '/etc/htsworkflow.ini',]): + # ConfigParser isn't a new-style class? lame + # super(ConfigParser.SafeConfigParser, self).__init__() + ConfigParser.SafeConfigParser.__init__(self) + read_path = self.read(path) + if len(read_path) > 0: + self.filename = read_path[0] + else: + self.filename = path[0] + + def setdefaultsecret(self, section, key, length=216): + '''return current secret key, creating a new key if needed + ''' + if not self.has_section(section): + self.add_section(section) + + if not self.has_option(section, key): + secret = make_django_secret_key(length) + self.set(section, key, secret) + self.save() + return self.get(section, key) + + def save(self): + try: + ini_stream = open(self.filename, 'w') + self.write(ini_stream) + ini_stream.close() + except IOError, e: + LOGGER.info("Error saving setting: %s" % (str(e))) diff --git a/manage.py b/manage.py index 89759ca..0515cd1 100755 --- a/manage.py +++ b/manage.py @@ -3,7 +3,7 @@ import os import sys if __name__ == "__main__": - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "htsworkflow.settings") + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "htsworkflow.settings.local") from django.core.management import execute_from_command_line