settings: use new method to manage settings

Closes #5703
This commit is contained in:
Jérôme Schneider 2014-10-16 16:25:18 +02:00
parent ad989bebf0
commit cee7c0d27b
5 changed files with 227 additions and 201 deletions

14
config_example.py Normal file
View File

@ -0,0 +1,14 @@
DEBUG = True
SECRET_KEY = 'changeme'
MEDIA_ROOT = 'media'
STATIC_ROOT = 'collected-static'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'db.sqlite3',
}
}

134
hobo/default_settings.py Normal file
View File

@ -0,0 +1,134 @@
"""
Django settings for hobo 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/
"""
from django.conf import global_settings
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# 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 = 'hc^g)m7+*n+!8ej5i4*5iiv21s-y#+lpgje1w8d1jw5cyd+g%s'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'gadjo',
'hobo.environment',
'hobo.agent',
'allauth_authentic2',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'hobo.urls'
WSGI_APPLICATION = 'hobo.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# 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
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'hobo', 'templates'),
)
STATICFILES_FINDERS = global_settings.STATICFILES_FINDERS + ('gadjo.finders.XStaticFinder',)
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'hobo', 'static'),
)
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'hobo', 'locale'),
)
SERVICE_URL_TEMPLATE = 'https://${app}.example.net'
# SERVICE_TEMPLATES: possible "flavours" for the various service types.
# This variable expects a dictionary mapping service identifiers
# to a list of (template name, template title) tuples.
#
# Note: Template names are opaque identifiers, it's up to the deployment
# agents to assign some meaning to them.
#
# Example:
# SERVICE_TEMPLATES = {
# 'wcs': [('export-auquo-light.wcs', u'Au quotidien light'),
# ('export-auquo.wcs', u'Au quotidien'),
# ('export-demo.wcs', u'Au quotidien Demo')],
# }
SERVICE_TEMPLATES = None
AGENT_HOST_PATTERNS = None
AGENT_WCS_APP_DIR = '/var/lib/wcs'
AGENT_WCS_COMMAND = '/usr/sbin/wcsctl check-hobos --site-url ${wcs_url}'
try:
from kombu.common import Broadcast
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_QUEUES = (Broadcast('broadcast_tasks'), )
except ImportError:
pass

View File

@ -1,171 +1,15 @@
"""
Django settings for hobo project.
from django.conf.global_settings import *
from hobo.default_settings import *
from django.core.exceptions import ImproperlyConfigured
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/
"""
from django.conf import global_settings
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
import logging
if 'DJANGO_CONFIG_FILE' in os.environ:
logging.getLogger('hobo').debug('Loading setting file %r', os.environ['DJANGO_CONFIG_FILE'])
execfile(os.environ['DJANGO_CONFIG_FILE'])
# 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 = 'hc^g)m7+*n+!8ej5i4*5iiv21s-y#+lpgje1w8d1jw5cyd+g%s'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'gadjo',
'hobo.environment',
'hobo.agent',
'allauth_authentic2',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'hobo.urls'
WSGI_APPLICATION = 'hobo.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': os.environ.get('DATABASE_ENGINE', 'django.db.backends.sqlite3'),
'NAME': os.environ.get('DATABASE_NAME', os.path.join(BASE_DIR, 'db.sqlite3')),
}
}
# 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
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.environ.get('STATIC_ROOT', os.path.join(BASE_DIR, 'static'))
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'hobo', 'templates'),
)
STATICFILES_FINDERS = global_settings.STATICFILES_FINDERS + ('gadjo.finders.XStaticFinder',)
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'hobo', 'static'),
)
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'hobo', 'locale'),
)
SERVICE_URL_TEMPLATE = 'https://${app}.example.net'
# SERVICE_TEMPLATES: possible "flavours" for the various service types.
# This variable expects a dictionary mapping service identifiers
# to a list of (template name, template title) tuples.
#
# Note: Template names are opaque identifiers, it's up to the deployment
# agents to assign some meaning to them.
#
# Example:
# SERVICE_TEMPLATES = {
# 'wcs': [('export-auquo-light.wcs', u'Au quotidien light'),
# ('export-auquo.wcs', u'Au quotidien'),
# ('export-demo.wcs', u'Au quotidien Demo')],
# }
SERVICE_TEMPLATES = None
AGENT_HOST_PATTERNS = None
AGENT_WCS_APP_DIR = '/var/lib/wcs'
AGENT_WCS_COMMAND = '/usr/sbin/wcsctl check-hobos --site-url ${wcs_url}'
try:
from kombu.common import Broadcast
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_QUEUES = (Broadcast('broadcast_tasks'), )
except ImportError:
pass
TENANT_BASE = None
try:
from local_settings import *
except ImportError:
pass
TENANT_BASE = os.environ.get('TENANT_BASE', TENANT_BASE)
if TENANT_BASE:
SOUTH_TESTS_MIGRATE = False
if DATABASES['default']['ENGINE'] != 'django.db.backends.postgresql_psycopg2':
raise ImproperlyConfigured('MULTITENANT only work with a postgresql database')
DATABASES['default']['ENGINE'] = 'tenant_schemas.postgresql_backend'
TEMPLATE_LOADERS = ('entrouvert.djommon.multitenant.template_loader.FilesystemLoader',) + TEMPLATE_LOADERS
DEFAULT_FILE_STORAGE = 'entrouvert.djommon.multitenant.storage.TenantFileSystemStorage'
MIDDLEWARE_CLASSES = (
'entrouvert.djommon.multitenant.middleware.TenantMiddleware',
'entrouvert.djommon.multitenant.middleware.JSONSettingsMiddleware',
'entrouvert.djommon.multitenant.middleware.PythonSettingsMiddleware',
) + MIDDLEWARE_CLASSES
INSTALLED_APPS = INSTALLED_APPS + ('entrouvert.djommon.multitenant',)
TENANT_APPS = INSTALLED_APPS
TENANT_MODEL = 'multitenant.Tenant'
TENANT_TEMPLATE_DIRS = ( TENANT_BASE, )
SOUTH_DATABASE_ADAPTERS = {
'default': 'south.db.postgresql_psycopg2',
}
SHARED_APPS = (
'django.contrib.staticfiles',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
)
if 'DATABASES' not in globals():
logging.getLogger('hobo').error('Unable to boot: You must define a DATABASES in your settings')
raise ImproperlyConfigured('You must define a DATABASES variable in your settings')

View File

@ -1,29 +1,39 @@
from django.conf.global_settings import *
from hobo.default_settings import *
from django.core.exceptions import ImproperlyConfigured
import os
import logging
try:
import entrouvert
except ImportError:
raise ImproperlyConfigured('python-entrouvert MUST be installed for the multitenant mode to work')
DATABASES = {
'default': {
'ENGINE': 'tenant_schemas.postgresql_backend',
'NAME': os.environ.get('DATABASE_NAME', 'portail_admin'),
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'portail_admin',
},
}
TENANT_BASE = None
TENANT_TEMPLATE_DIRS = ( TENANT_BASE, )
TENANT_MODEL = 'multitenant.Tenant'
SOUTH_TESTS_MIGRATE = False
SOUTH_DATABASE_ADAPTERS = {
'default': 'south.db.postgresql_psycopg2',
}
MIDDLEWARE_CLASSES = (
'tenant_schemas.middleware.TenantMiddleware',
'entrouvert.djommon.multitenant.middleware.EOTenantMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
DEFAULT_FILE_STORAGE = 'entrouvert.djommon.multitenant.storage.TenantFileSystemStorage'
MIDDLEWARE_CLASSES = (
'entrouvert.djommon.multitenant.middleware.TenantMiddleware',
'entrouvert.djommon.multitenant.middleware.JSONSettingsMiddleware',
'entrouvert.djommon.multitenant.middleware.PythonSettingsMiddleware',
) + MIDDLEWARE_CLASSES
TENANT_MODEL = 'multitenant.Tenant'
TEMPLATE_LOADERS = (
'entrouvert.djommon.multitenant.template_loader.FilesystemLoader',
@ -33,25 +43,7 @@ TEMPLATE_LOADERS = (
PUBLIC_SCHEMA_URLCONF = 'hobo.manager_urls'
MULTITENANT_TEMPLATE_DIRS = (
os.environ.get('MULTITENANT_TEMPLATE_DIRS', '.'),
)
TENANT_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'gadjo',
'hobo.environment',
'allauth_authentic2',
)
SHARED_APPS = (
'tenant_schemas',
'entrouvert.djommon.multitenant',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
@ -60,4 +52,22 @@ SHARED_APPS = (
'django.contrib.admin',
)
INSTALLED_APPS = TENANT_APPS + ('tenant_schemas', 'entrouvert.djommon.multitenant')
INSTALLED_APPS = INSTALLED_APPS + ('entrouvert.djommon.multitenant',)
TENANT_APPS = INSTALLED_APPS
if 'DJANGO_CONFIG_FILE' in os.environ:
logging.getLogger('hobo').debug('Loading setting file %r', os.environ['DJANGO_CONFIG_FILE'])
execfile(os.environ['DJANGO_CONFIG_FILE'])
if not TENANT_BASE:
logging.getLogger('hobo').error('Unable to boot: You must define a TENANT_BASE in your settings')
raise ImproperlyConfigured('You must define a TENANT_BASE in your settings')
if 'DATABASES' not in globals():
logging.getLogger('hobo').error('Unable to boot: You must define a DATABASES in your settings')
raise ImproperlyConfigured('You must define a DATABASES variable in your settings')
if DATABASES['default']['ENGINE'] != 'django.db.backends.postgresql_psycopg2':
raise ImproperlyConfigured('MULTITENANT only work with django.db.backends.postgresql_psycopg2 Django db backend')
DATABASES['default']['ENGINE'] = 'tenant_schemas.postgresql_backend'

View File

@ -3,8 +3,32 @@ import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hobo.settings")
multitenant = False
config_file = False
argv = sys.argv[1:]
for arg in list(argv):
if arg.startswith('--'):
if arg.startswith('--config='):
config_file = arg.split('=')[1]
argv.pop(0)
elif arg == '--multitenant':
multitenant = True
argv.pop(0)
else:
print >>sys.stderr, 'ERR: Unsupported flag', arg
sys.exit(1)
else:
break
if config_file:
os.environ['DJANGO_CONFIG_FILE'] = config_file
if multitenant:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hobo.tenant_settings")
else:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hobo.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
execute_from_command_line(sys.argv[:1] + argv)