settings.py 7.27 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__))
Nicolas Joyard's avatar
Nicolas Joyard committed
24
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
)

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

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

Nicolas Joyard's avatar
Nicolas Joyard committed
112
#
Arnaud Fabre's avatar
Arnaud Fabre committed
113
# Internationalization
Nicolas Joyard's avatar
Nicolas Joyard committed
114
#
Arnaud Fabre's avatar
Arnaud Fabre committed
115

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

Nicolas Joyard's avatar
Nicolas Joyard committed
122 123 124
#
# Templating
#
Arnaud Fabre's avatar
Arnaud Fabre committed
125

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

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

Jamesie Pic's avatar
Jamesie Pic committed
134
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
Jamesie Pic's avatar
Jamesie Pic committed
135
    'django.template.context_processors.request',
Nicolas Joyard's avatar
Nicolas Joyard committed
136
    'memopol.context_processors.search_form_options',
137
    'memopol.context_processors.intro_text'
Jamesie Pic's avatar
Jamesie Pic committed
138 139
)

Nicolas Joyard's avatar
Nicolas Joyard committed
140 141 142
#
# Cache
#
Arnaud Fabre's avatar
Arnaud Fabre committed
143

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

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

Nicolas Joyard's avatar
Nicolas Joyard committed
164
MEDIA_URL = '/static/media/'
165

Nicolas Joyard's avatar
Nicolas Joyard committed
166
COMPRESS_ENABLED = False
Nicolas Joyard's avatar
Nicolas Joyard committed
167

Nicolas Joyard's avatar
Nicolas Joyard committed
168 169 170
#
# Logging
#
Arnaud Fabre's avatar
Arnaud Fabre committed
171

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

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

Nicolas Joyard's avatar
Nicolas Joyard committed
213 214 215 216 217
###############################################################################
#
# Local settings
#

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 249 250 251 252 253
#
# 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
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
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
273 274 275 276 277 278
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
279

Jamesie Pic's avatar
Jamesie Pic committed
280 281 282
    for logger in LOGGING['loggers'].values():
        logger['handlers'].append('debug')

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

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