settings.py 8.05 KB
Newer Older
Arnaud Fabre's avatar
Arnaud Fabre committed
1 2 3
"""
Django settings for memopol project.

Nicolas Joyard's avatar
Nicolas Joyard committed
4 5 6 7 8
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)
Arnaud Fabre's avatar
Arnaud Fabre committed
9 10 11
"""

import os
12
from socket import gethostname
Arnaud Fabre's avatar
Arnaud Fabre committed
13

Jamesie Pic's avatar
Jamesie Pic committed
14
from django.conf import global_settings
Jamesie Pic's avatar
Quality  
Jamesie Pic committed
15
from django.utils.crypto import get_random_string
Arnaud Fabre's avatar
Arnaud Fabre committed
16 17


Nicolas Joyard's avatar
Nicolas Joyard committed
18 19 20 21
###############################################################################
#
# Base memopol settings
#
Jamesie Pic's avatar
Jamesie Pic committed
22

Jamesie Pic's avatar
Jamesie Pic committed
23
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
24 25 26 27
DEBUG = os.getenv('DJANGO_DEBUG', False)

if hasattr(DEBUG, 'lower') and DEBUG.lower() in ('false', '0', 'none'):
    DEBUG = False  # Support none-ish values
Arnaud Fabre's avatar
Arnaud Fabre committed
28

Nicolas Joyard's avatar
Nicolas Joyard committed
29 30 31
#
# Main Django config
#
Arnaud Fabre's avatar
Arnaud Fabre committed
32

Nicolas Joyard's avatar
Nicolas Joyard committed
33 34
ROOT_URLCONF = 'memopol.urls'
WSGI_APPLICATION = 'memopol.wsgi.application'
Jamesie Pic's avatar
Jamesie Pic committed
35

Nicolas Joyard's avatar
Nicolas Joyard committed
36 37 38
#
# Apps
#
Arnaud Fabre's avatar
Arnaud Fabre committed
39 40

INSTALLED_APPS = (
41 42
    'dal',
    'dal_select2',
Jamesie Pic's avatar
Jamesie Pic committed
43 44
    'suit',
    'django.contrib.admin',
Arnaud Fabre's avatar
Arnaud Fabre committed
45 46 47 48 49
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
Arnaud Fabre's avatar
Arnaud Fabre committed
50
    'django.contrib.humanize',
Jamesie Pic's avatar
Jamesie Pic committed
51
    'django.contrib.sites',
Nicolas Joyard's avatar
Nicolas Joyard committed
52

53
    # 3rd party apps
Arnaud Fabre's avatar
Arnaud Fabre committed
54
    'compressor',
Arnaud Fabre's avatar
Arnaud Fabre committed
55 56
    'bootstrap3',
    'datetimewidget',
57
    'django_filters',
58
    'fontawesome',
Jamesie Pic's avatar
Jamesie Pic committed
59
    'rest_framework',
60
    'taggit',
61
    'haystack',
62
    'nested_admin',
Nicolas Joyard's avatar
Nicolas Joyard committed
63 64

    # memopol apps
Arnaud Fabre's avatar
Arnaud Fabre committed
65
    'core',
Jamesie Pic's avatar
Jamesie Pic committed
66
    'memopol',
67
    'memopol_scores',
68
    'memopol_settings',
69
    'memopol_themes',
Arnaud Fabre's avatar
Arnaud Fabre committed
70 71
    'representatives',
    'representatives_votes',
Jamesie Pic's avatar
Jamesie Pic committed
72 73
    'representatives_recommendations',
    'representatives_positions',
Arnaud Fabre's avatar
Arnaud Fabre committed
74 75
)

76
if DEBUG:
Jamesie Pic's avatar
Quality  
Jamesie Pic committed
77 78 79 80 81 82
    try:
        import debug_toolbar  # noqa
    except:
        pass
    else:
        INSTALLED_APPS += ('debug_toolbar',)
Arnaud Fabre's avatar
Arnaud Fabre committed
83

robin Tissot's avatar
robin Tissot committed
84 85 86 87 88 89 90 91
    try:
        import django_extensions  # noqa
    except:
        pass
    else:
        INSTALLED_APPS += ('django_extensions',)


Nicolas Joyard's avatar
Nicolas Joyard committed
92 93 94
#
# Middleware
#
robin Tissot's avatar
robin Tissot committed
95

Arnaud Fabre's avatar
Arnaud Fabre committed
96 97 98 99 100 101 102 103
MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
Jamesie Pic's avatar
Jamesie Pic committed
104
    'django.contrib.sites.middleware.CurrentSiteMiddleware',
Arnaud Fabre's avatar
Arnaud Fabre committed
105 106
)

107 108 109 110
if 'debug_toolbar' in INSTALLED_APPS:
    MIDDLEWARE_CLASSES += (
        'debug_toolbar.middleware.DebugToolbarMiddleware',
    )
Nicolas Joyard's avatar
Nicolas Joyard committed
111 112 113
#
# Sessions
#
Arnaud Fabre's avatar
Arnaud Fabre committed
114

Nicolas Joyard's avatar
Nicolas Joyard committed
115
SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'
Arnaud Fabre's avatar
Arnaud Fabre committed
116

Nicolas Joyard's avatar
Nicolas Joyard committed
117
#
Arnaud Fabre's avatar
Arnaud Fabre committed
118
# Internationalization
Nicolas Joyard's avatar
Nicolas Joyard committed
119
#
Arnaud Fabre's avatar
Arnaud Fabre committed
120

Jamesie Pic's avatar
Jamesie Pic committed
121
LANGUAGE_CODE = os.environ.get('DJANGO_LANGUAGE_CODE', 'en-us')
Arnaud Fabre's avatar
Arnaud Fabre committed
122 123 124 125 126
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

Nicolas Joyard's avatar
Nicolas Joyard committed
127 128 129
#
# Templating
#
Arnaud Fabre's avatar
Arnaud Fabre committed
130

Nicolas Joyard's avatar
Nicolas Joyard committed
131
TEMPLATE_DEBUG = DEBUG
Jamesie Pic's avatar
Jamesie Pic committed
132
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
Arnaud Fabre's avatar
Arnaud Fabre committed
133 134 135 136 137 138

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

Jamesie Pic's avatar
Jamesie Pic committed
139
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
Jamesie Pic's avatar
Jamesie Pic committed
140
    'django.template.context_processors.request',
141
    'memopol.context_processors.search_forms',
142
    'memopol.context_processors.intro_text'
Jamesie Pic's avatar
Jamesie Pic committed
143 144
)

Nicolas Joyard's avatar
Nicolas Joyard committed
145 146 147
#
# Cache
#
Arnaud Fabre's avatar
Arnaud Fabre committed
148

Nicolas Joyard's avatar
Nicolas Joyard committed
149 150 151 152 153 154 155 156 157 158 159 160 161
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')]
Arnaud Fabre's avatar
Arnaud Fabre committed
162 163 164 165 166 167 168

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'compressor.finders.CompressorFinder',
)

Nicolas Joyard's avatar
Nicolas Joyard committed
169
MEDIA_URL = '/static/media/'
170

Nicolas Joyard's avatar
Nicolas Joyard committed
171
COMPRESS_ENABLED = False
Nicolas Joyard's avatar
Nicolas Joyard committed
172

Nicolas Joyard's avatar
Nicolas Joyard committed
173 174 175
#
# Logging
#
Arnaud Fabre's avatar
Arnaud Fabre committed
176

Nicolas Joyard's avatar
Nicolas Joyard committed
177
LOG_LEVEL = os.environ.get('DJANGO_LOG_LEVEL', 'DEBUG' if DEBUG else 'INFO')
Arnaud Fabre's avatar
Arnaud Fabre committed
178 179 180 181 182 183 184 185 186 187 188

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'simple': {
            'format': '%(levelname)s[%(module)s]: %(message)s'
        },
    },
    'handlers': {
        'console': {
Jamesie Pic's avatar
Jamesie Pic committed
189
            'level': LOG_LEVEL,
Arnaud Fabre's avatar
Arnaud Fabre committed
190 191 192 193 194 195 196
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
    },
    'loggers': {
        'memopol': {
            'handlers': ['console'],
Jamesie Pic's avatar
Jamesie Pic committed
197
            'level': LOG_LEVEL,
Arnaud Fabre's avatar
Arnaud Fabre committed
198 199 200
        },
        'representatives': {
            'handlers': ['console'],
Jamesie Pic's avatar
Jamesie Pic committed
201
            'level': LOG_LEVEL,
Arnaud Fabre's avatar
Arnaud Fabre committed
202
        },
Nicolas Joyard's avatar
Nicolas Joyard committed
203 204 205 206
        'representatives_positions': {
            'handlers': ['console'],
            'level': LOG_LEVEL
        },
207 208 209 210
        'representatives_recommendations': {
            'handlers': ['console'],
            'level': LOG_LEVEL
        },
Arnaud Fabre's avatar
Arnaud Fabre committed
211 212
        'representatives_votes': {
            'handlers': ['console'],
Jamesie Pic's avatar
Jamesie Pic committed
213
            'level': LOG_LEVEL,
Arnaud Fabre's avatar
Arnaud Fabre committed
214 215 216
        }
    },
}
217

Nicolas Joyard's avatar
Nicolas Joyard committed
218 219 220 221 222
###############################################################################
#
# Local settings
#

223 224 225 226 227 228 229 230 231 232 233
#
# Defaults
#

DATA_DIR = 'data'
LOG_DIR = 'log'
PUBLIC_DIR = 'wsgi/static'


DATABASES = {
    'default': {
234 235 236 237 238
        'NAME': os.getenv('DB_NAME', 'memopol'),
        'USER': os.getenv('DB_USER', 'memopol'),
        'PASSWORD': os.getenv('DB_PASSWORD', 'memopol'),
        'HOST': os.getenv('DB_HOST', 'localhost'),
        'PORT': os.getenv('DB_PORT', '5432'),
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
    }
}


ALLOWED_HOSTS = [
    gethostname(),
]


SITE_ID = 1
SITE_NAME = 'Memopol'
SITE_DOMAIN = gethostname()

ORGANIZATION_NAME = 'Memopol'

#
# Import local overrides
#

Nicolas Joyard's avatar
Nicolas Joyard committed
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
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')

Jamesie Pic's avatar
Jamesie Pic committed
278 279 280 281 282 283
if DEBUG:
    LOGGING['handlers']['debug'] = {
        'level': 'DEBUG',
        'class': 'logging.FileHandler',
        'filename': os.path.join(LOG_DIR, 'debug.log'),
    }
Jamesie Pic's avatar
Jamesie Pic committed
284

Jamesie Pic's avatar
Jamesie Pic committed
285 286 287
    for logger in LOGGING['loggers'].values():
        logger['handlers'].append('debug')

Nicolas Joyard's avatar
Nicolas Joyard committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
#
# 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
305
# Automaticaly add 127.0.0.1 in ALLOWED_HOSTS on DEBUG
Nicolas Joyard's avatar
Nicolas Joyard committed
306 307 308 309 310
#

if 'DJANGO_ALLOWED_HOSTS' in os.environ:
    ALLOWED_HOSTS += os.environ.get('DJANGO_ALLOWED_HOSTS').split(',')

311 312 313
if DEBUG:
    ALLOWED_HOSTS += ['127.0.0.1', 'localhost']

Nicolas Joyard's avatar
Nicolas Joyard committed
314 315 316 317 318 319 320
#
# 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.
#

Jamesie Pic's avatar
Jamesie Pic committed
321
RAVEN_FILE = os.path.join(DATA_DIR, 'sentry')
Nicolas Joyard's avatar
Nicolas Joyard committed
322
RAVEN_404_FILE = os.path.join(DATA_DIR, 'sentry.404')
Nicolas Joyard's avatar
Nicolas Joyard committed
323

Jamesie Pic's avatar
Jamesie Pic committed
324 325 326 327 328 329 330 331 332 333 334 335 336
if os.path.exists(RAVEN_FILE):
    INSTALLED_APPS += ('raven.contrib.django.raven_compat',)

    LOGGING['handlers']['sentry'] = {
        'level': 'INFO',
        'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
    }
    LOGGING['loggers']['sentry.errors'] = LOGGING['loggers']['raven'] = {
        'level': 'INFO',
        'handlers': ['console'],
        'propagate': False,
    }

Nicolas Joyard's avatar
Nicolas Joyard committed
337 338 339 340 341 342 343
    if os.path.exists(RAVEN_404_FILE):
        RAVEN_MIDDLEWARE = (
            'raven.contrib.django.raven_compat.middleware.Sentry404CatchMiddleware',  # noqa
        )

        MIDDLEWARE_CLASSES = RAVEN_MIDDLEWARE + MIDDLEWARE_CLASSES

Jamesie Pic's avatar
Jamesie Pic committed
344
    with open(RAVEN_FILE, 'r') as f:
Nicolas Joyard's avatar
Nicolas Joyard committed
345
        RAVEN_CONFIG = {'dsn': f.read().strip()}
346 347

# Haystack with Solr config
348
if not DEBUG:
349 350 351 352 353 354
    HAYSTACK_CONNECTIONS = {
        'default': {
            'ENGINE': 'haystack.backends.solr_backend.SolrEngine',
            'URL': 'http://127.0.0.1:8080/solr',
        },
    }
355 356 357 358 359 360 361
else:
    HAYSTACK_CONNECTIONS = {
        'default': {
            'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
            'PATH': os.path.join(os.path.dirname(__file__), 'whoosh_index'),
        },
    }