main.py 6.11 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 52
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
53
INSTALLED_APPS = [
54 55 56 57 58 59 60 61 62 63 64 65
    "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
66
    "base_app",
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 125
    "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
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 147
else:
    DEBUG = False

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