Commit 699c1dc3 authored by Nicolas Joyard's avatar Nicolas Joyard

Cleanup settings

Move openshift settings away, tidy up main settings and add the ability to
import local settings from a local_settings.py file.
parent b06921eb
......@@ -5,7 +5,8 @@ core/static/libs/*
# libs
static/libs
# settings
# settings
memopol/local_settings.py
memopol/config.json
# Byte-compiled / optimized / DLL files
......
"""
Memopol local settings example.
Edit and rename to local_settings.py to use.
"""
import os
from socket import gethostname
DATA_DIR = 'data'
LOG_DIR = 'log'
PUBLIC_DIR = 'wsgi/static'
DATABASES = {
'default': {
'NAME': 'memopol',
'USER': 'memopol',
'PASSWORD': 'memopol',
'HOST': 'localhost',
'PORT': '5432',
'ENGINE': 'django.db.backends.postgresql_psycopg2',
}
}
ALLOWED_HOSTS = [
gethostname(),
]
SITE_ID = 1
SITE_NAME = 'Memopol'
SITE_DOMAIN = gethostname()
ORGANIZATION_NAME = 'Memopol'
"""
Django settings for memopol project.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
Uses the following environment variables:
- DJANGO_DEBUG: set to "true" to enable debugging (default false)
- DJANGO_LANGUAGE_CODE: language (default en-us)
- DJANGO_LOG_LEVEL: log leve (defaults to INFO or DEBUG if debug enabled)
- DJANGO_ALLOWED_HOSTS: comma-separated additional allowed hosts (default none)
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
from socket import gethostname
from django.conf import global_settings
from django.utils.crypto import get_random_string
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DATA_DIR = os.environ.get('OPENSHIFT_DATA_DIR', 'data')
if not os.path.exists(DATA_DIR):
os.makedirs(DATA_DIR)
LOG_DIR = os.environ.get('OPENSHIFT_LOG_DIR', 'log')
if not os.path.exists(LOG_DIR):
os.makedirs(LOG_DIR)
PUBLIC_DIR = os.path.join(
os.environ.get(
'OPENSHIFT_REPO_DIR',
''),
'wsgi/static')
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_FILE = os.path.join(DATA_DIR, 'secret.txt')
###############################################################################
#
# Base memopol settings
#
if not os.path.exists(SECRET_FILE):
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
with open(SECRET_FILE, 'w+') as f:
f.write(get_random_string(50, chars))
with open(SECRET_FILE, 'r') as f:
SECRET_KEY = f.read()
DEBUG = os.environ.get('DJANGO_DEBUG', False)
TEMPLATE_DEBUG = DEBUG
LOG_LEVEL = os.environ.get('DJANGO_LOG_LEVEL', 'DEBUG' if DEBUG else 'INFO')
if SECRET_KEY == 'notsecret' and not DEBUG:
raise Exception('Please export DJANGO_SECRET_KEY or DEBUG')
ALLOWED_HOSTS = [
gethostname(),
]
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = os.environ.get('DJANGO_DEBUG', 'false').lower() == 'true'
DNS = os.environ.get('OPENSHIFT_APP_DNS', None),
if DNS:
ALLOWED_HOSTS += DNS
#
# Main Django config
#
if 'DJANGO_ALLOWED_HOSTS' in os.environ:
ALLOWED_HOSTS += os.environ.get('DJANGO_ALLOWED_HOSTS').split(',')
ROOT_URLCONF = 'memopol.urls'
WSGI_APPLICATION = 'memopol.wsgi.application'
REDIS_DB = os.environ.get('REDIS_DB', 1)
ORGANIZATION_NAME = os.environ.get('ORGANIZATION', 'Memopol Demo')
#
# Apps
#
INSTALLED_APPS = (
# 'django.contrib.admin',
'dal',
'dal_select2',
'suit',
......@@ -80,6 +45,7 @@ INSTALLED_APPS = (
'django.contrib.staticfiles',
'django.contrib.humanize',
'django.contrib.sites',
# 3rd party apps
'compressor',
'bootstrap3',
......@@ -88,7 +54,8 @@ INSTALLED_APPS = (
'fontawesome',
'rest_framework',
'taggit',
# ---
# memopol apps
'core',
'memopol',
'memopol_scores',
......@@ -115,12 +82,10 @@ if DEBUG:
else:
INSTALLED_APPS += ('django_extensions',)
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}
#
# Middleware
#
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
......@@ -133,70 +98,27 @@ MIDDLEWARE_CLASSES = (
'django.contrib.sites.middleware.CurrentSiteMiddleware',
)
ROOT_URLCONF = 'memopol.urls'
WSGI_APPLICATION = 'memopol.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
#
# Sessions
#
DATABASES = {
'default': {
'NAME': os.environ.get('MEMOPOL_DB_NAME', 'memopol'),
'USER': os.environ.get('MEMOPOL_DB_USER', 'memopol'),
'PASSWORD': os.environ.get('MEMOPOL_DB_PASSWORD', 'memopol'),
'HOST': os.environ.get('MEMOPOL_DB_HOST', 'localhost'),
'PORT': os.environ.get('MEMOPOL_DB_PORT', '5432'),
'ENGINE': os.environ.get('MEMOPOL_DB_ENGINE',
'django.db.backends.postgresql_psycopg2'),
}
}
if 'OPENSHIFT_POSTGRESQL_DB_HOST' in os.environ:
DATABASES['default']['NAME'] = os.environ['OPENSHIFT_APP_NAME']
DATABASES['default']['USER'] = os.environ[
'OPENSHIFT_POSTGRESQL_DB_USERNAME']
DATABASES['default']['PASSWORD'] = os.environ[
'OPENSHIFT_POSTGRESQL_DB_PASSWORD']
DATABASES['default']['HOST'] = os.environ['OPENSHIFT_POSTGRESQL_DB_HOST']
DATABASES['default']['PORT'] = os.environ['OPENSHIFT_POSTGRESQL_DB_PORT']
SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'
#
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
#
LANGUAGE_CODE = os.environ.get('DJANGO_LANGUAGE_CODE', 'en-us')
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
#
# Templating
#
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATIC_URL = '/static/'
COMPRESS_ROOT = 'static/'
if DATA_DIR:
MEDIA_URL = '/static/media/'
MEDIA_ROOT = os.path.join(DATA_DIR, 'media')
COMPRESS_ROOT = os.path.join(DATA_DIR, 'compress')
if PUBLIC_DIR:
STATIC_URL = '/static/collected/'
STATIC_ROOT = os.path.join(PUBLIC_DIR, 'collected')
# HAML Templates
# https://github.com/jessemiller/hamlpy
HAMLPY_ATTR_WRAPPER = '"'
TEMPLATE_DEBUG = DEBUG
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
TEMPLATE_LOADERS = (
......@@ -204,49 +126,44 @@ TEMPLATE_LOADERS = (
'django.template.loaders.app_directories.Loader',
)
"""
TEMPLATE_LOADERS = (
('django.template.loaders.cached.Loader', (
'hamlpy.template.loaders.HamlPyFilesystemLoader',
'hamlpy.template.loaders.HamlPyAppDirectoriesLoader',
)),
)
"""
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
'django.template.context_processors.request',
'memopol.context_processors.search_form_options',
)
# Static files finders
#
# Cache
#
if DEBUG:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}
#
# Static files and related settings
#
STATIC_URL = '/static/collected/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# Compressor finder
'compressor.finders.CompressorFinder',
)
# Use compressor even in debug
COMPRESS_ENABLED = False
if os.environ.get('OPENSHIFT_LOG_DIR', None):
# Enable offline compression on openshift
COMPRESS_ENABLED = True
COMPRESS_OFFLINE = True
MEDIA_URL = '/static/media/'
COMPRESS_ENABLED = False
COMPRESS_PRECOMPILERS = (
# ('text/coffeescript', 'coffee --compile --stdio'),
# ('text/less', 'lesscpy {infile}'),
# ('text/x-scss', 'django_libsass.SassCompiler'),
# ('text/x-sass', 'sass {infile} {outfile}'),
# ('text/x-scss', 'sass --scss {infile} {outfile}'),
# ('text/stylus', 'stylus < {infile} > {outfile}'),
# ('text/foobar', 'path.to.MyPrecompilerFilter'),
)
#
# Logging
#
LOG_LEVEL = os.environ.get('DJANGO_LOG_LEVEL', 'DEBUG' if DEBUG else 'INFO')
LOGGING = {
'version': 1,
......@@ -287,6 +204,30 @@ LOGGING = {
},
}
###############################################################################
#
# Local settings
#
try:
from local_settings import * # noqa
except ImportError:
pass
#
# Local directories
#
if not os.path.exists(DATA_DIR):
os.makedirs(DATA_DIR)
if not os.path.exists(LOG_DIR):
os.makedirs(LOG_DIR)
MEDIA_ROOT = os.path.join(DATA_DIR, 'media')
COMPRESS_ROOT = os.path.join(DATA_DIR, 'compress')
STATIC_ROOT = os.path.join(PUBLIC_DIR, 'collected')
if DEBUG:
LOGGING['handlers']['debug'] = {
'level': 'DEBUG',
......@@ -297,8 +238,38 @@ if DEBUG:
for logger in LOGGING['loggers'].values():
logger['handlers'].append('debug')
#
# Secret file
#
SECRET_FILE = os.path.join(DATA_DIR, 'secret.txt')
if not os.path.exists(SECRET_FILE):
# Create random secret on first execution
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
with open(SECRET_FILE, 'w+') as f:
f.write(get_random_string(50, chars))
with open(SECRET_FILE, 'r') as f:
SECRET_KEY = f.read()
#
# Add allowed hosts from environment
#
if 'DJANGO_ALLOWED_HOSTS' in os.environ:
ALLOWED_HOSTS += os.environ.get('DJANGO_ALLOWED_HOSTS').split(',')
#
# Raven configuration
#
# Put a 'sentry' file with Sentry DSN in DATA_DIR to enable.
# Optionally, put an empty 'sentry.404' in DATA_DIR to log 404s.
#
RAVEN_FILE = os.path.join(DATA_DIR, 'sentry')
RAVEN_404_FILE = os.path.join(DATA_DIR, 'sentry.404')
if os.path.exists(RAVEN_FILE):
INSTALLED_APPS += ('raven.contrib.django.raven_compat',)
......@@ -320,12 +291,4 @@ if os.path.exists(RAVEN_FILE):
MIDDLEWARE_CLASSES = RAVEN_MIDDLEWARE + MIDDLEWARE_CLASSES
with open(RAVEN_FILE, 'r') as f:
RAVEN_CONFIG = {
'dsn': f.read().strip()
}
SITE_ID = 1
SITE_NAME = os.environ.get('DJANGO_SITE_NAME', 'La Quadrature du Net')
SITE_DOMAIN = os.environ.get('OPENSHIFT_APP_DNS', 'localhost:8000')
SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'
RAVEN_CONFIG = {'dsn': f.read().strip()}
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