-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.py
More file actions
387 lines (333 loc) · 12.6 KB
/
settings.py
File metadata and controls
387 lines (333 loc) · 12.6 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import os
import pathlib
import traceback
import types
import typing
import boto3
import environ
import sentry_sdk
import sentry_sdk.integrations.aws_lambda
import sentry_sdk.integrations.django
if typing.TYPE_CHECKING:
import mypy_boto3_ssm
is_aws_lambda = os.environ.get("AWS_LAMBDA_FUNCTION_NAME") is not None
if is_aws_lambda and (project_name := os.environ.get("PROJECT_NAME")) and (stage := os.environ.get("API_STAGE")):
print("Running in AWS Lambda environment. Trying to load environment variables from AWS SSM Parameter Store.")
try:
ssm_client: "mypy_boto3_ssm.SSMClient" = boto3.client("ssm")
next_token = "" # nosec: B105
while next_token is not None:
result = ssm_client.get_parameters_by_path(
Path=f"/{project_name}/{stage}",
MaxResults=10,
**({"NextToken": next_token} if next_token else {}),
)
os.environ.update({p["Name"].split("/")[-1]: p["Value"] for p in result["Parameters"]})
next_token = result.get("NextToken")
print("Successfully loaded environment variables from AWS SSM Parameter Store.")
except Exception as e:
print(
"Failed to load environment variables from AWS SSM Parameter Store. Traceback: \n"
+ "".join(traceback.format_exception(e))
)
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = pathlib.Path(__file__).resolve().parent.parent
env = environ.Env(
DEBUG=(bool, False),
IS_LOCAL=(bool, False),
LOG_LEVEL=(str, "DEBUG"),
)
env.read_env(env.str("ENV_PATH", default="envfile/.env.local"))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/
SECRET_KEY = env("DJANGO_SECRET_KEY", default="local_secret_key")
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env("DEBUG")
IS_LOCAL = env("IS_LOCAL")
DEPLOYMENT_RELEASE_VERSION = os.environ.get("DEPLOYMENT_RELEASE_VERSION", "unknown")
# Loggers
SLACK = types.SimpleNamespace(token=env("SLACK_LOG_TOKEN", default=""), channel=env("SLACK_LOG_CHANNEL", default=""))
LOG_LEVEL = env("LOG_LEVEL")
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"basic": {"format": "%(asctime)s:%(module)s:%(levelname)s:%(message)s", "datefmt": "%Y-%m-%d %H:%M:%S"},
"slack": {"()": "core.logger.formatter.slack.SlackJsonFormatter"},
"cloudwatch": {"()": "core.logger.formatter.cloudwatch.CloudWatchJsonFormatter"},
},
"handlers": {
"console": {
"level": LOG_LEVEL,
"class": "logging.StreamHandler",
"formatter": "basic",
},
"cloudwatch": {
"level": LOG_LEVEL,
"class": "logging.StreamHandler",
"formatter": "cloudwatch",
},
"slack": {
"level": LOG_LEVEL,
"class": "core.logger.handler.slack.SlackHandler",
"formatter": "slack",
},
},
"loggers": {
"django.db.backends": ({"level": LOG_LEVEL, "handlers": ["console"]} if IS_LOCAL else {}),
"cloudwatch_logger": {"level": LOG_LEVEL, "handlers": ["cloudwatch"], "propagate": True},
"slack_logger": ({"level": LOG_LEVEL, "handlers": ["slack"]} if SLACK.token and SLACK.channel else {}),
},
}
# Zappa Settings
API_STAGE = env("API_STAGE", default="prod")
ADDITIONAL_TEXT_MIMETYPES: list[str] = []
ASYNC_RESPONSE_TABLE = ""
AWS_BOT_EVENT_MAPPING: dict[str, str] = {}
AWS_EVENT_MAPPING: dict[str, str] = {}
BASE_PATH = None
BINARY_SUPPORT = True
COGNITO_TRIGGER_MAPPING: dict[str, str] = {}
CONTEXT_HEADER_MAPPINGS: dict[str, str] = {}
DJANGO_SETTINGS = "core.settings"
DOMAIN = None
ENVIRONMENT_VARIABLES: dict[str, str] = {}
EXCEPTION_HANDLER = None
PROJECT_NAME = "PyConKR-backend"
ALLOWED_HOSTS = ["*"]
# CORS Settings
# pycon domain regex pattern
CORS_ALLOWED_ORIGIN_REGEXES = [
r"^https://\w+\.pycon\.kr$",
r"^http://\w+\.pycon\.kr$",
r"^https://\w+\.dev.pycon\.kr$",
r"^http://\w+\.dev.pycon\.kr$",
r"http://localhost:\d+$",
r"https://localhost:\d+$",
r"http://127.0.0.1:\d+$",
r"https://127.0.0.1:\d+$",
]
CORS_ALLOWED_ORIGINS = [
"https://pycon.kr",
"https://2025.pycon.kr",
"http://pycon.kr",
"http://2025.pycon.kr",
]
if DEBUG:
CORS_ALLOWED_ORIGIN_REGEXES += []
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOW_HEADERS = [
"authorization",
"content-type",
"x-csrftoken",
"accept",
"accept-encoding",
"origin",
"user-agent",
"x-requested-with",
]
# Application definition
INSTALLED_APPS = [
# django model translation
# https://django-modeltranslation.readthedocs.io/en/latest/installation.html#installed-apps
"modeltranslation",
# django default apps
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
# CORS
"corsheaders",
# django-rest-framework
"rest_framework",
"rest_framework.authtoken",
"drf_spectacular",
"drf_standardized_errors",
# django-filter
"django_filters",
# simple-history
"simple_history",
# zappa
"zappa_django_utils",
# For Shell Plus
"django_extensions",
# django-app
"user",
"cms",
# django-constance
"constance",
]
MIDDLEWARE = [
# Django default middlewares
"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",
# CORS
"corsheaders.middleware.CorsMiddleware",
# simple-history
"simple_history.middleware.HistoryRequestMiddleware",
# Request Response Logger
"core.middleware.request_response_logger.RequestResponseLogger",
]
ROOT_URLCONF = "core.urls"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": ["core/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",
"django.template.context_processors.request",
],
},
},
]
WSGI_APPLICATION = "core.wsgi.application"
# Database
# https://docs.djangoproject.com/en/5.2/ref/settings/#databases
DATABASES = {
"default": {
"ENGINE": env("DATABASE_ENGINE", default="django.db.backends.sqlite3"),
"NAME": env("DATABASE_NAME", default=str(BASE_DIR / "db.sqlite3")),
"PORT": env("DATABASE_PORT", default=None),
"HOST": env("DATABASE_HOST", default=None),
"USER": env("DATABASE_USER", default=None),
"PASSWORD": env("DATABASE_PASSWORD", default=None),
},
}
# Constance Settings
CONSTANCE_BACKEND = "constance.backends.database.DatabaseBackend"
CONSTANCE_CONFIG: dict[str, tuple[int, str]] = {
"DEBUG_COLLECT_SESSION_DATA": (False, "디버깅용 - 세션 데이터 수집 여부"),
}
# Password validation
# https://docs.djangoproject.com/en/5.2/ref/settings/#auth-password-validators
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"},
]
# Internationalization
# https://docs.djangoproject.com/en/5.2/topics/i18n/
LANGUAGE_CODE = "ko-kr"
TIME_ZONE = "Asia/Seoul"
USE_I18N = True
USE_TZ = True
MODELTRANSLATION_DEFAULT_LANGUAGE = "ko"
MODELTRANSLATION_LANGUAGES = ("ko", "en")
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.2/howto/static-files/
STATIC_ROOT = BASE_DIR / "static"
DEFAULT_STORAGE_BACKEND = env("DJANGO_DEFAULT_STORAGE_BACKEND", default="storages.backends.s3.S3Storage")
STATIC_STORAGE_BACKEND = env("DJANGO_STATIC_STORAGE_BACKEND", default="storages.backends.s3.S3Storage")
STORAGE_BUCKET_NAME = f"pyconkr-backend-{API_STAGE}"
STATIC_URL = (
f"https://s3.ap-northeast-2.amazonaws.com/{STORAGE_BUCKET_NAME}/"
if STATIC_STORAGE_BACKEND == "storages.backends.s3.S3Storage"
else "static/"
)
STORAGE_OPTIONS = (
{
"bucket_name": STORAGE_BUCKET_NAME,
"file_overwrite": False,
"addressing_style": "path",
}
if DEFAULT_STORAGE_BACKEND == "storages.backends.s3.S3Storage"
else {}
)
STORAGES = {
"default": {"BACKEND": DEFAULT_STORAGE_BACKEND, "OPTIONS": STORAGE_OPTIONS},
"staticfiles": {"BACKEND": STATIC_STORAGE_BACKEND, "OPTIONS": STORAGE_OPTIONS},
}
# Default primary key field type
# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = "core.fields.UUIDAutoField"
AUTH_USER_MODEL = "user.UserExt"
# Cookies
# https://docs.djangoproject.com/en/5.2/ref/settings/#cookies
COOKIE_PREFIX = (("LOCAL_" if IS_LOCAL else "DEBUG_") if DEBUG else "") + "PYCONKR_BACKEND_"
COOKIE_SAMESITE = "Lax" if IS_LOCAL else "None"
COOKIE_SECURE = not IS_LOCAL
COOKIE_HTTPONLY = True
COOKIE_DOMAIN = env("COOKIE_DOMAIN", default="pycon.kr")
SESSION_COOKIE_NAME = f"{COOKIE_PREFIX}sessionid"
SESSION_COOKIE_SAMESITE = COOKIE_SAMESITE
SESSION_COOKIE_SECURE = COOKIE_SECURE
SESSION_COOKIE_HTTPONLY = COOKIE_HTTPONLY
SESSION_COOKIE_DOMAIN = None if IS_LOCAL else COOKIE_DOMAIN
CSRF_COOKIE_NAME = f"{COOKIE_PREFIX}csrftoken"
CSRF_COOKIE_SAMESITE = COOKIE_SAMESITE
CSRF_COOKIE_SECURE = COOKIE_SECURE
CSRF_COOKIE_HTTPONLY = COOKIE_HTTPONLY
CSRF_COOKIE_DOMAIN = None if IS_LOCAL else COOKIE_DOMAIN
CSRF_TRUSTED_ORIGINS = set(env.list("CSRF_TRUSTED_ORIGINS", default=["https://pycon.kr"])) | {
"https://local.dev.pycon.kr:3000",
"https://localhost:3000",
"http://localhost:3000",
"https://127.0.0.1:3000",
"http://127.0.0.1:3000",
}
# Django Rest Framework Settings
REST_FRAMEWORK = {
"DEFAULT_VERSIONING_CLASS": "rest_framework.versioning.NamespaceVersioning",
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
"DEFAULT_RENDERER_CLASSES": ("rest_framework.renderers.JSONRenderer",),
"EXCEPTION_HANDLER": "drf_standardized_errors.handler.exception_handler",
"DEFAULT_FILTER_BACKENDS": ("django_filters.rest_framework.DjangoFilterBackend",),
}
# DRF Spectacular Settings
SPECTACULAR_SETTINGS = {
"TITLE": "PyCon KR Backend API",
"SERVE_INCLUDE_SCHEMA": False,
}
# Sentry Settings
if SENTRY_DSN := env("SENTRY_DSN", default=""):
SENTRY_TRACES_SAMPLE_RATE = env.float("SENTRY_TRACES_SAMPLE_RATE", default=1.0)
SENTRY_PROFILES_SAMPLE_RATE = env.float("SENTRY_PROFILES_SAMPLE_RATE", default=0.0)
SENTRY_IGNORED_TRACE_ROUTES = env.list("SENTRY_IGNORED_TRACE_ROUTES", default=[])
def traces_sampler(ctx: dict[str, typing.Any]) -> float:
"""
This function is used to determine if a transaction should be sampled.
from https://stackoverflow.com/a/74412613
"""
if (parent_sampled := ctx.get("parent_sampled")) is not None:
# If this transaction has a parent, we usually want to sample it
# if and only if its parent was sampled.
return parent_sampled
if "wsgi_environ" in ctx:
# Get the URL for WSGI requests
url = ctx["wsgi_environ"].get("PATH_INFO", "")
elif "asgi_scope" in ctx:
# Get the URL for ASGI requests
url = ctx["asgi_scope"].get("path", "")
else:
# Other kinds of transactions don't have a URL
url = ""
if ctx["transaction_context"]["op"] == "http.server":
# Conditions only relevant to operation "http.server"
if any(url.startswith(ignored_route) for ignored_route in SENTRY_IGNORED_TRACE_ROUTES):
return 0 # Don't trace any of these transactions
return SENTRY_TRACES_SAMPLE_RATE
sentry_sdk.init(
dsn=SENTRY_DSN,
environment=API_STAGE,
release=DEPLOYMENT_RELEASE_VERSION,
traces_sampler=traces_sampler,
profiles_sample_rate=SENTRY_PROFILES_SAMPLE_RATE,
send_default_pii=True,
integrations=[
sentry_sdk.integrations.aws_lambda.AwsLambdaIntegration(),
sentry_sdk.integrations.django.DjangoIntegration(),
],
)