Don't send monthly reports to inactive accounts (no pings in 6 months)

This commit is contained in:
Pēteris Caune 2018-10-24 11:30:16 +03:00
parent 9f02371d6a
commit 58cfaaa527
No known key found for this signature in database
GPG Key ID: E28D7679E9A9EDE2
3 changed files with 26 additions and 2 deletions

View File

@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.
### Improvements ### Improvements
- Load settings from environment variables - Load settings from environment variables
- Add "List-Unsubscribe" header to alert and report emails - Add "List-Unsubscribe" header to alert and report emails
- Don't send monthly reports to inactive accounts (no pings in 6 months)
### Bug Fixes ### Bug Fixes
- During DST transition, handle ambiguous dates as pre-transition - During DST transition, handle ambiguous dates as pre-transition

View File

@ -133,8 +133,12 @@ class Profile(models.Model):
def send_report(self, nag=False): def send_report(self, nag=False):
checks = self.checks_from_all_teams() checks = self.checks_from_all_teams()
# Is there at least one check that has received a ping? # Has there been a ping in last 6 months?
if not checks.filter(last_ping__isnull=False).exists(): result = checks.aggregate(models.Max("last_ping"))
last_ping = result["last_ping__max"]
six_months_ago = timezone.now() - timedelta(days=180)
if last_ping is None or last_ping < six_months_ago:
return False return False
# Is there at least one check that is down? # Is there at least one check that is down?

View File

@ -64,6 +64,25 @@ class ProfileTestCase(BaseTestCase):
self.assertEqual(message.subject, 'Monthly Report') self.assertEqual(message.subject, 'Monthly Report')
self.assertIn("Test Check", message.body) self.assertIn("Test Check", message.body)
def test_it_skips_report_if_no_pings(self):
check = Check(name="Test Check", user=self.alice)
check.save()
sent = self.profile.send_report()
self.assertFalse(sent)
self.assertEqual(len(mail.outbox), 0)
def test_it_skips_report_if_no_recent_pings(self):
check = Check(name="Test Check", user=self.alice)
check.last_ping = now() - td(days=365)
check.save()
sent = self.profile.send_report()
self.assertFalse(sent)
self.assertEqual(len(mail.outbox), 0)
def test_it_sends_nag(self): def test_it_sends_nag(self):
check = Check(name="Test Check", user=self.alice) check = Check(name="Test Check", user=self.alice)
check.status = "down" check.status = "down"