From 8ba84e6ffced17facc80d068bc24cdf2a5fd3444 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Mon, 30 Jul 2018 09:25:22 +0100
Subject: [PATCH] Add the ready_to_review messages for email

---
 opentech/apply/activity/messaging.py          | 43 ++++++++++++++-----
 .../messages/email/applicant_base.html        |  9 ++++
 .../templates/messages/email/base.html        |  6 +--
 .../templates/messages/email/comment.html     |  2 +-
 .../messages/email/determination.html         |  2 +-
 .../messages/email/invited_to_proposal.html   |  2 +-
 .../messages/email/ready_to_review.html       |  9 ++++
 .../templates/messages/email/transition.html  |  2 +-
 .../apply/activity/tests/test_messaging.py    | 11 ++++-
 .../apply/funds/tests/factories/models.py     |  5 +++
 10 files changed, 72 insertions(+), 19 deletions(-)
 create mode 100644 opentech/apply/activity/templates/messages/email/applicant_base.html
 create mode 100644 opentech/apply/activity/templates/messages/email/ready_to_review.html

diff --git a/opentech/apply/activity/messaging.py b/opentech/apply/activity/messaging.py
index 8cf1d1bd5..b17d599d5 100644
--- a/opentech/apply/activity/messaging.py
+++ b/opentech/apply/activity/messaging.py
@@ -52,6 +52,7 @@ class AdapterBase:
 
     def process(self, message_type, **kwargs):
         message = self.message(message_type, **kwargs)
+        kwargs.update(self.extra_kwargs(message_type, **kwargs))
 
         if not message:
             return
@@ -122,17 +123,22 @@ class SlackAdapter(AdapterBase):
         self.target_room = settings.SLACK_DESTINATION_ROOM
 
     def message(self, message_type, **kwargs):
-        submission = kwargs['submission']
-        request = kwargs['request']
-        link = link_to(submission, request)
+        message = super().message(message_type, **kwargs)
 
-        message = super().message(message_type, link=link, **kwargs)
+        user = kwargs['user']
+        submission = kwargs['submission']
 
         slack_target = self.slack_id(submission.lead)
 
         message = ' '.join([slack_target, message]).strip()
         return message
 
+    def extra_kwargs(self, message_type, **kwargs):
+        submission = kwargs['submission']
+        request = kwargs['request']
+        link = link_to(submission, request)
+        return {'link': link}
+
     def notify_reviewers(self, submission, **kwargs):
         reviewers_to_notify = []
         for reviewer in submission.reviewers.all():
@@ -175,6 +181,10 @@ class EmailAdapter(AdapterBase):
         MESSAGES.TRANSITION: 'messages/email/transition.html',
         MESSAGES.DETERMINATION_OUTCOME: 'messages/email/determination.html',
         MESSAGES.INVITED_TO_PROPOSAL: 'messages/email/invited_to_proposal.html',
+        MESSAGES.READY_FOR_REVIEW: 'messages/email/ready_to_review.html',
+    }
+    recipients = {
+        MESSAGES.READY_FOR_REVIEW: 'get_reviewers',
     }
 
     def notify_comment(self, **kwargs):
@@ -182,17 +192,30 @@ class EmailAdapter(AdapterBase):
         if not comment.private:
             return self.render_message('messages/email/comment.html', **kwargs)
 
+    def get_recipients(self, submission):
+        try:
+            return getattr(self, self.recipients[''])
+
+    def get_reviewers(self, )
+        return [
+            [reviewer.email]
+            for reviewer in submission.reviewers.all()
+            if submission.phase.permissions.can_review(reviewer)
+        ]
+
     def render_message(self, template, **kwargs):
         return render_to_string(template, kwargs)
 
     def send_message(self, message, submission, **kwargs):
         subject = submission.page.specific.subject or 'Your application to Open Technology Fund: {submission.title}'.format(submission=submission)
-        send_mail(
-            subject,
-            message,
-            submission.page.specific.from_address,
-            (submission.user.email,),
-        )
+        recipients = self.get_recipients(submission) or [(submission.user.email,)]
+        for recipient in recipients:
+            send_mail(
+                subject,
+                message,
+                submission.page.specific.from_address,
+                recipient,
+            )
 
 
 class MessengerBackend:
diff --git a/opentech/apply/activity/templates/messages/email/applicant_base.html b/opentech/apply/activity/templates/messages/email/applicant_base.html
new file mode 100644
index 000000000..4405cab7b
--- /dev/null
+++ b/opentech/apply/activity/templates/messages/email/applicant_base.html
@@ -0,0 +1,9 @@
+{% block salutation %}
+Dear {{ submission.user.get_full_name|default:"applicant" }},
+{% endblock %}
+
+{% block more_info %}
+You can access your application here: {{ request.scheme }}://{{ request.get_host }}{{ submission.get_absolute_url }}
+
+If you have any questions, please leave a message on your application or email us at info@opentechfund.org.
+{% endblock %}
diff --git a/opentech/apply/activity/templates/messages/email/base.html b/opentech/apply/activity/templates/messages/email/base.html
index 99ea0b562..80cc2fbbe 100644
--- a/opentech/apply/activity/templates/messages/email/base.html
+++ b/opentech/apply/activity/templates/messages/email/base.html
@@ -1,10 +1,8 @@
-Dear {{ submission.user.get_full_name|default:"applicant" }},
+{% block salutation %}Dear {{ user }}{% endblock %}
 
 {% block content %}{% endblock %}
 
-You can access your application here: {{ request.scheme }}://{{ request.get_host }}{{ submission.get_absolute_url }}
-
-If you have any questions, please leave a message on your application or email us at info@opentechfund.org.
+{% block more_info %}{% endblock %}
 
 Kind Regards,
 The OTF Team
diff --git a/opentech/apply/activity/templates/messages/email/comment.html b/opentech/apply/activity/templates/messages/email/comment.html
index c5714b9a5..845e7fe75 100644
--- a/opentech/apply/activity/templates/messages/email/comment.html
+++ b/opentech/apply/activity/templates/messages/email/comment.html
@@ -1,4 +1,4 @@
-{% extends "messages/email/base.html" %}
+{% extends "messages/email/applicant_base.html" %}
 
 {% block content %}
 There has been a new comment on your application: {{ submission.title }}
diff --git a/opentech/apply/activity/templates/messages/email/determination.html b/opentech/apply/activity/templates/messages/email/determination.html
index b4bee91fe..b51f4dcb3 100644
--- a/opentech/apply/activity/templates/messages/email/determination.html
+++ b/opentech/apply/activity/templates/messages/email/determination.html
@@ -1,4 +1,4 @@
-{% extends "messages/email/base.html" %}
+{% extends "messages/email/applicant_base.html" %}
 {% load bleach_tags %}
 
 {% block content %}
diff --git a/opentech/apply/activity/templates/messages/email/invited_to_proposal.html b/opentech/apply/activity/templates/messages/email/invited_to_proposal.html
index f84077881..d77afc516 100644
--- a/opentech/apply/activity/templates/messages/email/invited_to_proposal.html
+++ b/opentech/apply/activity/templates/messages/email/invited_to_proposal.html
@@ -1,4 +1,4 @@
-{% extends "messages/email/base.html" %}
+{% extends "messages/email/applicant_base.html" %}
 
 {% block content %}
 We’ve reviewed your Concept Note and think it could be a good fit for OTF funding. We would like to invite you to submit a Proposal with more details about your project.
diff --git a/opentech/apply/activity/templates/messages/email/ready_to_review.html b/opentech/apply/activity/templates/messages/email/ready_to_review.html
new file mode 100644
index 000000000..bef3718e3
--- /dev/null
+++ b/opentech/apply/activity/templates/messages/email/ready_to_review.html
@@ -0,0 +1,9 @@
+{% extends "messages/email/base.html" %}
+{% block salutation %}Dear Reviewer{% endblock %}
+
+{% block content %}
+A new proposal has been added to your review list.
+
+Title: {submission.title}
+Link: {{ request.scheme }}://{{ request.get_host }}{{ submission.get_absolute_url }}
+{% endblock %}
diff --git a/opentech/apply/activity/templates/messages/email/transition.html b/opentech/apply/activity/templates/messages/email/transition.html
index b11b6919c..2318ef7ea 100644
--- a/opentech/apply/activity/templates/messages/email/transition.html
+++ b/opentech/apply/activity/templates/messages/email/transition.html
@@ -1,4 +1,4 @@
-{% extends "messages/email/base.html" %}
+{% extends "messages/email/applicant_base.html" %}
 
 {% block content %}
 Your application has been progressed from {{ old_phase.display_name }} to {{ submission.phase }}.
diff --git a/opentech/apply/activity/tests/test_messaging.py b/opentech/apply/activity/tests/test_messaging.py
index 48a626047..a27299f51 100644
--- a/opentech/apply/activity/tests/test_messaging.py
+++ b/opentech/apply/activity/tests/test_messaging.py
@@ -9,7 +9,7 @@ from django.contrib.messages import get_messages
 
 from opentech.apply.utils.testing import make_request
 from opentech.apply.funds.tests.factories import ApplicationSubmissionFactory
-from opentech.apply.users.tests.factories import UserFactory
+from opentech.apply.users.tests.factories import UserFactory, ReviewerFactory
 
 from ..models import Activity
 from ..messaging import (
@@ -209,3 +209,12 @@ class TestEmailAdapter(TestCase):
         adapter.process(MESSAGES.NEW_SUBMISSION, submission=submission)
 
         self.assertEqual(len(mail.outbox), 1)
+        self.assertEqual(mail.outbox[0].to, [submission.user.email])
+
+    def test_reviewers_email(self):
+        adapter = EmailAdapter()
+        reviewers = ReviewerFactory.create_batch(4)
+        submission = ApplicationSubmissionFactory(status='external_review', reviewers=reviewers, workflow_stages=2)
+        adapter.process(MESSAGES.READY_FOR_REVIEW, submission=submission)
+
+        self.assertEqual(len(mail.outbox), 4)
diff --git a/opentech/apply/funds/tests/factories/models.py b/opentech/apply/funds/tests/factories/models.py
index 98ca5a773..a305c94ab 100644
--- a/opentech/apply/funds/tests/factories/models.py
+++ b/opentech/apply/funds/tests/factories/models.py
@@ -200,6 +200,11 @@ class ApplicationSubmissionFactory(factory.DjangoModelFactory):
     live_revision = None
     draft_revision = None
 
+    @factory.post_generation
+    def reviewers(self, create, reviewers, **kwargs):
+        if create and reviewers:
+            self.reviewers.set(reviewers)
+
     @classmethod
     def _generate(cls, strat, params):
         params.update(**build_form(params))
-- 
GitLab