main.py 7.79 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
Florent Chehab's avatar
Florent Chehab committed
13
from os.path import normpath, join
Florent Chehab's avatar
Florent Chehab committed
14

Florent Chehab's avatar
Florent Chehab committed
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

Florent Chehab's avatar
Florent Chehab committed
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
    "webpack_loader",
67
    "django_filters",
Florent Chehab's avatar
Florent Chehab committed
68 69
]

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#
#
#####################################
#
#   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",
    ),
88
    "DEFAULT_FILTER_BACKENDS": ("django_filters.rest_framework.DjangoFilterBackend",),
89 90 91 92 93 94 95 96 97 98 99 100 101
}

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


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

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


Florent Chehab's avatar
Florent Chehab committed
116
MIDDLEWARE = [
117 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",
    "base_app.middleware.LoginRequiredMiddleware",
Florent Chehab's avatar
Florent Chehab committed
125
]
126 127 128 129 130 131 132 133 134

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

Florent Chehab's avatar
Florent Chehab committed
135

Florent Chehab's avatar
Florent Chehab committed
136 137 138 139 140
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
141

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

148
TESTING = "pytest" in sys.modules
149

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

Florent Chehab's avatar
Florent Chehab committed
158

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

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

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

170 171 172
AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation."
Florent Chehab's avatar
Florent Chehab committed
173
        "UserAttributeSimilarityValidator"
174 175 176 177 178
    },
    {"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
179

180
LOGIN_URL = "/user/login"
181
LOGIN_EXEMPT_URLS = [LOGIN_URL]
Florent Chehab's avatar
Florent Chehab committed
182

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

Florent Chehab's avatar
Florent Chehab committed
188
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
Florent Chehab's avatar
Florent Chehab committed
189

190 191 192 193 194 195 196 197
#
#
#####################################
#
#   Django Templating settings
#
#####################################

Florent Chehab's avatar
Florent Chehab committed
198 199 200

TEMPLATES = [
    {
201 202 203 204 205 206 207 208 209 210
        "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
211
        },
212
    }
Florent Chehab's avatar
Florent Chehab committed
213 214
]

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

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

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

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


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

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

255 256
LANGUAGE_CODE = "fr-fr"
TIME_ZONE = "Europe/Paris"
Florent Chehab's avatar
Florent Chehab committed
257 258 259
USE_I18N = True
USE_L10N = True
USE_TZ = True
260 261 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

#
#
#####################################
#
#   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"},
    },
}