-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase.py
More file actions
371 lines (335 loc) · 13.1 KB
/
base.py
File metadata and controls
371 lines (335 loc) · 13.1 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import os
import socket
import sys
import warnings
from os.path import abspath, basename, dirname, join, normpath
import environ
import jsonlogging
import requests
from django.utils.translation import gettext_lazy as _
# Ignore irrelevant Openpyxl warnings
messages = [
r"Unknown type for ContentType",
r"Unknown type for _ip_UnifiedCompliancePolicyUIAction",
r"Unknown type for Sign-off status",
r"Unknown type for _ip_UnifiedCompliancePolicyProperties",
]
# And irrelevant Luigi errors, caused by using a local scheduler
messages += [
r"The configuration contains the parameter 'no_configure_logging' with value 'True' that is not consumed by the task 'core'.", # NOQA: E501
r"The configuration contains the parameter 'pidfile' with value '/usr/src/app/run/luigi.pid' that is not consumed by the task 'scheduler'.", # NOQA: E501
]
for message in messages:
warnings.filterwarnings("ignore", message)
env = environ.Env(LOG_FORMATTER=(str, "standard"))
DJANGO_ROOT = dirname(dirname(abspath(__file__)))
SITE_ROOT = dirname(DJANGO_ROOT)
SITE_NAME = basename(DJANGO_ROOT)
sys.path.append(DJANGO_ROOT)
sys.path.append(normpath(join(SITE_ROOT, "apps")))
with open(os.path.join(SITE_ROOT, "VERSION.txt")) as v_file:
APP_VERSION = v_file.readline().rstrip("\n")
SECRET_KEY = env("SECRET_KEY")
DEBUG = False
TEMPLATE_DEBUG = DEBUG
ALLOWED_HOSTS = env.list(
"DOMAIN",
default=[
env("PGDATABASE"),
env("PGDATABASE") + ".localdomain",
"localhost",
"127.0.0.1",
"host.docker.internal",
],
)
THEME = env("THEME", default="HEA_DEFAULT").lower()
try:
EC2_PRIVATE_IP = requests.get("http://169.254.169.254/2018-09-24/meta-data/local-ipv4", timeout=0.01).text
if EC2_PRIVATE_IP:
ALLOWED_HOSTS.append(EC2_PRIVATE_IP)
# If using ECS with the awsvpc network type, the call will be to the trunked ENI address
# rather than the address of the main network interface for the ECS host
ECS_PRIVATE_IP = socket.gethostbyname(socket.gethostname())
if ECS_PRIVATE_IP not in ALLOWED_HOSTS:
ALLOWED_HOSTS.append(ECS_PRIVATE_IP)
except requests.exceptions.RequestException:
pass
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
POSTGIS_TEMPLATE = f"template_{SITE_NAME.lower()}"
DATABASES = {
"default": {
"ENGINE": "django.contrib.gis.db.backends.postgis",
"NAME": env("PGDATABASE"),
"USER": env("PGUSER"),
"PASSWORD": env("PGPASSWORD"),
"HOST": env("PGHOST"),
"PORT": env.int("PGPORT", 5432),
"OPTIONS": {"sslmode": "prefer", "application_name": SITE_NAME.lower()},
"SCHEMA": f"{SITE_NAME.lower()}_owner",
"TEST": {
"TEMPLATE": f"template_{SITE_NAME.lower()}",
"SERIALIZE": False,
},
}
}
CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",
"OPTIONS": {
"MAX_ENTRIES": 1000,
},
},
}
EXTERNAL_APPS = [
"treebeard",
"rest_framework",
"django_filters",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"django.contrib.gis",
"django.contrib.admindocs",
"binary_database_files",
"django_extensions",
"rest_framework_gis",
"revproxy",
"corsheaders",
"channels",
]
PROJECT_APPS = ["common", "metadata", "baseline"]
INSTALLED_APPS = EXTERNAL_APPS + PROJECT_APPS
MIDDLEWARE = [
"django.middleware.gzip.GZipMiddleware",
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
"corsheaders.middleware.CorsMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.locale.LocaleMiddleware",
"common.middleware.language.LanguageMiddleware",
"django.middleware.common.CommonMiddleware",
# "common.middleware.RequestLoggingMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
REST_FRAMEWORK = {
"DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly",),
"DEFAULT_AUTHENTICATION_CLASSES": (
"rest_framework.authentication.SessionAuthentication",
"rest_framework.authentication.BasicAuthentication",
),
"DEFAULT_RENDERER_CLASSES": (
"rest_framework.renderers.JSONRenderer",
"rest_framework.renderers.BrowsableAPIRenderer",
"common.renderers.HtmlTableRenderer",
"rest_framework_xml.renderers.XMLRenderer",
"common.renderers.FormattedCSVRenderer",
"common.renderers.GeoJSONRenderer",
),
"DEFAULT_FILTER_BACKENDS": [
"django_filters.rest_framework.DjangoFilterBackend",
"rest_framework.filters.SearchFilter",
"rest_framework.filters.OrderingFilter",
],
"DATETIME_FORMAT": "%Y-%m-%dT%H:%M:%S",
"TEST_REQUEST_DEFAULT_FORMAT": "json",
"EXCEPTION_HANDLER": "apps.common.exception_handlers.drf_exception_handler",
"STRICT_JSON": True,
"SEARCH_PARAM": "search",
}
ASGI_APPLICATION = "hea.asgi.application"
CHANNEL_LAYERS = {"default": {"BACKEND": "channels.layers.InMemoryChannelLayer"}}
########## CORS CONFIGURATION
# See: https://github.com/ottoyiu/django-cors-headers
CORS_ALLOWED_ORIGINS = env.list("CORS_ALLOWED_ORIGINS", default=[])
CORS_ALLOWED_ORIGIN_REGEXES = env.list("CORS_ALLOWED_ORIGIN_REGEXES", default=[])
# when CORS_ALLOW_CREDENTIALS is True, it is not allowed to use
# the wildcard / CORS_ALLOW_ALL_ORIGINS
CORS_ALLOW_ALL_ORIGINS = False if (CORS_ALLOWED_ORIGINS or CORS_ALLOWED_ORIGIN_REGEXES) else True
CORS_ALLOW_CREDENTIALS = True if (CORS_ALLOWED_ORIGINS or CORS_ALLOWED_ORIGIN_REGEXES) else False
CSRF_TRUSTED_ORIGINS = env.list("CSRF_TRUSTED_ORIGINS", default=[])
########## End CORS CONFIGURATION
ROOT_URLCONF = "hea.urls"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [
normpath(join(SITE_ROOT, "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",
"common.context_processors.theme_context",
"common.context_processors.selected_settings",
],
},
},
]
WSGI_APPLICATION = "hea.wsgi.application"
AUTH_PASSWORD_VALIDATORS = [
{"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator"},
{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator"},
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"},
{"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"},
]
LANGUAGE_CODE = "en"
TIME_ZONE = "UTC"
USE_I18N = True
USE_TZ = True
LANGUAGES = (
("en", _("English")),
("fr", _("French")),
("es", _("Spanish")),
("pt", _("Portuguese")),
("ar", _("Arabic")),
)
LOCALE_PATHS = (os.path.join(SITE_ROOT, "locale"),)
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
SESSION_ENGINE = "django.contrib.sessions.backends.cached_db"
# Media (including uploaded files)
MEDIA_ROOT = normpath(join(SITE_ROOT, "media"))
MEDIA_URL = "/media/"
# Staticfiles
STATIC_HOST = env.str("DJANGO_STATIC_HOST", "")
STATIC_URL = STATIC_HOST + "/static/"
STATIC_ROOT = normpath(join(SITE_ROOT, "assets"))
STATICFILES_DIRS = (normpath(join(SITE_ROOT, "static")),)
LOGGING = {
"version": 1,
# Don't disable existing loggers, because doing so
# will stop the Gunicorn loggers from working
"disable_existing_loggers": False,
"formatters": {
"verbose": {"format": "%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s"},
"standard": {
"format": "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",
"datefmt": "%d/%b/%Y %H:%M:%S",
},
"simple": {"format": "%(levelname)s %(message)s"},
"json": {
"()": jsonlogging.LogstashFormatterV1,
"tags": [
"client=%s" % env("CLIENT"),
"app=%s" % env("APP"),
"env=%s" % env("ENV"),
"app_version=%s" % APP_VERSION,
],
},
},
"filters": {
"require_debug_false": {"()": "django.utils.log.RequireDebugFalse"},
"suppress_ws_pings": {
"()": "common.logging_filters.SuppressWebSocketPings",
},
"suppress_revproxy_noise": {
"()": "common.logging_filters.SuppressRevProxyNoise",
},
},
"handlers": {
"logfile": {
"level": "INFO",
"class": "logging.handlers.TimedRotatingFileHandler",
"filename": SITE_ROOT + "/log/django.log",
"when": "midnight",
"interval": 1,
"backupCount": 7,
"formatter": "standard",
},
"console": {
"level": "INFO",
"stream": sys.stdout,
"class": "logging.StreamHandler",
"formatter": env.str("LOG_FORMATTER", "standard"),
"filters": ["suppress_ws_pings", "suppress_revproxy_noise"],
},
"mail_admins": {
"level": "ERROR",
"class": "django.utils.log.AdminEmailHandler",
"filters": ["require_debug_false"],
"include_html": True,
},
},
"loggers": {
"django.request": {"handlers": ["console", "logfile"], "level": "INFO", "propagate": False},
"django.db.backends": {"handlers": ["console", "logfile"], "level": "INFO", "propagate": False},
"django.security": {"handlers": ["console", "logfile"], "level": "ERROR", "propagate": False},
"factory": {"handlers": ["console", "logfile"], "level": "INFO"},
"faker": {"handlers": ["console", "logfile"], "level": "INFO"},
"urllib3": {"handlers": ["console", "logfile"], "level": "INFO", "propagate": False},
"common.models": {"handlers": ["console", "logfile"], "level": "INFO", "propagate": False},
"common.signals": {"handlers": ["console", "logfile"], "level": "INFO", "propagate": False},
"uvicorn": {
"handlers": ["console"],
"level": "INFO",
"propagate": False,
},
"uvicorn.error": {
"handlers": ["console"],
"level": "DEBUG",
"propagate": False,
"filters": ["suppress_ws_pings"],
},
"uvicorn.access": {
"handlers": ["console"],
"level": "INFO",
"propagate": False,
},
"revproxy": {
"handlers": ["console"],
"level": "INFO",
"propagate": False,
"filters": ["suppress_revproxy_noise"],
},
"revproxy.view": {
"handlers": ["console"],
"level": "INFO",
"propagate": False,
"filters": ["suppress_revproxy_noise"],
},
"revproxy.response": {
"handlers": ["console"],
"level": "INFO",
"propagate": False,
"filters": ["suppress_revproxy_noise"],
},
},
# Keep root at DEBUG and use the `level` on the handler to control logging output,
# so that additional handlers can be used to get additional detail, e.g. `common.resources.LoggingResourceMixin`
"root": {"handlers": ["console", "logfile"], "level": "DEBUG"},
}
STORAGES = {
"default": {"BACKEND": "binary_database_files.storage.DatabaseStorage"},
"staticfiles": {
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
},
}
# Serve files from the database, in case they are not present on the file
# system, for example if the container has been replaced.
DATABASE_FILES_URL_METHOD = "URL_METHOD_2"
SERIALIZATION_MODULES = {
"verbose_json": "common.pipelines.serializers.verbose_json",
}
# Don't report missing HSTS preload, because we don't run SSL in local or CI environments.
# See: https://docs.djangoproject.com/en/3.0/ref/settings/#std:setting-SILENCED_SYSTEM_CHECKS
SILENCED_SYSTEM_CHECKS = [
# SECURE_HSTS_PRELOAD: https://docs.djangoproject.com/en/3.0/ref/settings/#secure-hsts-preload
"security.W021",
]
# Ensure we can delete large numbers or records through the admin, to facilitate reloading.
DATA_UPLOAD_MAX_NUMBER_FIELDS = 10000
PRIVACY_URL = "https://help.fews.net/fdp/privacy-policy"
DISCLAIMER_URL = "https://help.fews.net/fdp/data-and-information-use-and-attribution-policy"
########## DATA EXPLORER CONFIGURATION
EXPLORER_CLOUDFRONT_URL = env.str("EXPLORER_CLOUDFRONT_URL")
########## End DATA EXPLORER CONFIGURATION
# Allow GDAL/GEOS library path overrides to be set in the environment, for MacOS.
GDAL_LIBRARY_PATH = env("GDAL_LIBRARY_PATH", default=None) # For example, /opt/homebrew/lib/libgdal.dylib
GEOS_LIBRARY_PATH = env("GEOS_LIBRARY_PATH", default=None) # For example, /opt/homebrew/lib/libgeos_c.dylib