settings.py 8.95 KB
Newer Older
Arnaud Fabre's avatar
Arnaud Fabre committed
1
2
3
4
5
6
7
8
9
10
11
12
"""
Django settings for memopol project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
Jamesie Pic's avatar
Quality    
Jamesie Pic committed
13
from socket import gethostname
Arnaud Fabre's avatar
Arnaud Fabre committed
14

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

Jamesie Pic's avatar
Jamesie Pic committed
18
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Arnaud Fabre's avatar
Arnaud Fabre committed
19

Jamesie Pic's avatar
Jamesie Pic committed
20
21
22
DATA_DIR = os.environ.get('OPENSHIFT_DATA_DIR', 'data')
if not os.path.exists(DATA_DIR):
    os.makedirs(DATA_DIR)
Arnaud Fabre's avatar
Arnaud Fabre committed
23

Jamesie Pic's avatar
Jamesie Pic committed
24
25
26
LOG_DIR = os.environ.get('OPENSHIFT_LOG_DIR', 'log')
if not os.path.exists(LOG_DIR):
    os.makedirs(LOG_DIR)
Arnaud Fabre's avatar
Arnaud Fabre committed
27

Jamesie Pic's avatar
Quality    
Jamesie Pic committed
28
29
30
31
32
PUBLIC_DIR = os.path.join(
    os.environ.get(
        'OPENSHIFT_REPO_DIR',
        ''),
    'wsgi/static')
Arnaud Fabre's avatar
Arnaud Fabre committed
33
34
35
36
37

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
Jamesie Pic's avatar
Jamesie Pic committed
38
39
40
41
42
43
44
45
46
SECRET_FILE = os.path.join(DATA_DIR, 'secret.txt')

if not os.path.exists(SECRET_FILE):
    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()
Arnaud Fabre's avatar
Arnaud Fabre committed
47

Jamesie Pic's avatar
Jamesie Pic committed
48

Jamesie Pic's avatar
Quality    
Jamesie Pic committed
49
DEBUG = os.environ.get('DJANGO_DEBUG', False)
Arnaud Fabre's avatar
Arnaud Fabre committed
50
TEMPLATE_DEBUG = DEBUG
Jamesie Pic's avatar
Jamesie Pic committed
51
LOG_LEVEL = os.environ.get('DJANGO_LOG_LEVEL', 'DEBUG' if DEBUG else 'INFO')
Jamesie Pic's avatar
Jamesie Pic committed
52
53
54

if SECRET_KEY == 'notsecret' and not DEBUG:
    raise Exception('Please export DJANGO_SECRET_KEY or DEBUG')
Arnaud Fabre's avatar
Arnaud Fabre committed
55

Jamesie Pic's avatar
Jamesie Pic committed
56
57
58
ALLOWED_HOSTS = [
    gethostname(),
]
Arnaud Fabre's avatar
Arnaud Fabre committed
59

Jamesie Pic's avatar
Jamesie Pic committed
60
61
62
DNS = os.environ.get('OPENSHIFT_APP_DNS', None),
if DNS:
    ALLOWED_HOSTS += DNS
Arnaud Fabre's avatar
Arnaud Fabre committed
63

Jamesie Pic's avatar
Jamesie Pic committed
64
65
66
67
68
if 'DJANGO_ALLOWED_HOSTS' in os.environ:
    ALLOWED_HOSTS += os.environ.get('DJANGO_ALLOWED_HOSTS').split(',')

REDIS_DB = os.environ.get('REDIS_DB', 1)
ORGANIZATION_NAME = os.environ.get('ORGANIZATION', 'Memopol Demo')
Arnaud Fabre's avatar
Arnaud Fabre committed
69
70
71

INSTALLED_APPS = (
    # 'django.contrib.admin',
72
73
    'dal',
    'dal_select2',
Jamesie Pic's avatar
Jamesie Pic committed
74
75
    'suit',
    'django.contrib.admin',
Arnaud Fabre's avatar
Arnaud Fabre committed
76
77
78
79
80
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
Arnaud Fabre's avatar
Arnaud Fabre committed
81
    'django.contrib.humanize',
Jamesie Pic's avatar
Jamesie Pic committed
82
    'django.contrib.sites',
83
    # 3rd party apps
Arnaud Fabre's avatar
Arnaud Fabre committed
84
    'compressor',
Arnaud Fabre's avatar
Arnaud Fabre committed
85
86
    'bootstrap3',
    'datetimewidget',
87
    'django_filters',
88
    'fontawesome',
Jamesie Pic's avatar
Jamesie Pic committed
89
    'rest_framework',
90
    'taggit',
Arnaud Fabre's avatar
Arnaud Fabre committed
91
92
    # ---
    'core',
Jamesie Pic's avatar
Jamesie Pic committed
93
    'memopol',
94
    'memopol_scores',
95
    'memopol_settings',
96
    'memopol_themes',
Arnaud Fabre's avatar
Arnaud Fabre committed
97
98
    'representatives',
    'representatives_votes',
Jamesie Pic's avatar
Jamesie Pic committed
99
100
    'representatives_recommendations',
    'representatives_positions',
Arnaud Fabre's avatar
Arnaud Fabre committed
101
102
)

103
if DEBUG:
Jamesie Pic's avatar
Quality    
Jamesie Pic committed
104
105
106
107
108
109
    try:
        import debug_toolbar  # noqa
    except:
        pass
    else:
        INSTALLED_APPS += ('debug_toolbar',)
Arnaud Fabre's avatar
Arnaud Fabre committed
110

robin Tissot's avatar
robin Tissot committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
    try:
        import django_extensions  # noqa
    except:
        pass
    else:
        INSTALLED_APPS += ('django_extensions',)

    CACHES = {
        'default': {
            'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
        }
    }


Arnaud Fabre's avatar
Arnaud Fabre committed
125
126
127
128
129
130
131
132
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
133
    'django.contrib.sites.middleware.CurrentSiteMiddleware',
Arnaud Fabre's avatar
Arnaud Fabre committed
134
135
136
137
138
139
140
141
142
143
144
145
)

ROOT_URLCONF = 'memopol.urls'

WSGI_APPLICATION = 'memopol.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
    'default': {
Nicolas Joyard's avatar
Nicolas Joyard committed
146
147
148
149
150
151
152
        'NAME': os.environ.get('MEMOPOL_DB_NAME', 'memopol'),
        'USER': os.environ.get('MEMOPOL_DB_USER', 'memopol'),
        'PASSWORD': os.environ.get('MEMOPOL_DB_PASSWORD', 'memopol'),
        'HOST': os.environ.get('MEMOPOL_DB_HOST', 'localhost'),
        'PORT': os.environ.get('MEMOPOL_DB_PORT', '5432'),
        'ENGINE': os.environ.get('MEMOPOL_DB_ENGINE',
                                 'django.db.backends.postgresql_psycopg2'),
Arnaud Fabre's avatar
Arnaud Fabre committed
153
154
155
    }
}

Jamesie Pic's avatar
Jamesie Pic committed
156
157
if 'OPENSHIFT_POSTGRESQL_DB_HOST' in os.environ:
    DATABASES['default']['NAME'] = os.environ['OPENSHIFT_APP_NAME']
Jamesie Pic's avatar
Quality    
Jamesie Pic committed
158
159
160
161
    DATABASES['default']['USER'] = os.environ[
        'OPENSHIFT_POSTGRESQL_DB_USERNAME']
    DATABASES['default']['PASSWORD'] = os.environ[
        'OPENSHIFT_POSTGRESQL_DB_PASSWORD']
Jamesie Pic's avatar
Jamesie Pic committed
162
163
    DATABASES['default']['HOST'] = os.environ['OPENSHIFT_POSTGRESQL_DB_HOST']
    DATABASES['default']['PORT'] = os.environ['OPENSHIFT_POSTGRESQL_DB_PORT']
Arnaud Fabre's avatar
Arnaud Fabre committed
164
165
166
167

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

Jamesie Pic's avatar
Jamesie Pic committed
168
LANGUAGE_CODE = os.environ.get('DJANGO_LANGUAGE_CODE', 'en-us')
Arnaud Fabre's avatar
Arnaud Fabre committed
169
170
171
172
173
174
175
176
177
178
179
180
181

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

Jamesie Pic's avatar
Jamesie Pic committed
182
183
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]

Arnaud Fabre's avatar
Arnaud Fabre committed
184
STATIC_URL = '/static/'
Jamesie Pic's avatar
Jamesie Pic committed
185
186
187
188
189
COMPRESS_ROOT = 'static/'

if DATA_DIR:
    MEDIA_URL = '/static/media/'
    MEDIA_ROOT = os.path.join(DATA_DIR, 'media')
190
    COMPRESS_ROOT = os.path.join(DATA_DIR, 'compress')
Jamesie Pic's avatar
Jamesie Pic committed
191
192
193
194

if PUBLIC_DIR:
    STATIC_URL = '/static/collected/'
    STATIC_ROOT = os.path.join(PUBLIC_DIR, 'collected')
Arnaud Fabre's avatar
Arnaud Fabre committed
195
196
197

# HAML Templates
# https://github.com/jessemiller/hamlpy
Jamesie Pic's avatar
Jamesie Pic committed
198
HAMLPY_ATTR_WRAPPER = '"'
Arnaud Fabre's avatar
Arnaud Fabre committed
199

Jamesie Pic's avatar
Jamesie Pic committed
200
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
Arnaud Fabre's avatar
Arnaud Fabre committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215

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

"""
TEMPLATE_LOADERS = (
    ('django.template.loaders.cached.Loader', (
        'hamlpy.template.loaders.HamlPyFilesystemLoader',
        'hamlpy.template.loaders.HamlPyAppDirectoriesLoader',
    )),
)
"""

Jamesie Pic's avatar
Jamesie Pic committed
216
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
Jamesie Pic's avatar
Jamesie Pic committed
217
    'django.template.context_processors.request',
Nicolas Joyard's avatar
Nicolas Joyard committed
218
    'memopol.context_processors.search_form_options',
Jamesie Pic's avatar
Jamesie Pic committed
219
220
)

Arnaud Fabre's avatar
Arnaud Fabre committed
221
222
223
224
225
226
# Static files finders


STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
Arnaud Fabre's avatar
Arnaud Fabre committed
227
    # Compressor finder
Arnaud Fabre's avatar
Arnaud Fabre committed
228
229
230
    'compressor.finders.CompressorFinder',
)

Arnaud Fabre's avatar
Arnaud Fabre committed
231
# Use compressor even in debug
Arnaud Fabre's avatar
Arnaud Fabre committed
232
COMPRESS_ENABLED = False
Arnaud Fabre's avatar
Arnaud Fabre committed
233

234
235
236
237
238
if os.environ.get('OPENSHIFT_LOG_DIR', None):
    # Enable offline compression on openshift
    COMPRESS_ENABLED = True
    COMPRESS_OFFLINE = True

Nicolas Joyard's avatar
Nicolas Joyard committed
239

Arnaud Fabre's avatar
Arnaud Fabre committed
240
241
COMPRESS_PRECOMPILERS = (
    # ('text/coffeescript', 'coffee --compile --stdio'),
Nicolas Joyard's avatar
Nicolas Joyard committed
242
    # ('text/less', 'lesscpy {infile}'),
243
    # ('text/x-scss', 'django_libsass.SassCompiler'),
244
245
    # ('text/x-sass', 'sass {infile} {outfile}'),
    # ('text/x-scss', 'sass --scss {infile} {outfile}'),
Arnaud Fabre's avatar
Arnaud Fabre committed
246
247
248
    # ('text/stylus', 'stylus < {infile} > {outfile}'),
    # ('text/foobar', 'path.to.MyPrecompilerFilter'),
)
Arnaud Fabre's avatar
Arnaud Fabre committed
249
250
251
252
253
254
255
256
257
258
259
260


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
261
            'level': LOG_LEVEL,
Arnaud Fabre's avatar
Arnaud Fabre committed
262
263
264
265
266
267
268
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
    },
    'loggers': {
        'memopol': {
            'handlers': ['console'],
Jamesie Pic's avatar
Jamesie Pic committed
269
            'level': LOG_LEVEL,
Arnaud Fabre's avatar
Arnaud Fabre committed
270
271
272
        },
        'representatives': {
            'handlers': ['console'],
Jamesie Pic's avatar
Jamesie Pic committed
273
            'level': LOG_LEVEL,
Arnaud Fabre's avatar
Arnaud Fabre committed
274
        },
Nicolas Joyard's avatar
Nicolas Joyard committed
275
276
277
278
        'representatives_positions': {
            'handlers': ['console'],
            'level': LOG_LEVEL
        },
279
280
281
282
        'representatives_recommendations': {
            'handlers': ['console'],
            'level': LOG_LEVEL
        },
Arnaud Fabre's avatar
Arnaud Fabre committed
283
284
        'representatives_votes': {
            'handlers': ['console'],
Jamesie Pic's avatar
Jamesie Pic committed
285
            'level': LOG_LEVEL,
Arnaud Fabre's avatar
Arnaud Fabre committed
286
287
288
        }
    },
}
289

Jamesie Pic's avatar
Jamesie Pic committed
290
291
292
293
294
295
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
296

Jamesie Pic's avatar
Jamesie Pic committed
297
298
299
300
    for logger in LOGGING['loggers'].values():
        logger['handlers'].append('debug')

RAVEN_FILE = os.path.join(DATA_DIR, 'sentry')
Nicolas Joyard's avatar
Nicolas Joyard committed
301
RAVEN_404_FILE = os.path.join(DATA_DIR, 'sentry.404')
Jamesie Pic's avatar
Jamesie Pic committed
302
303
304
305
306
307
308
309
310
311
312
313
314
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
315
316
317
318
319
320
321
    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
322
323
324
325
326
    with open(RAVEN_FILE, 'r') as f:
        RAVEN_CONFIG = {
            'dsn': f.read().strip()
        }

Jamesie Pic's avatar
Jamesie Pic committed
327
328
329
SITE_ID = 1
SITE_NAME = os.environ.get('DJANGO_SITE_NAME', 'La Quadrature du Net')
SITE_DOMAIN = os.environ.get('OPENSHIFT_APP_DNS', 'localhost:8000')
330

Jamesie Pic's avatar
Jamesie Pic committed
331
SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'