Skip to content

Commit 4d76d43

Browse files
committed
refactor: update the settings to make moving to production easier
1 parent dc0b3e1 commit 4d76d43

File tree

3 files changed

+87
-21
lines changed

3 files changed

+87
-21
lines changed

{{cookiecutter.project_slug}}/config/settings/base.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from typing import List
1515

1616
import environ
17+
# from celery.schedules import crontab
1718

1819
ROOT_DIR = environ.Path(__file__) - 3
1920
APPS_DIR = ROOT_DIR.path("{{cookiecutter.project_slug}}")
@@ -291,3 +292,40 @@
291292
},
292293
}
293294
}
295+
296+
# Celery
297+
# ------------------------------------------------------------------------------
298+
# if USE_TZ:
299+
# # http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-timezone
300+
# CELERY_TIMEZONE = TIME_ZONE
301+
# # http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-broker_url
302+
# CELERY_BROKER_URL = env("CELERY_BROKER_URL")
303+
# # http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-result_backend
304+
# CELERY_RESULT_BACKEND = CELERY_BROKER_URL
305+
# # http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-accept_content
306+
# CELERY_ACCEPT_CONTENT = ["json"]
307+
# # http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-task_serializer
308+
# CELERY_TASK_SERIALIZER = "json"
309+
# # http://docs.celeryproject.org/en/latest/userguide/configuration.html#std:setting-result_serializer
310+
# CELERY_RESULT_SERIALIZER = "json"
311+
# # http://docs.celeryproject.org/en/latest/userguide/configuration.html#task-time-limit
312+
# # TODO: set to whatever value is adequate in your circumstances
313+
# CELERY_TASK_TIME_LIMIT = 5 * 60
314+
# # http://docs.celeryproject.org/en/latest/userguide/configuration.html#task-soft-time-limit
315+
# # TODO: set to whatever value is adequate in your circumstances
316+
# CELERY_TASK_SOFT_TIME_LIMIT = 60
317+
# # https://docs.celeryproject.org/en/v4.4.7/userguide/configuration.html#task-default-queue
318+
# CELERY_DEFAULT_QUEUE = "cmcndola.org"
319+
320+
# # https://docs.celeryproject.org/en/v4.4.7/userguide/periodic-tasks.html#beat-entries
321+
# CELERY_BEAT_SCHEDULE = {
322+
# "get_daily_quote": {
323+
# "task": "task_fetch_daily_quote",
324+
# # Executes everyday at midnight (good for production)
325+
# "schedule": crontab(minute=0, hour=0),
326+
# # Executes every 2 hours (for development)
327+
# # "schedule": crontab(minute=0, hour="*/2"),
328+
# # Executes every 5 minutes (for development)
329+
# # "schedule": crontab(minute="*/5"),
330+
# },
331+
# }

{{cookiecutter.project_slug}}/config/settings/dev.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,6 @@
8888
"ATOMIC_REBUILD": True,
8989
},
9090
}
91+
92+
# http://docs.celeryproject.org/en/latest/userguide/configuration.html#task-eager-propagates
93+
# CELERY_TASK_EAGER_PROPAGATES = True

{{cookiecutter.project_slug}}/config/settings/production.py

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
"""See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/"""
1+
"""See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/"""
22

33
import logging
4+
from email.utils import formataddr, getaddresses
45

5-
from email.utils import getaddresses
6-
7-
from .base import * # noqa
86
import sentry_sdk
7+
# from sentry_sdk.integrations.celery import CeleryIntegration
98
from sentry_sdk.integrations.django import DjangoIntegration
109
from sentry_sdk.integrations.logging import LoggingIntegration
10+
from sentry_sdk.integrations.redis import RedisIntegration
11+
12+
from .base import * # noqa
1113

1214
DEBUG = False # just to make sure!
1315

@@ -53,16 +55,21 @@
5355
# "COMPRESSOR": "django_redis.compressors.zlib.ZlibCompressor",
5456
"COMPRESSOR": "django_redis.compressors.lz4.Lz4Compressor",
5557
},
56-
}
58+
},
59+
"renditions": {
60+
"BACKEND": "django.core.cache.backends.memcached.PyMemcacheCache",
61+
"LOCATION": env("MEMCACHED_URL"), # noqa F405
62+
"TIMEOUT": 600,
63+
"OPTIONS": {"MAX_ENTRIES": 1000},
64+
},
5765
}
5866
SESSION_ENGINE = "django.contrib.sessions.backends.cache"
5967
SESSION_CACHE_ALIAS = "default"
6068

6169
WAGTAILFRONTENDCACHE = {
6270
"cloudflare": {
6371
"BACKEND": "wagtail.contrib.frontend_cache.backends.CloudflareBackend",
64-
"EMAIL": env("CLOUDFLARE_EMAIL_ADDRESS"), # noqa F405
65-
"TOKEN": env("CLOUDFLARE_API_TOKEN"), # noqa F405
72+
"BEARER_TOKEN": env("CLOUDFLARE_BEARER_TOKEN"), # noqa F405
6673
"ZONEID": env("CLOUDFLARE_DOMAIN_ZONE_ID"), # noqa F405
6774
},
6875
}
@@ -75,26 +82,36 @@
7582
SESSION_COOKIE_SECURE = True
7683
CSRF_COOKIE_SECURE = True
7784

78-
# setup email backend
79-
EMAIL_BACKEND = "sendgrid_backend.SendgridBackend"
80-
SENDGRID_API_KEY = env("SENDGRID_API_KEY") # noqa F405
85+
# setup email backend via Anymail
86+
# ------------------------------------------------------------------------------
87+
# https://anymail.readthedocs.io/en/stable/installation/#installing-anymail
88+
INSTALLED_APPS += ["anymail"] # noqa F405
89+
# https://docs.djangoproject.com/en/dev/ref/settings/#email-backend
90+
# https://anymail.readthedocs.io/en/stable/installation/#anymail-settings-reference
91+
# https://anymail.readthedocs.io/en/stable/esps/sendgrid/
92+
EMAIL_BACKEND = "anymail.backends.sendgrid.EmailBackend"
93+
ANYMAIL = {
94+
"SENDGRID_API_KEY": env("SENDGRID_API_KEY"), # noqa F405
95+
"SENDGRID_GENERATE_MESSAGE_ID": env("SENDGRID_GENERATE_MESSAGE_ID"), # noqa F405
96+
"SENDGRID_MERGE_FIELD_FORMAT": env("SENDGRID_MERGE_FIELD_FORMAT"), # noqa F405
97+
"SENDGRID_API_URL": env( # noqa F405
98+
"SENDGRID_API_URL", default="https://api.sendgrid.com/v3/"
99+
),
100+
}
81101

82102
if len(getaddresses([env("EMAIL_RECIPIENTS")])) == 1: # noqa F405
83103
LIST_OF_EMAIL_RECIPIENTS.append( # noqa F405
84-
getaddresses([env("EMAIL_RECIPIENTS")])[0] # noqa F405
104+
formataddr(getaddresses([env("EMAIL_RECIPIENTS")])[0]) # noqa F405
85105
)
86106
else:
87-
LIST_OF_EMAIL_RECIPIENTS += getaddresses([env("EMAIL_RECIPIENTS")]) # noqa F405
107+
for email_address in getaddresses([env("EMAIL_RECIPIENTS")]): # noqa F405
108+
LIST_OF_EMAIL_RECIPIENTS += formataddr(email_address) # noqa F405
88109

89-
if len(getaddresses([env("DEFAULT_FROM_EMAIL")])) == 1: # noqa F405
90-
DEFAULT_FROM_EMAIL = getaddresses([env("DEFAULT_FROM_EMAIL")])[0] # noqa F405
91-
else:
92-
DEFAULT_FROM_EMAIL = getaddresses([env("DEFAULT_FROM_EMAIL")]) # noqa F405
110+
email_address = getaddresses([env("DEFAULT_FROM_EMAIL")])[0] # noqa F405
111+
DEFAULT_FROM_EMAIL = formataddr(email_address)
93112

94-
# try:
95-
# from .local import *
96-
# except ImportError:
97-
# pass
113+
# https://docs.djangoproject.com/en/dev/ref/settings/#server-email
114+
SERVER_EMAIL = env("DJANGO_SERVER_EMAIL", default=DEFAULT_FROM_EMAIL) # noqa F405
98115

99116
# enable SSL flag, if the data exchange needs to be secure.
100117
# Not needed for small apps
@@ -148,10 +165,18 @@
148165
level=SENTRY_LOG_LEVEL, # Capture info and above as breadcrumbs
149166
event_level=logging.ERROR, # Send errors as events
150167
)
151-
integrations = [sentry_logging, DjangoIntegration()]
168+
integrations = [
169+
sentry_logging,
170+
DjangoIntegration(),
171+
# CeleryIntegration(),
172+
RedisIntegration(),
173+
]
152174
sentry_sdk.init(
153175
dsn=SENTRY_DSN,
154176
integrations=integrations,
155177
environment=env("SENTRY_ENVIRONMENT", default="production"), # noqa F405
156178
traces_sample_rate=env.float("SENTRY_TRACES_SAMPLE_RATE", default=0.0), # noqa F405
179+
# If you wish to associate users to errors (assuming you are using
180+
# django.contrib.auth) you may enable sending PII data.
181+
send_default_pii=True,
157182
)

0 commit comments

Comments
 (0)