diff --git a/CHANGELOG.md b/CHANGELOG.md index 8aad926d..8fc4416c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. ### Improvements - Load settings from environment variables - Add "List-Unsubscribe" header to alert and report emails +- Don't send monthly reports to inactive accounts (no pings in 6 months) ### Bug Fixes - During DST transition, handle ambiguous dates as pre-transition diff --git a/hc/accounts/models.py b/hc/accounts/models.py index f6d701ca..6d051f01 100644 --- a/hc/accounts/models.py +++ b/hc/accounts/models.py @@ -133,8 +133,12 @@ class Profile(models.Model): def send_report(self, nag=False): checks = self.checks_from_all_teams() - # Is there at least one check that has received a ping? - if not checks.filter(last_ping__isnull=False).exists(): + # Has there been a ping in last 6 months? + 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 # Is there at least one check that is down? diff --git a/hc/accounts/tests/test_profile.py b/hc/accounts/tests/test_profile.py index f0a5f3e8..f5ea306c 100644 --- a/hc/accounts/tests/test_profile.py +++ b/hc/accounts/tests/test_profile.py @@ -64,6 +64,25 @@ class ProfileTestCase(BaseTestCase): self.assertEqual(message.subject, 'Monthly Report') 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): check = Check(name="Test Check", user=self.alice) check.status = "down"