Skip to content
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

Validator update emails #201

Merged
merged 3 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add ability to resubmit boundaries [#185](https://github.com/azavea/iow-boundary-tool/pull/185)
- Add support for approving/unapproving boundaries [#186](https://github.com/azavea/iow-boundary-tool/pull/186)
- Add S3 Permissions to ECS Tasks [#199](https://github.com/azavea/iow-boundary-tool/pull/199)
- Add submit boundary validator email [#201](https://github.com/azavea/iow-boundary-tool/pull/201)

### Changed

Expand Down
54 changes: 54 additions & 0 deletions src/django/api/mail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import logging

from django.conf import settings
from django.core.mail import send_mail
from django.template.loader import get_template

from .models.user import Roles

logger = logging.getLogger(__name__)


def send_boundary_submitted_validator_email(request, updated_boundary):
subj_template = get_template('mail/boundary_submitted_validator_subject.txt')
body_template = get_template('mail/boundary_submitted_validator_body.txt')

validators = updated_boundary.utility.state.users.filter(role=Roles.VALIDATOR)
validator_emails = [user.email for user in validators.only('email')]

template_data = {
"city": updated_boundary.utility.address_city,
"pwsid": updated_boundary.utility.pwsid,
"details_link": "{}/submissions/{}/".format(
make_iow_url(request),
updated_boundary.id,
),
}

subject = subj_template.render(template_data)
body = body_template.render(template_data)

for email in validator_emails:
try:
send_mail(
subject,
body,
from_email=None,
recipient_list=[email],
)
except Exception as exception:
logger.error(
'Could not send validator update email to %s. Caught exception:\n%s',
(email, exception),
)


def make_iow_url(request):
if settings.ENVIRONMENT == 'Development':
protocol = 'http'
host = 'localhost:4545'
else:
protocol = 'https'
host = request.get_host()

return f'{protocol}://{host}'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Submission for review - {{city}} {{pwsid}}

A new boundary has been submitted for review.

Review submission:
{{ details_link }}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Submission for review - {{city}} {{pwsid}}
5 changes: 4 additions & 1 deletion src/django/api/views/boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from rest_framework.views import APIView

from ..exceptions import BadRequestException
from ..mail import send_boundary_submitted_validator_email
from ..models import ReferenceImage, Roles, Submission
from ..models.boundary import BOUNDARY_STATUS, Boundary
from ..models.submission import Approval
Expand Down Expand Up @@ -136,7 +137,7 @@ class BoundarySubmitView(APIView):

def patch(self, request, id, format=None):
boundary_set = get_boundary_queryset_for_user(request.user)
boundary_set = boundary_set.prefetch_related("submissions")
boundary_set = boundary_set.select_related('utility__state')
boundary = get_object_or_404(boundary_set, pk=id)
if boundary.status != BOUNDARY_STATUS.DRAFT:
raise BadRequestException(
Expand All @@ -156,6 +157,8 @@ def patch(self, request, id, format=None):
boundary.latest_submission.submitted_at = now
boundary.latest_submission.save()

send_boundary_submitted_validator_email(request, boundary)

return Response(status=HTTP_204_NO_CONTENT)


Expand Down