-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsettings.py
234 lines (196 loc) · 6.84 KB
/
settings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import os
import sys
from pathlib import Path
from socket import gaierror, gethostbyname, gethostname
import sentry_sdk
from dotenv import load_dotenv
TRUTHY_VALUES = ["True", "true", "T", "1"]
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# loads the configs from .env
load_dotenv(os.path.join(BASE_DIR, ".env"))
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = str(os.environ.get("SECRET_KEY"))
# SECURITY WARNING: don't run with debug turned on in production!
# os.environ returns string values for env vars
DEBUG_STR: str = os.environ.get("DEBUG", default="0")
DEBUG: bool = DEBUG_STR in TRUTHY_VALUES
ALLOWED_HOSTS = str(os.environ.get("DJANGO_ALLOWED_HOSTS")).split(" ")
try:
ALLOWED_HOSTS.append(gethostbyname(gethostname()))
except gaierror:
pass
# Application definition
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"django.contrib.humanize",
"azure_auth",
"home.apps.HomeConfig",
"django_prometheus",
"users",
]
MIDDLEWARE = [
"django_prometheus.middleware.PrometheusBeforeMiddleware",
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
"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",
"core.middleware.CustomErrorMiddleware",
# Prometheus needs to be the last middleware in the list.
# Avoid appending to this list and rather insert into -1.
"django_prometheus.middleware.PrometheusAfterMiddleware",
]
ROOT_URLCONF = "core.urls"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [BASE_DIR / "templates"],
"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",
"core.context_processors.env",
"core.context_processors.analytics",
],
},
},
]
WSGI_APPLICATION = "core.wsgi.application"
# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", # noqa: E501
},
{
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]
AUTH_USER_MODEL = "users.CustomUser"
# Internationalization
# https://docs.djangoproject.com/en/5.0/topics/i18n/
LANGUAGE_CODE = "en-gb"
TIME_ZONE = "Europe/London"
USE_I18N = False
USE_TZ = True
# https://whitenoise.readthedocs.io/en/latest/django.html#WHITENOISE_STATIC_PREFIX
STATIC_URL = "/static/"
# https://whitenoise.readthedocs.io/en/latest/django.html#make-sure-staticfiles-is-configured-correctly
STATIC_ROOT = BASE_DIR / "staticfiles"
STATICFILES_STORAGE = "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR / "media"
STATICFILES_DIRS = [BASE_DIR / "static"]
# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
)
# Catalog settings
CATALOGUE_URL = os.environ.get("CATALOGUE_URL")
CATALOGUE_TOKEN = os.environ.get("CATALOGUE_TOKEN")
ENV = os.environ.get("ENV")
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": "db.sqlite3",
}
}
# Define a service name setting for page titles
SERVICE_NAME = "Find MOJ data"
GOV_UK_SUFFIX = "GOV.UK"
MAX_RESULTS = 10_000
LOGGING = {
"version": 1, # the dictConfig format version
"disable_existing_loggers": False, # retain the default loggers
"formatters": {
"verbose": {
"format": "{levelname} {asctime} {module} {process:d} {thread:d} {message}",
"style": "{",
},
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"formatter": "verbose",
"stream": sys.stdout,
},
},
"root": {
"handlers": ["console"],
"level": os.environ.get("DJANGO_LOG_LEVEL", "DEBUG"),
},
"loggers": {
"django": {
"level": os.environ.get("DJANGO_LOG_LEVEL", "DEBUG"),
"handlers": ["console"],
"propagate": False,
},
},
}
CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",
}
}
ANALYTICS_ID: str = os.environ.get("ANALYTICS_ID", "")
ENABLE_ANALYTICS: bool = (
os.environ.get("ENABLE_ANALYTICS") in TRUTHY_VALUES
) and ANALYTICS_ID != ""
# Sentry Configuration
sentry_sdk.init(
dsn=os.environ.get(
"SENTRY_DSN_WORKAROUND"
), # Datahub overwrites with this variable unless it is renamed,
# causing Sentry to tag issues with the incorrect environment
enable_tracing=True,
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
traces_sample_rate=1.0,
# Set profiles_sample_rate to 1.0 to profile 100%
# of sampled transactions.
# We recommend adjusting this value in production.
profiles_sample_rate=1.0,
environment=ENV or "local",
)
# Enable / Disable Azure Auth
if not os.environ.get("AZURE_AUTH_ENABLED", "true") == "false":
# Adds the Azure Authentication middleware to the Django Authentication middleware
MIDDLEWARE.insert(
-1,
"azure_auth.middleware.AzureMiddleware",
)
# Azure auth configuration
AZURE_AUTH = {
"CLIENT_ID": os.environ.get("AZURE_CLIENT_ID"),
"CLIENT_SECRET": os.environ.get("AZURE_CLIENT_SECRET"),
"REDIRECT_URI": os.environ.get("AZURE_REDIRECT_URI"),
"SCOPES": ["User.Read"],
"AUTHORITY": os.environ.get("AZURE_AUTHORITY"),
"PUBLIC_PATHS": [
"/metrics",
],
}
LOGIN_URL = "/azure_auth/login"
LOGIN_REDIRECT_URL = "/" # Or any other endpoint
AUTHENTICATION_BACKENDS = ("azure_auth.backends.AzureBackend",)