Commit a344a18b authored by BERJON Matthieu's avatar BERJON Matthieu

Update of the configuration system

I decided to perform something a bit more simpler than what is suggested
in Django best practices for configuration (having a base configuration
file extended for local, staging and production. I now have a single
configuration file setup for production. I overwrite it using
environment variables that I load with the virtual environment. If the
secret key isn't loaded then we consider that we are in production
environment and must load a .env file located at the root of the
program.
There are 2 mandatory environment variables:

- ALLGO_SECRET_KEY
- ALLGO_DATABASE_PASSWORD

The documentation reference all the environement variables available.
Signed-off-by: BERJON Matthieu's avatarMatthieu Berjon <matthieu.berjon@inria.fr>
parent a08f7ae8
import os
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
APPS_DIR = os.path.join(ROOT_DIR, 'allgo')
# REQUIRED SETTINGS
# ------------------------------------------------------------------------------
# TODO
# if ALLGO_SECRET_KEY is empty:
# load .env
SECRET_KEY = os.environ['ALLGO_SECRET_KEY']
# GENERAL
# ------------------------------------------------------------------------------
DEBUG = os.environ.get('ALLGO_DEBUG', default=False)
ALLOWED_HOSTS = os.environ \
.get('ALLGO_ALLOWED_HOSTS', default='localhost') \
.split(",")
TIME_ZONE = 'UTC'
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
USE_I18N = True
USE_L10N = True
USE_TZ = True
# DATABASES
# ------------------------------------------------------------------------------
DATABASES = {
'default': {
'ENGINE': os.environ.get(
'ALLGO_DATABASE_ENGINE', default='django.db.backends.mysql'),
'NAME': os.environ.get('ALLGO_DATABASE_NAME', default='allgo'),
'USER': os.environ.get('ALLGO_DATABASE_USER', default='allgo'),
'PASSWORD': os.environ['ALLGO_DATABASE_PASSWORD'],
'HOST': os.environ.get('ALLGO_DATABASE_HOST', default='dev-mysql'),
}
}
DATABASES['default']['ATOMIC_REQUESTS'] = True
# URLS
# ------------------------------------------------------------------------------
ROOT_URLCONF = 'config.urls'
WSGI_APPLICATION = 'config.wsgi.application'
# APPS
# ------------------------------------------------------------------------------
DJANGO_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
LOCAL_APPS = [
'poc',
]
if "ALLGO_ADDITIONAL_APPS" in os.environ:
INSTALLED_APPS = DJANGO_APPS + LOCAL_APPS + \
os.environ['ALLGO_ADDITIONAL_APPS'].split(",")
else:
INSTALLED_APPS = DJANGO_APPS + LOCAL_APPS
# MIGRATIONS
# ------------------------------------------------------------------------------
MIGRATION_MODULES = {
'sites': 'django.contrib.sites.migrations'
}
# AUTHENTIFICATION
# ------------------------------------------------------------------------------
AUTHENTIFICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
]
LOGIN_REDIRECT_URL = 'main:home'
LOGOUT_REDIRECT_URL = 'main:home'
# PASSWORDS
# ------------------------------------------------------------------------------
PASSWORD_HASHERS = [
'django.contrib.auth.hashers.PBKDF2PasswordHasher',
'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
'django.contrib.auth.hashers.BCryptPasswordHasher',
]
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# MIDDLEWARE
# ------------------------------------------------------------------------------
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'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',
]
# STATIC
# ------------------------------------------------------------------------------
STATIC_ROOT = os.path.join(ROOT_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(APPS_DIR, 'static'),
]
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
# MEDIA
# ------------------------------------------------------------------------------
MEDIA_ROOT = os.path.join(APPS_DIR, 'media')
MEDIA_URL = '/media/'
# TEMPLATES
# ------------------------------------------------------------------------------
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(APPS_DIR, 'templates'),
],
'OPTIONS': {
'debug': DEBUG,
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.media',
'django.template.context_processors.i18n',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]
# EMAIL
# ------------------------------------------------------------------------------
EMAIL_BACKEND = os.environ.get(
'ALLGO_EMAIL_BACKEND',
default='django.core.mail.backends.smtp.EmailBackend')
# ADMIN
# ------------------------------------------------------------------------------
ADMIN_URL = r'^admin/'
ADMINS = [
("""Matthieu Berjon""", 'matthieu.berjon@inria.fr'),
]
MANAGERS = ADMINS
......@@ -17,6 +17,33 @@ Dependencies
Development
-----------
+-------------------------+-----------------------------------------------+
| VARIABLE | DEFAULT |
+=========================+===============================================+
| ALLGO_SECRET_KEY | None |
+-------------------------+-----------------------------------------------+
| ALLGO_DEBUG | False |
+-------------------------+-----------------------------------------------+
| ALLGO_ALLOWED_HOSTS | 'localhost' |
+-------------------------+-----------------------------------------------+
| ALLGO_DATABASE_ENGINE | 'django.db.backends.mysql' |
+-------------------------+-----------------------------------------------+
| ALLGO_DATABASE_NAME | 'allgo' |
+-------------------------+-----------------------------------------------+
| ALLGO_DATABASE_USER | 'allgo' |
+-------------------------+-----------------------------------------------+
| ALLGO_DATABASE_PASSWORD | None |
+-------------------------+-----------------------------------------------+
| ALLGO_ADDITIONAL_APPS | None |
+-------------------------+-----------------------------------------------+
| ALLGO_EMAIL_BACKEND | 'django.core.mail.backends.smtp.EmailBackend' |
+-------------------------+-----------------------------------------------+
.. warning::
In production, you must at least set up a `.env` file containing both the
environment variables ALLGO_DATABASE_PASSWORD and ALLGO_SECRET_KEY.
.. todo::
Set up of the Mysql database. Ensure that there is an existing user called
......
......@@ -2,8 +2,18 @@
import os
import sys
if os.environ['ALLGO_SECRET_KEY'] == '':
if os.path.exists('.env'):
print('Importing environment from .env...')
for line in open('.env'):
var = line.strip().split('=')
if len(var) == 2:
os.environ[var[0]] = var[1]
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
# os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
try:
from django.core.management import execute_from_command_line
except ImportError:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment