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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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