Browse Source

Send email notification when monthly SMS sending limit is reached. Fixes #292

pull/307/head
Pēteris Caune 5 years ago
parent
commit
66a6de70c0
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
8 changed files with 61 additions and 0 deletions
  1. +1
    -0
      CHANGELOG.md
  2. +7
    -0
      hc/accounts/models.py
  3. +14
    -0
      hc/api/tests/test_notify.py
  4. +2
    -0
      hc/api/transports.py
  5. +4
    -0
      hc/lib/emails.py
  6. +22
    -0
      templates/emails/sms-limit-body-html.html
  7. +10
    -0
      templates/emails/sms-limit-body-text.html
  8. +1
    -0
      templates/emails/sms-limit-subject.html

+ 1
- 0
CHANGELOG.md View File

@ -14,6 +14,7 @@ All notable changes to this project will be documented in this file.
- Add support for OpsGenie EU region (#294)
- Update OpsGenie logo and setup illustrations
- Add a "Create a Copy" function for cloning checks (#288)
- Send email notification when monthly SMS sending limit is reached (#292)
### Bug Fixes
- Prevent double-clicking the submit button in signup form


+ 7
- 0
hc/accounts/models.py View File

@ -109,6 +109,13 @@ class Profile(models.Model):
ctx = {"button_text": "Change Email", "button_url": settings.SITE_ROOT + path}
emails.change_email(self.user.email, ctx)
def send_sms_limit_notice(self, transport):
ctx = {"transport": transport, "limit": self.sms_limit}
if self.sms_limit != 500 and settings.USE_PAYMENTS:
ctx["url"] = settings.SITE_ROOT + reverse("hc-billing")
emails.sms_limit(self.user.email, ctx)
def projects(self):
""" Return a queryset of all projects we have access to. """


+ 14
- 0
hc/api/tests/test_notify.py View File

@ -591,6 +591,13 @@ class NotifyTestCase(BaseTestCase):
n = Notification.objects.get()
self.assertTrue("Monthly SMS limit exceeded" in n.error)
# And email should have been sent
self.assertEqual(len(mail.outbox), 1)
email = mail.outbox[0]
self.assertEqual(email.to[0], "[email protected]")
self.assertEqual(email.subject, "Monthly SMS Limit Reached")
@patch("hc.api.transports.requests.request")
def test_sms_limit_reset(self, mock_post):
# At limit, but also into a new month
@ -652,6 +659,13 @@ class NotifyTestCase(BaseTestCase):
n = Notification.objects.get()
self.assertTrue("Monthly message limit exceeded" in n.error)
# And email should have been sent
self.assertEqual(len(mail.outbox), 1)
email = mail.outbox[0]
self.assertEqual(email.to[0], "[email protected]")
self.assertEqual(email.subject, "Monthly WhatsApp Limit Reached")
@patch("apprise.Apprise")
@override_settings(APPRISE_ENABLED=True)
def test_apprise_enabled(self, mock_apprise):


+ 2
- 0
hc/api/transports.py View File

@ -412,6 +412,7 @@ class Sms(HttpTransport):
def notify(self, check):
profile = Profile.objects.for_user(self.channel.project.owner)
if not profile.authorize_sms():
profile.send_sms_limit_notice("SMS")
return "Monthly SMS limit exceeded"
url = self.URL % settings.TWILIO_ACCOUNT
@ -439,6 +440,7 @@ class WhatsApp(HttpTransport):
def notify(self, check):
profile = Profile.objects.for_user(self.channel.project.owner)
if not profile.authorize_sms():
profile.send_sms_limit_notice("WhatsApp")
return "Monthly message limit exceeded"
url = self.URL % settings.TWILIO_ACCOUNT


+ 4
- 0
hc/lib/emails.py View File

@ -75,3 +75,7 @@ def invoice(to, ctx, filename, pdf_data):
def deletion_notice(to, ctx, headers={}):
send("deletion-notice", to, ctx, headers)
def sms_limit(to, ctx):
send("sms-limit", to, ctx)

+ 22
- 0
templates/emails/sms-limit-body-html.html View File

@ -0,0 +1,22 @@
{% extends "emails/base.html" %}
{% load hc_extras %}
{% block content %}
Hello,<br />
<p>
We could not deliver a {{ transport }} notification because your {% site_name %}
account has reached its monthly sending limit of
<strong>{{ limit }} sends per month</strong>. The limit resets at the start of
each month.
</p>
{% if url %}
<p>You can increase the monthly sending limit by <a href="{{ url }}">upgrading your billing plan</a>.</p>
{% endif %}
{% endblock %}
{% block content_more %}
Regards,<br />
The {% site_name %} Team
{% endblock %}

+ 10
- 0
templates/emails/sms-limit-body-text.html View File

@ -0,0 +1,10 @@
{% load hc_extras %}Hello,
We could not deliver a {{ transport }} notification because your {% site_name %} account has reached its monthly sending limit of {{ limit }} sends per month. The limit resets at the start of each month.
{% if url %}You can increase the monthly sending limit by upgrading your billing plan:
{{ url }}{% endif %}
--
Regards,
The {% site_name %} Team

+ 1
- 0
templates/emails/sms-limit-subject.html View File

@ -0,0 +1 @@
Monthly {{ transport }} Limit Reached

Loading…
Cancel
Save