-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
283 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,3 +114,4 @@ | |
|
||
# django-ratelimit | ||
RATELIMIT_ENABLE = False | ||
CELERY_TASK_ALWAYS_EAGER = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,7 @@ | |
from .base import * | ||
|
||
# Email settings for all non prod environments. | ||
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" | ||
AWS_SES_ACCESS_KEY_ID = "" | ||
AWS_SES_SECRET_ACCESS_KEY = "" | ||
AWS_SES_REGION_NAME = "" | ||
AWS_SES_REGION_ENDPOINT = "" | ||
SEND_ALL_EMAILS_TO = env.list("SEND_ALL_EMAILS_TO", default=[]) | ||
|
||
# Email/phone contacts | ||
EMAIL_FROM = env.str("ICMS_EMAIL_FROM", "[email protected]") # /PS-IGNORE | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from uuid import UUID | ||
|
||
import structlog as logging | ||
from django.core.mail.backends.base import BaseEmailBackend | ||
|
||
from web.mail.api import send_email | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class GovNotifyEmailBackend(BaseEmailBackend): | ||
def send_messages(self, email_messages: list) -> None: | ||
for message in email_messages: | ||
for recipient in message.recipients(): | ||
logger.info(f"Sending {message.name.label} email to {recipient}") | ||
self.send_message(message.template_id, message.personalisation, recipient) | ||
|
||
def send_message(self, template_id: UUID, personalisation: dict, recipient: str) -> None: | ||
send_email.delay(template_id, personalisation, recipient) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from functools import wraps | ||
|
||
from django.conf import settings | ||
|
||
|
||
def override_recipients(f): | ||
"""Helper decorator to override the email addresses returned by the wrapped function. | ||
If APP_ENV is dev or local and SEND_ALL_EMAILS_TO is set in django settings all emails will be sent to | ||
the specified email addresses. | ||
""" | ||
|
||
@wraps(f) | ||
def wrapper(*args, **kwargs): | ||
if settings.APP_ENV in ("local", "dev") and settings.SEND_ALL_EMAILS_TO: | ||
return settings.SEND_ALL_EMAILS_TO | ||
return f(*args, **kwargs) | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from web.models import AccessRequest | ||
|
||
from .messages import AccessRequestEmail | ||
from .recipients import get_ilb_case_officers_email_addresses | ||
|
||
|
||
def send_access_requested_email(access_request: AccessRequest) -> None: | ||
recipients = get_ilb_case_officers_email_addresses() | ||
for recipient in recipients: | ||
email = AccessRequestEmail(access_request, to=[recipient]) | ||
email.send() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from uuid import UUID | ||
|
||
from django.conf import settings | ||
from django.core.mail import EmailMessage, SafeMIMEMultipart | ||
|
||
from .constants import EmailTypes | ||
from .models import EmailTemplate | ||
|
||
|
||
class GOVNotifyEmailMessage(EmailMessage): | ||
name = None | EmailTypes | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.template_id = self.get_template_id() | ||
self.personalisation = self.get_personalisation() | ||
|
||
def message(self) -> SafeMIMEMultipart: | ||
"""Adds the personalisation data to the message header, so it is visible when using the console backend.""" | ||
message = super().message() | ||
message["Personalisation"] = self.personalisation | ||
return message | ||
|
||
def get_context(self) -> dict: | ||
raise NotImplementedError | ||
|
||
def get_personalisation(self) -> dict: | ||
return { | ||
"icms_url": settings.DEFAULT_DOMAIN, | ||
"icms_contact_email": settings.ILB_CONTACT_EMAIL, | ||
"icms_contact_phone": settings.ILB_CONTACT_PHONE, | ||
"subject": self.subject, | ||
"body": self.body, | ||
} | self.get_context() | ||
|
||
def get_template_id(self) -> UUID: | ||
return EmailTemplate.objects.get(name=self.name).gov_notify_template_id | ||
|
||
|
||
class AccessRequestEmail(GOVNotifyEmailMessage): | ||
name = EmailTypes.ACCESS_REQUEST | ||
|
||
def __init__(self, access_request, *args, **kwargs): | ||
self.access_request = access_request | ||
super().__init__(*args, **kwargs) | ||
|
||
def get_context(self) -> dict: | ||
return {"reference": self.access_request.reference} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from django.db.models import QuerySet | ||
|
||
from web.mail.decorators import override_recipients | ||
from web.models import User | ||
from web.notify.utils import get_notification_emails | ||
from web.permissions import get_ilb_case_officers | ||
|
||
|
||
def get_ilb_case_officers_email_addresses() -> list[str]: | ||
users = get_ilb_case_officers() | ||
return get_email_addresses_for_users(users) | ||
|
||
|
||
@override_recipients | ||
def get_email_addresses_for_users(users: QuerySet[User]) -> list[str]: | ||
emails = [] | ||
for user in users: | ||
emails.extend(get_notification_emails(user)) | ||
return list(set(emails)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.