Skip to content

Commit 5bc14d3

Browse files
committed
dotdigital settings
1 parent 63adc8b commit 5bc14d3

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

codeforlife/mail.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
Dotdigital helpers.
66
"""
77

8-
import os
8+
import json
9+
import logging
910
import typing as t
1011
from dataclasses import dataclass
1112

1213
import requests
14+
from django.conf import settings
1315

1416
from .types import JsonDict
1517

@@ -72,7 +74,7 @@ def add_contact(
7274
# pylint: enable=line-too-long
7375

7476
if auth is None:
75-
auth = os.environ["DOTDIGITAL_AUTH"]
77+
auth = settings.DOTDIGITAL_AUTH
7678

7779
contact: JsonDict = {"email": email.lower()}
7880
if opt_in_type is not None:
@@ -123,6 +125,12 @@ def add_contact(
123125
for preference in preferences
124126
]
125127

128+
if not settings.DOTDIGITAL_ENABLED:
129+
logging.info(
130+
"Added contact to DotDigital:\n%s", json.dumps(body, indent=2)
131+
)
132+
return
133+
126134
response = requests.post(
127135
# pylint: disable-next=line-too-long
128136
url=f"https://{region}-api.dotdigital.com/v2/contacts/with-consent-and-preferences",
@@ -166,8 +174,12 @@ def remove_contact(
166174
"""
167175
# pylint: enable=line-too-long
168176

177+
if not settings.DOTDIGITAL_ENABLED:
178+
logging.info("Removed contact from DotDigital: %s", contact_identifier)
179+
return
180+
169181
if auth is None:
170-
auth = os.environ["DOTDIGITAL_AUTH"]
182+
auth = settings.DOTDIGITAL_AUTH
171183

172184
response = requests.get(
173185
# pylint: disable-next=line-too-long
@@ -250,7 +262,7 @@ def send_mail(
250262
# pylint: enable=line-too-long
251263

252264
if auth is None:
253-
auth = os.environ["DOTDIGITAL_AUTH"]
265+
auth = settings.DOTDIGITAL_AUTH
254266

255267
body = {
256268
"campaignId": campaign_id,
@@ -282,6 +294,13 @@ def send_mail(
282294
for attachment in attachments
283295
]
284296

297+
if not settings.DOTDIGITAL_ENABLED:
298+
logging.info(
299+
"Sent a triggered email with DotDigital:\n%s",
300+
json.dumps(body, indent=2),
301+
)
302+
return
303+
285304
response = requests.post(
286305
url=f"https://{region}-api.dotdigital.com/v2/email/triggered-campaign",
287306
json=body,

codeforlife/settings/custom.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,10 @@
2222

2323
# The api url of the current service.
2424
SERVICE_API_URL = f"{SERVICE_BASE_URL}/api"
25+
26+
# The authorization bearer token used to authenticate with Dotdigital.
27+
DOTDIGITAL_AUTH = os.getenv("DOTDIGITAL_AUTH", "REPLACE_ME")
28+
29+
# A global flag used to enabled/disable calls to Dotdigital's API.
30+
# If disabled, API calls will be logged to the console instead.
31+
DOTDIGITAL_ENABLED = bool(int(os.getenv("DOTDIGITAL_ENABLED", "0")))

0 commit comments

Comments
 (0)