Skip to content

Commit

Permalink
Merge pull request #132 from SexualHealthInnovations/multiple-report-…
Browse files Browse the repository at this point in the history
…recipients

allow reports to be sent to multiple recipients
  • Loading branch information
Kelsey Gilmore-Innis authored Jan 24, 2017
2 parents 2325784 + 16a1805 commit a60ba87
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
20 changes: 20 additions & 0 deletions callisto/delivery/migrations/0009_to_address_to_textfield.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-01-24 15:20
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('delivery', '0008_make_salt_nullable'),
]

operations = [
migrations.AlterField(
model_name='sentreport',
name='to_address',
field=models.CharField(max_length=4096),
),
]
2 changes: 1 addition & 1 deletion callisto/delivery/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ class SentReport(PolymorphicModel):
"""Report of one or more incidents, sent to the monitoring organization"""
# TODO: store link to s3 backup https://github.com/SexualHealthInnovations/callisto-core/issues/14
sent = models.DateTimeField(auto_now_add=True)
to_address = models.EmailField(blank=False, null=False, max_length=256)
to_address = models.CharField(blank=False, null=False, max_length=4096)

def _get_id_for_schools(self, is_match):
return "{0}-{1}-{2}".format(settings.SCHOOL_REPORT_PREFIX, '%05d' % self.id, 0 if is_match else 1)
Expand Down
4 changes: 2 additions & 2 deletions callisto/delivery/report_delivery.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@ def func(canvas, doc):
def send_email_to_coordinator(self, pdf_to_attach, notification_name, report_id):
notification = EmailNotification.objects.get(name=notification_name)

to = settings.COORDINATOR_EMAIL
to_addresses = [x.strip() for x in settings.COORDINATOR_EMAIL.split(',')]

email = EmailMultiAlternatives(notification.subject, notification.render_body_plain(), self.from_email, [to])
email = EmailMultiAlternatives(notification.subject, notification.render_body_plain(), self.from_email, to_addresses)
email.attach_alternative(notification.render_body(), "text/html")

gpg = gnupg.GPG()
Expand Down
25 changes: 25 additions & 0 deletions tests/callistocore/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,28 @@ def test_matches_after_encryption(self, mock_process):
find_matches([identifier])
# have to use ANY because objects in migration tests are faked
mock_process.assert_called_once_with([ANY, ANY], 'test_identifier', PDFMatchReport)


class MultipleRecipientMigrationTest(MigrationTest):

app_name = 'delivery'
before = '0008_make_salt_nullable'
after = '0009_to_address_to_textfield'

def test_recipient_data_is_migrated(self):

user = User.objects.create_user(username="dummy", password="dummy")
Report = self.get_model_before('Report')
report = Report(owner_id=user.pk)
report.save()
SentFullReport = self.get_model_before('SentFullReport')
sent_report = SentFullReport.objects.create(report_id=report.pk, to_address="[email protected]")
sent_report.save()
self.assertEqual(SentFullReport.objects.count(), 1)

self.run_migration()

SentFullReport = self.get_model_after('SentFullReport')
self.assertEqual(SentFullReport.objects.count(), 1)
sent_report = SentFullReport.objects.first()
self.assertEqual(sent_report.to_address, '[email protected]')
22 changes: 21 additions & 1 deletion tests/callistocore/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
from django.test.utils import override_settings

from callisto.delivery.forms import NewSecretKeyForm, SecretKeyForm
from callisto.delivery.models import EmailNotification, MatchReport, Report
from callisto.delivery.models import (
EmailNotification, MatchReport, Report, SentFullReport,
)
from callisto.evaluation.models import EvalRow

from .forms import EncryptedFormWizard
Expand Down Expand Up @@ -475,6 +477,24 @@ def test_submit_sends_report(self):
self.assertEqual(message.to, ['[email protected]'])
self.assertRegexpMatches(message.attachments[0][0], 'report_.*\\.pdf\\.gpg')

@override_settings(COORDINATOR_EMAIL='[email protected],[email protected]')
def test_submit_sends_report_to_multiple_coordinators(self):
response = self.client.post((self.submission_url % self.report.pk),
data={'name': 'test submitter',
'email': '[email protected]',
'phone_number': '555-555-1212',
'email_confirmation': "False",
'key': self.report_key})
self.assertEqual(response.status_code, 200)
self.assertNotIn('submit_error', response.context)
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(SentFullReport.objects.count(), 1)
message1 = mail.outbox[0]
self.assertEqual(message1.subject, 'test delivery')
self.assertIn('"Reports" <reports', message1.from_email)
self.assertEqual(message1.to, ['[email protected]', '[email protected]'])
self.assertRegexpMatches(message1.attachments[0][0], 'report_.*\\.pdf\\.gpg')

def test_submit_sends_email_confirmation(self):
response = self.client.post((self.submission_url % self.report.pk),
data={'name': 'test submitter',
Expand Down

0 comments on commit a60ba87

Please sign in to comment.