settings.py 6.62 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 12
"""

import os

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


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

Nicolas Joyard's avatar
Nicolas Joyard committed
22 23
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = os.environ.get('DJANGO_DEBUG', 'false').lower() == 'true'
Arnaud Fabre's avatar
Arnaud Fabre committed
24

Nicolas Joyard's avatar
Nicolas Joyard committed
25 26 27
#
# Main Django config
#
Arnaud Fabre's avatar
Arnaud Fabre committed
28

Nicolas Joyard's avatar
Nicolas Joyard committed
29 30
ROOT_URLCONF = 'memopol.urls'
WSGI_APPLICATION = 'memopol.wsgi.application'
Jamesie Pic's avatar
Jamesie Pic committed
31

Nicolas Joyard's avatar
Nicolas Joyard committed
32 33 34
#
# Apps
#
Arnaud Fabre's avatar
Arnaud Fabre committed
35 36

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

49
    # 3rd party apps
Arnaud Fabre's avatar
Arnaud Fabre committed
50
    'compressor',
Arnaud Fabre's avatar
Arnaud Fabre committed
51 52
    'bootstrap3',
    'datetimewidget',
53
    'django_filters',
54
    'fontawesome',
Jamesie Pic's avatar
Jamesie Pic committed
55
    'rest_framework',
56
    'taggit',
Nicolas Joyard's avatar
Nicolas Joyard committed
57 58

    # memopol apps
Arnaud Fabre's avatar
Arnaud Fabre committed
59
    'core',
Jamesie Pic's avatar
Jamesie Pic committed
60
    'memopol',
61
    'memopol_scores',
62
    'memopol_settings',
63
    'memopol_themes',
Arnaud Fabre's avatar
Arnaud Fabre committed
64 65
    'representatives',
    'representatives_votes',
Jamesie Pic's avatar
Jamesie Pic committed
66 67
    'representatives_recommendations',
    'representatives_positions',
Arnaud Fabre's avatar
Arnaud Fabre committed
68 69
)

70
if DEBUG:
Jamesie Pic's avatar
Quality  
Jamesie Pic committed
71 72 73 74 75 76
    try:
        import debug_toolbar  # noqa
    except:
        pass
    else:
        INSTALLED_APPS += ('debug_toolbar',)
Arnaud Fabre's avatar
Arnaud Fabre committed
77

robin Tissot's avatar
robin Tissot committed
78 79 80 81 82 83 84 85
    try:
        import django_extensions  # noqa
    except:
        pass
    else:
        INSTALLED_APPS += ('django_extensions',)


Nicolas Joyard's avatar
Nicolas Joyard committed
86 87 88
#
# Middleware
#
robin Tissot's avatar
robin Tissot committed
89

Arnaud Fabre's avatar
Arnaud Fabre committed
90 91 92 93 94 95 96 97
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
98
    'django.contrib.sites.middleware.CurrentSiteMiddleware',
Arnaud Fabre's avatar
Arnaud Fabre committed
99 100
)

Nicolas Joyard's avatar
Nicolas Joyard committed
101 102 103
#
# Sessions
#
Arnaud Fabre's avatar
Arnaud Fabre committed
104

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

Nicolas Joyard's avatar
Nicolas Joyard committed
107
#
Arnaud Fabre's avatar
Arnaud Fabre committed
108
# Internationalization
Nicolas Joyard's avatar
Nicolas Joyard committed
109
#
Arnaud Fabre's avatar
Arnaud Fabre committed
110

Jamesie Pic's avatar
Jamesie Pic committed
111
LANGUAGE_CODE = os.environ.get('DJANGO_LANGUAGE_CODE', 'en-us')
Arnaud Fabre's avatar
Arnaud Fabre committed
112 113 114 115 116
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

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

Nicolas Joyard's avatar
Nicolas Joyard committed
121
TEMPLATE_DEBUG = DEBUG
Jamesie Pic's avatar
Jamesie Pic committed
122
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
Arnaud Fabre's avatar
Arnaud Fabre committed
123 124 125 126 127 128

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

Jamesie Pic's avatar
Jamesie Pic committed
129
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
Jamesie Pic's avatar
Jamesie Pic committed
130
    'django.template.context_processors.request',
Nicolas Joyard's avatar
Nicolas Joyard committed
131
    'memopol.context_processors.search_form_options',
Jamesie Pic's avatar
Jamesie Pic committed
132 133
)

Nicolas Joyard's avatar
Nicolas Joyard committed
134 135 136
#
# Cache
#
Arnaud Fabre's avatar
Arnaud Fabre committed
137

Nicolas Joyard's avatar
Nicolas Joyard committed
138 139 140 141 142 143 144 145 146 147 148 149 150
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
151 152 153 154 155 156 157

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

Nicolas Joyard's avatar
Nicolas Joyard committed
158
MEDIA_URL = '/static/media/'
159

Nicolas Joyard's avatar
Nicolas Joyard committed
160
COMPRESS_ENABLED = False
Nicolas Joyard's avatar
Nicolas Joyard committed
161

Nicolas Joyard's avatar
Nicolas Joyard committed
162 163 164
#
# Logging
#
Arnaud Fabre's avatar
Arnaud Fabre committed
165

Nicolas Joyard's avatar
Nicolas Joyard committed
166
LOG_LEVEL = os.environ.get('DJANGO_LOG_LEVEL', 'DEBUG' if DEBUG else 'INFO')
Arnaud Fabre's avatar
Arnaud Fabre committed
167 168 169 170 171 172 173 174 175 176 177

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
178
            'level': LOG_LEVEL,
Arnaud Fabre's avatar
Arnaud Fabre committed
179 180 181 182 183 184 185
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
    },
    'loggers': {
        'memopol': {
            'handlers': ['console'],
Jamesie Pic's avatar
Jamesie Pic committed
186
            'level': LOG_LEVEL,
Arnaud Fabre's avatar
Arnaud Fabre committed
187 188 189
        },
        'representatives': {
            'handlers': ['console'],
Jamesie Pic's avatar
Jamesie Pic committed
190
            'level': LOG_LEVEL,
Arnaud Fabre's avatar
Arnaud Fabre committed
191
        },
Nicolas Joyard's avatar
Nicolas Joyard committed
192 193 194 195
        'representatives_positions': {
            'handlers': ['console'],
            'level': LOG_LEVEL
        },
196 197 198 199
        'representatives_recommendations': {
            'handlers': ['console'],
            'level': LOG_LEVEL
        },
Arnaud Fabre's avatar
Arnaud Fabre committed
200 201
        'representatives_votes': {
            'handlers': ['console'],
Jamesie Pic's avatar
Jamesie Pic committed
202
            'level': LOG_LEVEL,
Arnaud Fabre's avatar
Arnaud Fabre committed
203 204 205
        }
    },
}
206

Nicolas Joyard's avatar
Nicolas Joyard committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
###############################################################################
#
# 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')

Jamesie Pic's avatar
Jamesie Pic committed
231 232 233 234 235 236
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
237

Jamesie Pic's avatar
Jamesie Pic committed
238 239 240
    for logger in LOGGING['loggers'].values():
        logger['handlers'].append('debug')

Nicolas Joyard's avatar
Nicolas Joyard committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
#
# 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.
#

Jamesie Pic's avatar
Jamesie Pic committed
270
RAVEN_FILE = os.path.join(DATA_DIR, 'sentry')
Nicolas Joyard's avatar
Nicolas Joyard committed
271
RAVEN_404_FILE = os.path.join(DATA_DIR, 'sentry.404')
Nicolas Joyard's avatar
Nicolas Joyard committed
272

Jamesie Pic's avatar
Jamesie Pic committed
273 274 275 276 277 278 279 280 281 282 283 284 285
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
286 287 288 289 290 291 292
    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
293
    with open(RAVEN_FILE, 'r') as f:
Nicolas Joyard's avatar
Nicolas Joyard committed
294
        RAVEN_CONFIG = {'dsn': f.read().strip()}