mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-25 12:08:40 +02:00
linter errors
This commit is contained in:
parent
ab4024bab5
commit
43b48edf36
5 changed files with 78 additions and 82 deletions
|
@ -66,13 +66,13 @@ class TestEmails(TestCase):
|
|||
"""Test sending email with cc works"""
|
||||
with boto3_mocking.clients.handler_for("sesv2", self.mock_client_class):
|
||||
send_templated_email(
|
||||
"test content",
|
||||
"test subject",
|
||||
"doesnotexist@igorville.com",
|
||||
context={"domain_request": self},
|
||||
bcc_address=None,
|
||||
cc_addresses=["test_email1@example.com", "test_email2@example.com"]
|
||||
)
|
||||
"test content",
|
||||
"test subject",
|
||||
"doesnotexist@igorville.com",
|
||||
context={"domain_request": self},
|
||||
bcc_address=None,
|
||||
cc_addresses=["test_email1@example.com", "test_email2@example.com"],
|
||||
)
|
||||
|
||||
# check that an email was sent
|
||||
self.assertTrue(self.mock_client.send_email.called)
|
||||
|
|
|
@ -2,17 +2,14 @@ from unittest import skip
|
|||
from unittest.mock import MagicMock, ANY, patch
|
||||
|
||||
from django.conf import settings
|
||||
from django.test import override_settings
|
||||
from django.urls import reverse
|
||||
from django.contrib.auth import get_user_model
|
||||
from waffle.testutils import override_flag
|
||||
from api.tests.common import less_console_noise_decorator
|
||||
from registrar.models.utility.portfolio_helper import UserPortfolioRoleChoices
|
||||
from registrar.utility.email import send_templated_email
|
||||
from .common import MockEppLib, MockSESClient, create_user # type: ignore
|
||||
from django_webtest import WebTest # type: ignore
|
||||
import boto3_mocking # type: ignore
|
||||
from django.middleware.csrf import get_token
|
||||
|
||||
from registrar.utility.errors import (
|
||||
NameserverError,
|
||||
|
@ -106,7 +103,6 @@ class TestWithDomainPermissions(TestWithUser):
|
|||
DomainInformation.objects.get_or_create(creator=self.user, domain=self.domain_deleted)
|
||||
DomainInformation.objects.get_or_create(creator=self.user, domain=self.domain_dns_needed)
|
||||
|
||||
|
||||
self.role, _ = UserDomainRole.objects.get_or_create(
|
||||
user=self.user, domain=self.domain, role=UserDomainRole.Roles.MANAGER
|
||||
)
|
||||
|
@ -2196,7 +2192,7 @@ class TestDomainChangeNotifications(TestDomainOverview):
|
|||
self.domain_information.senior_official = Contact.objects.create(
|
||||
first_name="Old", last_name="Official", title="Manager", email="old_official@example.com"
|
||||
)
|
||||
portfolio, _ =Portfolio.objects.get_or_create(
|
||||
portfolio, _ = Portfolio.objects.get_or_create(
|
||||
organization_name="portfolio",
|
||||
creator=self.user,
|
||||
)
|
||||
|
|
|
@ -22,15 +22,15 @@ class EmailSendingError(RuntimeError):
|
|||
pass
|
||||
|
||||
|
||||
def send_templated_email(
|
||||
def send_templated_email( # noqa
|
||||
template_name: str,
|
||||
subject_template_name: str,
|
||||
to_address: str="",
|
||||
bcc_address: str="",
|
||||
to_address: str = "",
|
||||
bcc_address: str = "",
|
||||
context={},
|
||||
attachment_file=None,
|
||||
wrap_email=False,
|
||||
cc_addresses: list[str]=[],
|
||||
cc_addresses: list[str] = [],
|
||||
):
|
||||
"""Send an email built from a template.
|
||||
|
||||
|
@ -58,7 +58,6 @@ def send_templated_email(
|
|||
if len(sendable_cc_addresses) < len(cc_addresses):
|
||||
logger.warning("Some CC'ed addresses were removed: %s.", blocked_cc_addresses)
|
||||
|
||||
|
||||
template = get_template(template_name)
|
||||
email_body = template.render(context=context)
|
||||
|
||||
|
@ -127,6 +126,7 @@ def send_templated_email(
|
|||
except Exception as exc:
|
||||
raise EmailSendingError("Could not send SES email.") from exc
|
||||
|
||||
|
||||
def _can_send_email(to_address, bcc_address):
|
||||
"""Raises an EmailSendingError if we cannot send an email. Does nothing otherwise."""
|
||||
|
||||
|
@ -144,6 +144,7 @@ def _can_send_email(to_address, bcc_address):
|
|||
if bcc_address and not AllowedEmail.is_allowed_email(bcc_address):
|
||||
raise EmailSendingError(message.format(bcc_address))
|
||||
|
||||
|
||||
def get_sendable_addresses(addresses: list[str]) -> tuple[list[str], list[str]]:
|
||||
"""Checks whether a list of addresses can be sent to.
|
||||
|
||||
|
|
|
@ -176,29 +176,35 @@ class DomainFormBaseView(DomainBaseView, FormMixin):
|
|||
|
||||
if form.__class__ in form_label_dict:
|
||||
# these types of forms can cause notifications
|
||||
should_notify=True
|
||||
should_notify = True
|
||||
if form.__class__ in check_for_portfolio:
|
||||
# some forms shouldn't cause notifications if they are in a portfolio
|
||||
info = self.get_domain_info_from_domain()
|
||||
if not info or info.portfolio:
|
||||
logger.info(f"Not notifying because of portfolio")
|
||||
should_notify = False
|
||||
else:
|
||||
# don't notify for any other types of forms
|
||||
should_notify=False
|
||||
should_notify = False
|
||||
logger.info(f"Not notifying for {form.__class__}")
|
||||
if (should_notify and form.has_changed()) or force_send:
|
||||
context={
|
||||
"domain": self.object.name,
|
||||
"user": self.request.user,
|
||||
"date": date.today(),
|
||||
"changes": form_label_dict[form.__class__]
|
||||
}
|
||||
self.email_domain_managers(self.object, "emails/update_to_approved_domain.txt", "emails/update_to_approved_domain_subject.txt", context)
|
||||
context = {
|
||||
"domain": self.object.name,
|
||||
"user": self.request.user,
|
||||
"date": date.today(),
|
||||
"changes": form_label_dict[form.__class__],
|
||||
}
|
||||
self.email_domain_managers(
|
||||
self.object,
|
||||
"emails/update_to_approved_domain.txt",
|
||||
"emails/update_to_approved_domain_subject.txt",
|
||||
context,
|
||||
)
|
||||
else:
|
||||
logger.info(f"Not notifying for {form.__class__}, form changes: {form.has_changed()}, force_send: {force_send}")
|
||||
logger.info(
|
||||
f"Not notifying for {form.__class__}, form changes: {form.has_changed()}, force_send: {force_send}"
|
||||
)
|
||||
|
||||
def email_domain_managers(self, domain_name, template: str, subject_template: str, context: any = {}):
|
||||
def email_domain_managers(self, domain_name, template: str, subject_template: str, context={}):
|
||||
"""Send a single email built from a template to all managers for a given domain.
|
||||
|
||||
template_name and subject_template_name are relative to the same template
|
||||
|
@ -214,20 +220,16 @@ class DomainFormBaseView(DomainBaseView, FormMixin):
|
|||
domain = Domain.objects.get(name=domain_name)
|
||||
except Domain.DoesNotExist:
|
||||
logger.warning(
|
||||
"Could not send notification email for domain %s, unable to find matching domain object",
|
||||
domain_name
|
||||
"Could not send notification email for domain %s, unable to find matching domain object", domain_name
|
||||
)
|
||||
manager_pks = UserDomainRole.objects.filter(domain=domain.pk, role=UserDomainRole.Roles.MANAGER).values_list("user", flat=True)
|
||||
manager_pks = UserDomainRole.objects.filter(domain=domain.pk, role=UserDomainRole.Roles.MANAGER).values_list(
|
||||
"user", flat=True
|
||||
)
|
||||
emails = list(User.objects.filter(pk__in=manager_pks).values_list("email", flat=True))
|
||||
logger.debug("attempting to send templated email to domain managers")
|
||||
try:
|
||||
send_templated_email(
|
||||
template,
|
||||
subject_template,
|
||||
context=context,
|
||||
cc_addresses=emails
|
||||
)
|
||||
except EmailSendingError as exc:
|
||||
send_templated_email(template, subject_template, context=context, cc_addresses=emails)
|
||||
except EmailSendingError:
|
||||
logger.warning(
|
||||
"Could not sent notification email to %s for domain %s",
|
||||
emails,
|
||||
|
@ -492,8 +494,6 @@ class DomainNameserversView(DomainFormBaseView):
|
|||
|
||||
This post method harmonizes using DomainBaseView and FormMixin
|
||||
"""
|
||||
logger.info(f"POST request to DomainNameserversView")
|
||||
|
||||
self._get_domain(request)
|
||||
formset = self.get_form()
|
||||
|
||||
|
@ -502,7 +502,6 @@ class DomainNameserversView(DomainFormBaseView):
|
|||
return HttpResponseRedirect(url)
|
||||
|
||||
if formset.is_valid():
|
||||
logger.info(f"Formset is valid")
|
||||
return self.form_valid(formset)
|
||||
else:
|
||||
return self.form_invalid(formset)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue