From 3d53199c26e61db891a3579ff056bea1b24a5b4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Schneider?= Date: Thu, 19 Jun 2014 15:42:36 +0200 Subject: [PATCH] wcsinst: rewrite settings to support an ini file Refs #4956 --- wcsinst/settings.py | 146 +++++++++++++++++++++++++++++++++----------- wcsinst/wsgi.py | 1 - 2 files changed, 112 insertions(+), 35 deletions(-) diff --git a/wcsinst/settings.py b/wcsinst/settings.py index e27e992..6e4739b 100644 --- a/wcsinst/settings.py +++ b/wcsinst/settings.py @@ -1,45 +1,66 @@ # Django settings for wcsinst project. import os +from ConfigParser import SafeConfigParser -DEBUG = 'DEBUG' in os.environ -TEMPLATE_DEBUG = DEBUG +# get configuration files from : +# 1. default-settings.ini from source code +# 2. os.environ.get('SETTINGS_INI') if it exists +# else /etc/wcsinstd/settings.ini +# and then /etc/wcsinstd/local-settings.ini +BASE_DIR = os.path.dirname(os.path.abspath(__file__)) +SETTINGS_INI = (os.path.join(BASE_DIR, 'default-settings.ini'),) +if os.environ.get('SETTINGS_INI'): + SETTINGS_INI += (os.environ.get('SETTINGS_INI'),) +else: + ETC_DIR = os.path.join('/', 'etc', 'univnautes-idp') + SETTINGS_INI += ( + os.path.join(ETC_DIR, 'settings.ini'), + os.path.join(ETC_DIR, 'local-settings.ini') + ) -PROJECT_PATH = os.path.dirname(os.path.dirname(__file__)) +config = SafeConfigParser() +config.read(SETTINGS_INI) -ADMINS = ( - # ('Your Name', 'your_email@example.com'), -) -if 'ADMINS' in os.environ: - ADMINS = filter(None, os.environ.get('ADMINS').split(':')) - ADMINS = [ admin.split(';') for admin in ADMINS ] - for admin in ADMINS: - assert len(admin) == 2, 'ADMINS setting must be a colon separated list of name and emails separated by a semi-colon' - assert '@' in admin[1], 'ADMINS setting pairs second value must be emails' - -MANAGERS = ADMINS +DEBUG = config.getboolean('debug', 'general') +INTERNAL_IPS = tuple(config.get('debug', 'internal_ips').split()) +TEMPLATE_DEBUG = config.getboolean('debug', 'template') +ADMINS = tuple(config.items('admins')) +MANAGERS = tuple(config.items('managers')) +SENTRY_DSN = config.get('debug', 'sentry_dsn') +DEBUG_TOOLBAR = config.getboolean('debug', 'toolbar') DATABASES = { 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(PROJECT_PATH, 'wcsinst.sqlite3'), + 'ENGINE': config.get('database', 'engine'), + 'NAME': config.get('database','name'), + 'USER': config.get('database','user'), + 'PASSWORD': config.get('database','password'), + 'HOST': config.get('database','host'), + 'PORT': config.get('database','port'), } } # 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 = [] +ALLOWED_HOSTS = ['*'] +USE_X_FORWARDED_HOST = True # 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. # In a Windows environment this must be set to your system time zone. -TIME_ZONE = 'Europe/Brussels' +TIME_ZONE = 'Europe/Paris' # Language code for this installation. All choices can be found here: # http://www.i18nguy.com/unicode/language-identifiers.html LANGUAGE_CODE = 'fr-fr' +gettext_noop = lambda s: s +LANGUAGES = ( + ('en', gettext_noop('English')), + ('fr', gettext_noop('French')), +) SITE_ID = 1 @@ -56,26 +77,25 @@ USE_TZ = True # Absolute filesystem path to the directory that will hold user-uploaded files. # Example: "/var/www/example.com/media/" -MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media') +MEDIA_ROOT = config.get('dirs','media_root') # URL that handles the media served from MEDIA_ROOT. Make sure to use a # trailing slash. # Examples: "http://example.com/media/", "http://media.example.com/" -MEDIA_URL = '/media/' +MEDIA_URL = '' # Absolute path to the directory static files should be collected to. # Don't put anything in this directory yourself; store your static files # in apps' "static/" subdirectories and in STATICFILES_DIRS. # Example: "/var/www/example.com/static/" -STATIC_ROOT = os.path.join(PROJECT_PATH, 'static') +STATIC_ROOT = config.get('dirs','static_root') # URL prefix for static files. # Example: "http://example.com/static/", "http://static.example.com/" STATIC_URL = '/static/' # Additional locations of static files -STATICFILES_DIRS = ( -) +STATICFILES_DIRS = tuple(config.get('dirs','static_dirs').split()) # List of finder classes that know how to find static files in # various locations. @@ -86,7 +106,7 @@ STATICFILES_FINDERS = ( ) # Make this unique, and don't share it with anybody. -SECRET_KEY = 'sw-v(^psaet3)44flti-zr!=u64mzfeaodkey(m=&^nz(=43!o' +SECRET_KEY = config.get('secrets', 'secret_key') # List of callables that know how to import templates from various sources. TEMPLATE_LOADERS = ( @@ -105,14 +125,10 @@ MIDDLEWARE_CLASSES = ( # 'django.middleware.clickjacking.XFrameOptionsMiddleware', ) -ROOT_URLCONF = 'wcsinst.urls' - # Python dotted path to the WSGI application used by Django's runserver. WSGI_APPLICATION = 'wcsinst.wsgi.application' -TEMPLATE_DIRS = ( - os.path.join(PROJECT_PATH, 'wcsinst', 'templates'), -) +TEMPLATE_DIRS = tuple(config.get('dirs', 'template_dirs').split()) INSTALLED_APPS = ( 'south', @@ -125,6 +141,16 @@ INSTALLED_APPS = ( 'django.contrib.admin', ) +ROOT_URLCONF = 'wcsinst.urls' + +if config.getboolean('cache', 'memcached'): + CACHES = { + 'default': { + 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', + 'LOCATION': '127.0.0.1:11211', + }, + } + # A sample logging configuration. The only tangible logging # performed by this configuration is to send an email to # the site admins on every HTTP 500 error when DEBUG=False. @@ -138,12 +164,32 @@ LOGGING = { '()': 'django.utils.log.RequireDebugFalse' } }, + 'formatters': { + 'verbose': { + 'format': '[%(asctime)s] %(levelname)s %(name)s: %(message)s', + 'datefmt': '%Y-%m-%d %a %H:%M:%S' + }, + 'syslog': { + 'format': '%(levelname)s %(name)s: %(message)s', + }, + }, 'handlers': { + 'console': { + 'level': 'DEBUG', + 'class':'logging.StreamHandler', + 'formatter': 'verbose', + }, 'mail_admins': { 'level': 'ERROR', 'filters': ['require_debug_false'], 'class': 'django.utils.log.AdminEmailHandler' - } + }, + 'syslog': { + 'level': 'DEBUG', + 'address': '/dev/log', + 'class': 'logging.handlers.SysLogHandler', + 'formatter': 'syslog', + }, }, 'loggers': { 'django.request': { @@ -151,23 +197,55 @@ LOGGING = { 'level': 'ERROR', 'propagate': True, }, + '': { + 'handlers': ['mail_admins', 'syslog', 'console'], + 'level': 'INFO', + } } } -WCSINSTD_URL = os.environ.get('WCSINSTD_URL') +# debug settings +if DEBUG: + LOGGING['loggers']['']['level'] = 'DEBUG' +# email settings +EMAIL_HOST = config.get('email', 'host') +EMAIL_PORT = config.getint('email', 'port') +EMAIL_HOST_USER = config.get('email', 'user') +EMAIL_HOST_PASSWORD = config.get('email', 'password') +EMAIL_SUBJECT_PREFIX = config.get('email', 'subject_prefix') +EMAIL_USE_TLS = config.getboolean('email', 'use_tls') +SERVER_EMAIL = config.get('email', 'server_email') +DEFAULT_FROM_EMAIL = config.get('email', 'default_from_email') + +# wcsinst / wcsintd settings +WCSINSTD_URL = os.environ.get('WCSINSTD_URL') if WCSINSTD_URL: INSTALLED_APPS += ('wcsinst.wcsinst',) else: INSTALLED_APPS += ('wcsinst.wcsinstd',) +WCSINST_URL_TEMPLATE = config.get('wcsinstd', 'url_template') +WCSINST_WCSCTL_SCRIPT = config.get('wcsinstd', 'wcsctl_script') +WCSINST_WCS_APP_DIR = config.get('wcsinstd', 'wcs_app_dir') -WCSINST_WCSCTL_SCRIPT = os.environ.get('WCSINST_WCSCTL_SCRIPT', 'wcsctl') +# debug toolbar needs more +if DEBUG_TOOLBAR: + DEBUG_TOOLBAR_CONFIG = {'INTERCEPT_REDIRECTS': False} + INSTALLED_APPS += ('debug_toolbar',) + MIDDLEWARE_CLASSES += ('debug_toolbar.middleware.DebugToolbarMiddleware',) -if 'SENTRY_DSN' in os.environ: +# sentry needs more +if SENTRY_DSN: + RAVEN_CONFIG = {'dsn': SENTRY_DSN} INSTALLED_APPS += ('raven.contrib.django.raven_compat',) - RAVEN_CONFIG = os.environ.get('SENTRY_DSN') + LOGGING['handlers']['sentry'] = { + 'level': 'ERROR', + 'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler', + } + LOGGING['loggers']['']['handlers'].append('sentry') try: from local_settings import * except ImportError: pass + diff --git a/wcsinst/wsgi.py b/wcsinst/wsgi.py index 3863eea..2f541b3 100644 --- a/wcsinst/wsgi.py +++ b/wcsinst/wsgi.py @@ -18,7 +18,6 @@ import os # We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks # if running multiple sites in the same mod_wsgi process. To fix this, use # mod_wsgi daemon mode with each site in its own daemon process, or use -# os.environ["DJANGO_SETTINGS_MODULE"] = "wcsinst.settings" os.environ.setdefault("DJANGO_SETTINGS_MODULE", "wcsinst.settings") # This application object is used by any WSGI server configured to use this