settings.py 7.12 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

Nicolas Joyard's avatar
Nicolas Joyard committed
23
24
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
25

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Nicolas Joyard's avatar
Nicolas Joyard committed
208
209
210
211
212
###############################################################################
#
# Local settings
#

213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#
# Defaults
#

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'

#
# Import local overrides
#

Nicolas Joyard's avatar
Nicolas Joyard committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
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
268
269
270
271
272
273
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
274

Jamesie Pic's avatar
Jamesie Pic committed
275
276
277
    for logger in LOGGING['loggers'].values():
        logger['handlers'].append('debug')

Nicolas Joyard's avatar
Nicolas Joyard committed
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#
# 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
307
RAVEN_FILE = os.path.join(DATA_DIR, 'sentry')
Nicolas Joyard's avatar
Nicolas Joyard committed
308
RAVEN_404_FILE = os.path.join(DATA_DIR, 'sentry.404')
Nicolas Joyard's avatar
Nicolas Joyard committed
309

Jamesie Pic's avatar
Jamesie Pic committed
310
311
312
313
314
315
316
317
318
319
320
321
322
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
323
324
325
326
327
328
329
    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
330
    with open(RAVEN_FILE, 'r') as f:
Nicolas Joyard's avatar
Nicolas Joyard committed
331
        RAVEN_CONFIG = {'dsn': f.read().strip()}