· 6 years ago · Jul 24, 2019, 07:38 AM
1import logging
2
3import sentry_sdk
4
5from sentry_sdk.integrations.django import DjangoIntegration
6from sentry_sdk.integrations.logging import LoggingIntegration
7from sentry_sdk.integrations.celery import CeleryIntegration
8
9
10from .base import * # noqa
11from .base import env
12
13# GENERAL
14# ------------------------------------------------------------------------------
15# https://docs.djangoproject.com/en/dev/ref/settings/#secret-key
16SECRET_KEY = env("DJANGO_SECRET_KEY")
17JWT_AUTH["JWT_SECRET_KEY"] = SECRET_KEY
18# https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts
19ALLOWED_HOSTS = env.list("DJANGO_ALLOWED_HOSTS", default=["sahabatpena.co.id"])
20
21# DATABASES
22# ------------------------------------------------------------------------------
23DATABASES["default"] = env.db("DATABASE_URL") # noqa F405
24DATABASES["default"]["ATOMIC_REQUESTS"] = True # noqa F405
25DATABASES["default"]["CONN_MAX_AGE"] = env.int("CONN_MAX_AGE", default=60) # noqa F405
26
27# CACHES
28# ------------------------------------------------------------------------------
29CACHES = {
30 "default": {
31 "BACKEND": "django_redis.cache.RedisCache",
32 "LOCATION": env("REDIS_URL"),
33 "OPTIONS": {
34 "CLIENT_CLASS": "django_redis.client.DefaultClient",
35 # Mimicing memcache behavior.
36 # http://niwinz.github.io/django-redis/latest/#_memcached_exceptions_behavior
37 "IGNORE_EXCEPTIONS": True,
38 },
39 }
40}
41
42# SECURITY
43# ------------------------------------------------------------------------------
44# https://docs.djangoproject.com/en/dev/ref/settings/#secure-proxy-ssl-header
45SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
46# https://docs.djangoproject.com/en/dev/ref/settings/#secure-ssl-redirect
47SECURE_SSL_REDIRECT = env.bool("DJANGO_SECURE_SSL_REDIRECT", default=True)
48# https://docs.djangoproject.com/en/dev/ref/settings/#session-cookie-secure
49SESSION_COOKIE_SECURE = True
50# https://docs.djangoproject.com/en/dev/ref/settings/#csrf-cookie-secure
51CSRF_COOKIE_SECURE = True
52# https://docs.djangoproject.com/en/dev/topics/security/#ssl-https
53# https://docs.djangoproject.com/en/dev/ref/settings/#secure-hsts-seconds
54# TODO: set this to 60 seconds first and then to 518400 once you prove the former works
55SECURE_HSTS_SECONDS = 60
56# https://docs.djangoproject.com/en/dev/ref/settings/#secure-hsts-include-subdomains
57SECURE_HSTS_INCLUDE_SUBDOMAINS = env.bool(
58 "DJANGO_SECURE_HSTS_INCLUDE_SUBDOMAINS", default=True
59)
60# https://docs.djangoproject.com/en/dev/ref/settings/#secure-hsts-preload
61SECURE_HSTS_PRELOAD = env.bool("DJANGO_SECURE_HSTS_PRELOAD", default=True)
62# https://docs.djangoproject.com/en/dev/ref/middleware/#x-content-type-options-nosniff
63SECURE_CONTENT_TYPE_NOSNIFF = env.bool(
64 "DJANGO_SECURE_CONTENT_TYPE_NOSNIFF", default=True
65)
66
67# STORAGES
68# ------------------------------------------------------------------------------
69# https://django-storages.readthedocs.io/en/latest/#installation
70INSTALLED_APPS += ["storages"] # noqa F405
71
72# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
73AWS_ACCESS_KEY_ID = env("DJANGO_AWS_ACCESS_KEY_ID")
74# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
75AWS_SECRET_ACCESS_KEY = env("DJANGO_AWS_SECRET_ACCESS_KEY")
76# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
77AWS_STORAGE_BUCKET_NAME = env("DJANGO_AWS_STORAGE_BUCKET_NAME")
78# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
79AWS_QUERYSTRING_AUTH = False
80# DO NOT change these unless you know what you're doing.
81_AWS_EXPIRY = 60 * 60 * 24 * 7
82# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
83AWS_S3_OBJECT_PARAMETERS = {
84 "CacheControl": f"max-age={_AWS_EXPIRY}, s-maxage={_AWS_EXPIRY}, must-revalidate"
85}
86# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
87AWS_DEFAULT_ACL = None
88# https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
89AWS_S3_REGION_NAME = env("DJANGO_AWS_S3_REGION_NAME", default=None)
90
91
92# STATIC
93# ------------------------
94STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
95STATICFILES_STORAGE = "config.settings.production.StaticRootS3Boto3Storage"
96STATIC_URL = f"https://{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com/static/"
97
98# MEDIA
99# ------------------------------------------------------------------------------
100# region http://stackoverflow.com/questions/10390244/
101# Full-fledge class: https://stackoverflow.com/a/18046120/104731
102from storages.backends.s3boto3 import S3Boto3Storage # noqa E402
103
104
105class StaticRootS3Boto3Storage(S3Boto3Storage):
106 location = "static"
107
108
109class MediaRootS3Boto3Storage(S3Boto3Storage):
110 location = "media"
111 file_overwrite = False
112
113
114# endregion
115DEFAULT_FILE_STORAGE = "config.settings.production.MediaRootS3Boto3Storage"
116MEDIA_URL = f"https://{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com/media/"
117
118# TEMPLATES
119# ------------------------------------------------------------------------------
120# https://docs.djangoproject.com/en/dev/ref/settings/#templates
121TEMPLATES[0]["OPTIONS"]["loaders"] = [ # noqa F405
122 (
123 "django.template.loaders.cached.Loader",
124 [
125 "django.template.loaders.filesystem.Loader",
126 "django.template.loaders.app_directories.Loader",
127 ],
128 )
129]
130
131# EMAIL
132# ------------------------------------------------------------------------------
133# https://docs.djangoproject.com/en/dev/ref/settings/#default-from-email
134DEFAULT_FROM_EMAIL = env(
135 "DJANGO_DEFAULT_FROM_EMAIL", default="Pena <noreply@sahabatpena.co.id>"
136)
137# https://docs.djangoproject.com/en/dev/ref/settings/#server-email
138SERVER_EMAIL = env("DJANGO_SERVER_EMAIL", default=DEFAULT_FROM_EMAIL)
139# https://docs.djangoproject.com/en/dev/ref/settings/#email-subject-prefix
140EMAIL_SUBJECT_PREFIX = env(
141 "DJANGO_EMAIL_SUBJECT_PREFIX", default="[Pena]"
142)
143
144# ADMIN
145# ------------------------------------------------------------------------------
146# Django Admin URL regex.
147ADMIN_URL = env("DJANGO_ADMIN_URL")
148
149# Anymail (Mailgun)
150# ------------------------------------------------------------------------------
151# https://anymail.readthedocs.io/en/stable/installation/#installing-anymail
152INSTALLED_APPS += ["anymail"] # noqa F405
153EMAIL_BACKEND = "anymail.backends.mailgun.EmailBackend"
154# https://anymail.readthedocs.io/en/stable/installation/#anymail-settings-reference
155ANYMAIL = {
156 "MAILGUN_API_KEY": env("MAILGUN_API_KEY"),
157 "MAILGUN_SENDER_DOMAIN": env("MAILGUN_DOMAIN"),
158}
159
160# Gunicorn
161# ------------------------------------------------------------------------------
162INSTALLED_APPS += ["gunicorn"] # noqa F405
163
164# WhiteNoise
165# ------------------------------------------------------------------------------
166# http://whitenoise.evans.io/en/latest/django.html#enable-whitenoise
167MIDDLEWARE.insert(1, "whitenoise.middleware.WhiteNoiseMiddleware") # noqa F405
168
169
170# LOGGING
171# ------------------------------------------------------------------------------
172# https://docs.djangoproject.com/en/dev/ref/settings/#logging
173# See https://docs.djangoproject.com/en/dev/topics/logging for
174# more details on how to customize your logging configuration.
175
176LOGGING = {
177 "version": 1,
178 "disable_existing_loggers": True,
179 "formatters": {
180 "verbose": {
181 "format": "%(levelname)s %(asctime)s %(module)s "
182 "%(process)d %(thread)d %(message)s"
183 }
184 },
185 "handlers": {
186 "console": {
187 "level": "DEBUG",
188 "class": "logging.StreamHandler",
189 "formatter": "verbose",
190 }
191 },
192 "loggers": {
193 "django.db.backends": {
194 "level": "ERROR",
195 "handlers": ["console"],
196 "propagate": False,
197 },
198 # Errors logged by the SDK itself
199 "sentry_sdk": {"level": "ERROR", "handlers": ["console"], "propagate": False},
200 "django.security.DisallowedHost": {
201 "level": "ERROR",
202 "handlers": ["console"],
203 "propagate": False,
204 },
205 },
206}
207
208# Sentry
209# ------------------------------------------------------------------------------
210SENTRY_DSN = env("SENTRY_DSN")
211SENTRY_LOG_LEVEL = env.int("DJANGO_SENTRY_LOG_LEVEL", logging.INFO)
212
213sentry_logging = LoggingIntegration(
214 level=SENTRY_LOG_LEVEL, # Capture info and above as breadcrumbs
215 event_level=None, # Send no events from log messages
216)
217sentry_sdk.init(
218 dsn=SENTRY_DSN,
219 integrations=[sentry_logging, DjangoIntegration(), CeleryIntegration()],
220)
221
222# Your stuff...
223# ------------------------------------------------------------------------------