main.py 7.84 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
"""
Florent Chehab's avatar
Florent Chehab committed
2
Django settings
Florent Chehab's avatar
Florent Chehab committed
3 4 5 6 7 8 9 10 11

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

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

import os
12
import sys
13
from os.path import normpath, join
Florent Chehab's avatar
Florent Chehab committed
14

15 16
from base_app.settings.dir_locations import BACKEND_ROOT_DIR, REPO_ROOT_DIR
from .app_settings import *  # noqa: F403, F401 # We need to load app specific settings
17

18 19
# A stupid assert to prevent pycharm from removing the * import
assert MODERATION_ACTIVATED or not MODERATION_ACTIVATED  # noqa: F405
20 21 22 23 24 25 26

#####################################
#
#   Django essential settings
#
#####################################

Florent Chehab's avatar
Florent Chehab committed
27

28 29 30 31 32
try:
    SECRET_KEY = os.environ["SECRET_KEY"]
except KeyError:
    raise Exception("Env variable missing. Please run `make setup`")

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
ROOT_URLCONF = "base_app.urls"

WSGI_APPLICATION = "base_app.wsgi.application"

STATIC_URL = "/static/"
STATIC_ROOT = normpath(join(BACKEND_ROOT_DIR, "static"))

MEDIA_ROOT = normpath(join(BACKEND_ROOT_DIR, "media"))
MEDIA_URL = "/media/"

#
#
#####################################
#
#   Sub-applications in the app
#
#####################################


Florent Chehab's avatar
Florent Chehab committed
52
INSTALLED_APPS = [
53 54 55 56 57 58 59 60 61 62 63 64
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "django_cas_ng",
    "reversion",
    "reversion_compare",
    "rest_framework",
    "rest_framework.authtoken",
    "backend_app",
Florent Chehab's avatar
Florent Chehab committed
65
    "base_app",
66
    "external_data",
67
    "webpack_loader",
68
    "django_filters",
Florent Chehab's avatar
Florent Chehab committed
69 70
]

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#
#
#####################################
#
#   Rest framework configuration
#
#####################################


REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    "DEFAULT_PERMISSION_CLASSES": (
        "rest_framework.permissions.DjangoModelPermissions",
    ),
    "DEFAULT_AUTHENTICATION_CLASSES": (
        "rest_framework.authentication.SessionAuthentication",
        "rest_framework.authentication.TokenAuthentication",
    ),
89
    "DEFAULT_FILTER_BACKENDS": ("django_filters.rest_framework.DjangoFilterBackend",),
90 91 92 93 94 95 96 97 98 99 100 101 102
}

#
#
#####################################
#
#   Webpack loader config to enable frontend hot module replacement
#
#####################################


WEBPACK_LOADER = {
    "DEFAULT": {
103
        "BUNDLE_DIR_NAME": "base_app/frontend_dist/bundles/",
104 105 106 107 108 109 110 111 112 113 114 115 116
        "STATS_FILE": join(REPO_ROOT_DIR, "frontend/webpack-stats.json"),
    }
}

#
#
#####################################
#
#   Middlewares applied to the request
#
#####################################


Florent Chehab's avatar
Florent Chehab committed
117
MIDDLEWARE = [
118 119 120 121 122 123 124
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
125
    "base_app.middleware.RexDriRequestMiddleware",
Florent Chehab's avatar
Florent Chehab committed
126
]
127 128 129 130 131 132 133 134 135

#
#
#####################################
#
#   Big variables settings
#
#####################################

Florent Chehab's avatar
Florent Chehab committed
136

Florent Chehab's avatar
Florent Chehab committed
137 138 139 140 141
if os.environ["ENV"] == "DEV":
    DEBUG = True
    ALLOWED_HOSTS = ["*"]
    INSTALLED_APPS += ["django_extensions", "debug_toolbar"]
    MIDDLEWARE = ["debug_toolbar.middleware.DebugToolbarMiddleware"] + MIDDLEWARE
Florent Chehab's avatar
Florent Chehab committed
142

143
    # Force the debug toolbar to be shown
144
    DEBUG_TOOLBAR_CONFIG = {"SHOW_TOOLBAR_CALLBACK": "base_app.utils.show_toolbar"}
Florent Chehab's avatar
Florent Chehab committed
145 146
else:
    DEBUG = False
147
    ALLOWED_HOSTS = ["127.0.0.1", "localhost", "rex.dri.utc.fr"]
Florent Chehab's avatar
Florent Chehab committed
148

149
TESTING = "pytest" in sys.modules
150

151 152 153 154 155 156 157
#
#
#####################################
#
#   Authentication related
#
#####################################
Florent Chehab's avatar
Florent Chehab committed
158

159

160 161
# Use a custom User model for optimization
AUTH_USER_MODEL = "base_app.User"
Florent Chehab's avatar
Florent Chehab committed
162 163

AUTHENTICATION_BACKENDS = [
164 165
    "django.contrib.auth.backends.ModelBackend",
    "django_cas_ng.backends.CASBackend",
Florent Chehab's avatar
Florent Chehab committed
166
]
Florent Chehab's avatar
Florent Chehab committed
167

168 169
# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
Florent Chehab's avatar
Florent Chehab committed
170

171 172 173
AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation."
174
        "UserAttributeSimilarityValidator"
175 176 177 178 179
    },
    {"NAME": "django.contrib.auth.password_validation." "MinimumLengthValidator"},
    {"NAME": "django.contrib.auth.password_validation." "CommonPasswordValidator"},
    {"NAME": "django.contrib.auth.password_validation." "NumericPasswordValidator"},
]
Florent Chehab's avatar
Florent Chehab committed
180

181
LOGIN_URL = "/user/login"
182 183
RGPD_URL = "/rgpd-raw"
LOGIN_EXEMPT_URLS = [LOGIN_URL, RGPD_URL]
Florent Chehab's avatar
Florent Chehab committed
184

185
if TESTING:
186
    # In a testing environment we may want to by pass the loginRequiredMiddleware
187
    LOGIN_EXEMPT_URLS.append("api")
188
    LOGIN_EXEMPT_URLS.append("/admin/")
Florent Chehab's avatar
Florent Chehab committed
189

Florent Chehab's avatar
Florent Chehab committed
190
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
Florent Chehab's avatar
Florent Chehab committed
191

192 193 194 195 196 197 198 199
#
#
#####################################
#
#   Django Templating settings
#
#####################################

Florent Chehab's avatar
Florent Chehab committed
200 201 202

TEMPLATES = [
    {
203 204 205 206 207 208 209 210 211 212
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ]
Florent Chehab's avatar
Florent Chehab committed
213
        },
214
    }
Florent Chehab's avatar
Florent Chehab committed
215 216
]

217 218 219 220 221 222 223
#
#
#####################################
#
#   Database configuration
#
#####################################
Florent Chehab's avatar
Florent Chehab committed
224 225 226

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

Florent Chehab's avatar
Florent Chehab committed
227 228 229 230 231 232
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql_psycopg2",
        "NAME": os.environ["POSTGRES_DB"],
        "USER": os.environ["POSTGRES_USER"],
        "PASSWORD": os.environ["POSTGRES_PASSWORD"],
233 234
        "HOST": os.environ["POSTGRES_HOST"],
        "PORT": os.environ["POSTGRES_PORT"],
Florent Chehab's avatar
Florent Chehab committed
235 236 237
    }
}

238
# To use Sqlite, uncomment below and comment above
Florent Chehab's avatar
Florent Chehab committed
239 240 241
# DATABASES = {
#     "default": {
#         "ENGINE": "django.db.backends.sqlite3",
242
#         "NAME": join(BACKEND_ROOT_DIR, "./database.db"),
Florent Chehab's avatar
Florent Chehab committed
243 244
#     }
# }
Florent Chehab's avatar
Florent Chehab committed
245 246


247 248 249 250 251 252 253
#
#
#####################################
#
#   Internationalization
#
#####################################
Florent Chehab's avatar
Florent Chehab committed
254 255 256

# https://docs.djangoproject.com/en/2.0/topics/i18n/

257 258
LANGUAGE_CODE = "fr-fr"
TIME_ZONE = "Europe/Paris"
Florent Chehab's avatar
Florent Chehab committed
259 260 261
USE_I18N = True
USE_L10N = True
USE_TZ = True
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 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 307 308 309 310 311 312 313 314 315 316 317

#
#
#####################################
#
#   Logging
#
#####################################

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "filters": {
        "require_debug_false": {"()": "django.utils.log.RequireDebugFalse"},
        "require_debug_true": {"()": "django.utils.log.RequireDebugTrue"},
    },
    "formatters": {
        "verbose": {
            "format": "%(levelname)s|%(asctime)s|%(module)s|%(process)d|%(thread)d|%(message)s",
            "datefmt": "%d/%b/%Y %H:%M:%S",
        }
    },
    "handlers": {
        "console": {
            "level": "INFO",
            "filters": ["require_debug_true"],
            "class": "logging.StreamHandler",
        },
        "mail_admins": {
            "level": "ERROR",
            "filters": ["require_debug_false"],
            "class": "django.utils.log.AdminEmailHandler",
        },
        "log_to_file_django": {
            "level": "ERROR",
            "filters": ["require_debug_false"],
            "class": "logging.FileHandler",
            "formatter": "verbose",
            "filename": "/var/log/django/error.log",
        },
        "log_to_file_frontend": {
            "level": "ERROR",
            "filters": ["require_debug_false"],
            "class": "logging.FileHandler",
            "formatter": "verbose",
            "filename": "/var/log/frontend/error.log",
        },
    },
    "loggers": {
        "django": {
            "handlers": ["console", "log_to_file_django", "mail_admins"],
            "level": "INFO",
        },
        "frontend": {"handlers": ["console", "log_to_file_frontend"], "level": "INFO"},
    },
}