Skip to content

Commit

Permalink
Merge 9f8e0a5 into 007ef12
Browse files Browse the repository at this point in the history
  • Loading branch information
eheinrich committed Apr 1, 2020
2 parents 007ef12 + 9f8e0a5 commit ed783ac
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
6 changes: 4 additions & 2 deletions observation_portal/proposals/tasks.py
Expand Up @@ -12,7 +12,9 @@ def time_allocation_reminder():
for proposal in Proposal.current_proposals().filter(active=True).distinct():
# Only send an email if we are within 3 months of the end of the semester
# and the proposal has at least one allocation.
if (proposal.current_semester.end - timezone.now()).days <= 93 and \
len(proposal.current_allocation) > 0:
if (
len(proposal.current_allocation) > 0
and (proposal.current_semester.end - timezone.now()).days <= 93
):
logger.info('Sending time allocation reminder for {}'.format(proposal))
proposal.send_time_allocation_reminder()
49 changes: 49 additions & 0 deletions observation_portal/proposals/tests.py
Expand Up @@ -13,6 +13,7 @@
from observation_portal.requestgroups.models import RequestGroup, Configuration, InstrumentConfig
from observation_portal.accounts.models import Profile
from observation_portal.common.test_helpers import create_simple_requestgroup
from observation_portal.proposals.tasks import time_allocation_reminder
from observation_portal.requestgroups.signals import handlers # DO NOT DELETE, needed to active signals


Expand Down Expand Up @@ -159,6 +160,54 @@ def test_no_notifications(self):
self.assertEqual(len(mail.outbox), 0)


class TestTimeAllocationEmail(DramatiqTestCase):
def setUp(self):
super().setUp()
self.pi = mixer.blend(User)
self.coi = mixer.blend(User)
self.proposal = mixer.blend(Proposal, active=True)
mixer.blend(Membership, user=self.pi, proposal=self.proposal, role='PI')
mixer.blend(Membership, user=self.coi, proposal=self.proposal, role='CI')
now = timezone.now()
self.current_semester = mixer.blend(
Semester, start=now, end=now + datetime.timedelta(days=30))

self.future_semester = mixer.blend(
Semester, start=now + datetime.timedelta(days=30), end=now + datetime.timedelta(days=60)
)

def test_sends_email_to_pi_for_current_active_proposal(self):
mixer.blend(TimeAllocation, proposal=self.proposal, semester=self.current_semester)
time_allocation_reminder()

self.broker.join('default')
self.worker.join()

self.assertEqual(len(mail.outbox), 1)
self.assertIn(self.proposal.id, str(mail.outbox[0].message()))
self.assertEqual(mail.outbox[0].to, [self.pi.email])

def test_does_not_send_email_for_active_proposal_with_no_current_allocations(self):
mixer.blend(TimeAllocation, proposal=self.proposal, semester=self.future_semester)
time_allocation_reminder()

self.broker.join('default')
self.worker.join()

self.assertEqual(len(mail.outbox), 0)

def test_does_not_send_email_for_inactive_proposal(self):
mixer.blend(TimeAllocation, proposal=self.proposal, semester=self.current_semester)
self.proposal.active = False
self.proposal.save()
time_allocation_reminder()

self.broker.join('default')
self.worker.join()

self.assertEqual(len(mail.outbox), 0)


class TestProposalUserLimits(TestCase):
def setUp(self):
super().setUp()
Expand Down

0 comments on commit ed783ac

Please sign in to comment.