main.py 5.99 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
from os.path import dirname, normpath, join
13
from .app_settings import *  # noqa: F403, F401 # We need to load app specific settings
14
import sys
Florent Chehab's avatar
Florent Chehab committed
15

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
####################################
#
#   Directory locations
#
####################################

BACKEND_ROOT_DIR = dirname(dirname(dirname(os.path.abspath(__file__))))
REPO_ROOT_DIR = dirname(BACKEND_ROOT_DIR)

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

Florent Chehab's avatar
Florent Chehab committed
33

Florent Chehab's avatar
Florent Chehab committed
34
SECRET_KEY = os.environ["SECRET_KEY"]
35

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
55
INSTALLED_APPS = [
56
57
58
59
60
61
62
63
64
65
66
67
    "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
68
    "base_app",
69
    "webpack_loader",
70
    "django_filters",
Florent Chehab's avatar
Florent Chehab committed
71
72
]

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

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


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

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


Florent Chehab's avatar
Florent Chehab committed
119
MIDDLEWARE = [
120
121
122
123
124
125
126
127
    "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
128
]
129
130
131
132
133
134
135
136
137

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

Florent Chehab's avatar
Florent Chehab committed
138

Florent Chehab's avatar
Florent Chehab committed
139
140
141
142
143
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
144

145
    # Force the debug toolbar to be shown
146
    DEBUG_TOOLBAR_CONFIG = {"SHOW_TOOLBAR_CALLBACK": "base_app.utils.show_toolbar"}
Florent Chehab's avatar
Florent Chehab committed
147
148
149
else:
    DEBUG = False

150
TESTING = "pytest" in sys.modules
151

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

Florent Chehab's avatar
Florent Chehab committed
160

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

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

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

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

182
183
LOGIN_URL = "/user/login"
LOGIN_EXEMPT_URLS = [LOGIN_URL, "/admin/"]
Florent Chehab's avatar
Florent Chehab committed
184

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

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

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

Florent Chehab's avatar
Florent Chehab committed
199
200
201

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

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

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

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

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


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

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

256
257
LANGUAGE_CODE = "fr-fr"
TIME_ZONE = "Europe/Paris"
Florent Chehab's avatar
Florent Chehab committed
258
259
260
USE_I18N = True
USE_L10N = True
USE_TZ = True