-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ICMSLST-2135 - Gov Notify #1149
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
boto3==1.21.29 | ||
celery[redis]==5.2.7 | ||
celery[redis]==5.3.1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Came across this issue during testing celery/billiard#377 Upgrading celery fixed the problem |
||
django-celery-results==2.4.0 | ||
django-chunk-upload-handlers==0.0.11 | ||
django-compressor==4.1 | ||
|
@@ -11,7 +11,6 @@ django-phonenumber-field==5.0.0 | |
django-ratelimit==3.0.1 | ||
django-redis==5.2.0 | ||
django-select2==7.10.1 | ||
django-ses==3.5.0 | ||
django-structlog==1.6.2 | ||
Django==4.1.10 | ||
elastic-apm==6.15.1 | ||
|
@@ -26,6 +25,7 @@ notifications-python-client==8.0.1 | |
openpyxl==3.0.7 | ||
oracledb==1.2.0 | ||
phonenumbers==8.12.12 | ||
pytz==2023.3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was a dependancy of celery 5.2.7 and not 5.3.1. Is used by |
||
psycogreen==1.0.2 | ||
psycopg2-binary==2.9.3 | ||
pydantic==1.10.2 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from uuid import UUID | ||
|
||
import structlog as logging | ||
from django.core.mail.backends.base import BaseEmailBackend | ||
|
||
from web.mail.api import send_email | ||
from web.mail.messages import GOVNotifyEmailMessage | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class GovNotifyEmailBackend(BaseEmailBackend): | ||
def send_messages(self, email_messages: list[GOVNotifyEmailMessage]) -> None: | ||
for message in email_messages: | ||
for recipient in message.recipients(): | ||
logger.info("Sending %s email to %s", message.name.label, 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) |
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 |
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=access_request, to=[recipient]) | ||
email.send() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from typing import ClassVar | ||
from uuid import UUID | ||
|
||
from django.conf import settings | ||
from django.core.mail import EmailMessage, SafeMIMEMultipart | ||
|
||
from web.models import AccessRequest | ||
|
||
from .constants import EmailTypes | ||
from .models import EmailTemplate | ||
|
||
|
||
class GOVNotifyEmailMessage(EmailMessage): | ||
name: ClassVar[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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't think it will be a problem but this will hit the db everytime any derived class is initialised. |
||
|
||
|
||
class AccessRequestEmail(GOVNotifyEmailMessage): | ||
name = EmailTypes.ACCESS_REQUEST | ||
|
||
def __init__(self, *args, access_request: AccessRequest, **kwargs): | ||
self.access_request = access_request | ||
super().__init__(*args, **kwargs) | ||
|
||
def get_context(self) -> dict: | ||
return {"reference": self.access_request.reference} |
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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding this takes care of logging (Ticket ICMSLST-2031). All tasks are logged in
TaskResult
object and this setting adds the task args and traceback information on failure. Is all viewable in django admin