From 4918b4e5419b242575fb92510891b8b6da50b551 Mon Sep 17 00:00:00 2001 From: Austin Riba Date: Tue, 30 Jul 2019 13:28:59 -0700 Subject: [PATCH] Only send emails if semester end is within 3 months. --- observation_portal/proposals/models.py | 23 +++++++++++++---------- observation_portal/proposals/tasks.py | 8 +++++--- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/observation_portal/proposals/models.py b/observation_portal/proposals/models.py index ad1de58f..5b1f82a1 100644 --- a/observation_portal/proposals/models.py +++ b/observation_portal/proposals/models.py @@ -135,16 +135,19 @@ def add_users(self, emails, role): logger.info('Users added to proposal {0}: {1}'.format(self, emails)) def send_time_allocation_reminder(self): - subject = _('Your LCO Time Allocation Summary') - message = render_to_string( - 'proposals/timeallocationreminder.html', - { - 'proposal': self, - 'allocations': self.timeallocation_set.filter(semester=self.current_semester) - } - ) - - send_mail.send(subject, message, 'science-support@lco.global', [self.pi.email]) + if self.pi: + subject = _('Your LCO Time Allocation Summary') + message = render_to_string( + 'proposals/timeallocationreminder.html', + { + 'proposal': self, + 'allocations': self.timeallocation_set.filter(semester=self.current_semester) + } + ) + + send_mail.send(subject, message, 'science-support@lco.global', [self.pi.email]) + else: + logger.warn('Proposal {} does not have a PI!'.format(self)) def __str__(self): return self.id diff --git a/observation_portal/proposals/tasks.py b/observation_portal/proposals/tasks.py index 5bb76494..2e9f8a64 100644 --- a/observation_portal/proposals/tasks.py +++ b/observation_portal/proposals/tasks.py @@ -1,3 +1,4 @@ +from django.utils import timezone import dramatiq import logging @@ -9,8 +10,9 @@ @dramatiq.actor() def time_allocation_reminder(): for proposal in Proposal.current_proposals().filter(active=True): - if proposal.pi: + # 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 \ + proposal.timeallocation_set.count() > 0: logger.info('Sending time allocation reminder for {}'.format(proposal)) proposal.send_time_allocation_reminder() - else: - logger.warn('Proposal {} does not have a PI!'.format(proposal))