settings.py 7.58 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',
Nicolas Joyard's avatar
Nicolas Joyard committed
61
62

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

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

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


Nicolas Joyard's avatar
Nicolas Joyard committed
90
91
92
#
# Middleware
#
robin Tissot's avatar
robin Tissot committed
93

Arnaud Fabre's avatar
Arnaud Fabre committed
94
95
96
97
98
99
100
101
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
102
    'django.contrib.sites.middleware.CurrentSiteMiddleware',
Arnaud Fabre's avatar
Arnaud Fabre committed
103
104
)

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

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

Nicolas Joyard's avatar
Nicolas Joyard committed
115
#
Arnaud Fabre's avatar
Arnaud Fabre committed
116
# Internationalization
Nicolas Joyard's avatar
Nicolas Joyard committed
117
#
Arnaud Fabre's avatar
Arnaud Fabre committed
118

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

Nicolas Joyard's avatar
Nicolas Joyard committed
125
126
127
#
# Templating
#
Arnaud Fabre's avatar
Arnaud Fabre committed
128

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

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

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

Nicolas Joyard's avatar
Nicolas Joyard committed
143
144
145
#
# Cache
#
Arnaud Fabre's avatar
Arnaud Fabre committed
146

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

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

Nicolas Joyard's avatar
Nicolas Joyard committed
167
MEDIA_URL = '/static/media/'
168

Nicolas Joyard's avatar
Nicolas Joyard committed
169
COMPRESS_ENABLED = False
Nicolas Joyard's avatar
Nicolas Joyard committed
170

Nicolas Joyard's avatar
Nicolas Joyard committed
171
172
173
#
# Logging
#
Arnaud Fabre's avatar
Arnaud Fabre committed
174

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

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
187
            'level': LOG_LEVEL,
Arnaud Fabre's avatar
Arnaud Fabre committed
188
189
190
191
192
193
194
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
    },
    'loggers': {
        'memopol': {
            'handlers': ['console'],
Jamesie Pic's avatar
Jamesie Pic committed
195
            'level': LOG_LEVEL,
Arnaud Fabre's avatar
Arnaud Fabre committed
196
197
198
        },
        'representatives': {
            'handlers': ['console'],
Jamesie Pic's avatar
Jamesie Pic committed
199
            'level': LOG_LEVEL,
Arnaud Fabre's avatar
Arnaud Fabre committed
200
        },
Nicolas Joyard's avatar
Nicolas Joyard committed
201
202
203
204
        'representatives_positions': {
            'handlers': ['console'],
            'level': LOG_LEVEL
        },
205
206
207
208
        'representatives_recommendations': {
            'handlers': ['console'],
            'level': LOG_LEVEL
        },
Arnaud Fabre's avatar
Arnaud Fabre committed
209
210
        'representatives_votes': {
            'handlers': ['console'],
Jamesie Pic's avatar
Jamesie Pic committed
211
            'level': LOG_LEVEL,
Arnaud Fabre's avatar
Arnaud Fabre committed
212
213
214
        }
    },
}
215

Nicolas Joyard's avatar
Nicolas Joyard committed
216
217
218
219
220
###############################################################################
#
# Local settings
#

221
222
223
224
225
226
227
228
229
230
231
#
# Defaults
#

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


DATABASES = {
    'default': {
232
233
234
235
236
        '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'),
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
        '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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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
276
277
278
279
280
281
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
282

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

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

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

309
310
311
if DEBUG:
    ALLOWED_HOSTS += ['127.0.0.1', 'localhost']

Nicolas Joyard's avatar
Nicolas Joyard committed
312
313
314
315
316
317
318
#
# 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
319
RAVEN_FILE = os.path.join(DATA_DIR, 'sentry')
Nicolas Joyard's avatar
Nicolas Joyard committed
320
RAVEN_404_FILE = os.path.join(DATA_DIR, 'sentry.404')
Nicolas Joyard's avatar
Nicolas Joyard committed
321

Jamesie Pic's avatar
Jamesie Pic committed
322
323
324
325
326
327
328
329
330
331
332
333
334
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
335
336
337
338
339
340
341
    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
342
    with open(RAVEN_FILE, 'r') as f:
Nicolas Joyard's avatar
Nicolas Joyard committed
343
        RAVEN_CONFIG = {'dsn': f.read().strip()}