Skip to content

Commit

Permalink
if TWILIO_ERROR_MESSAGE is configured in settings, then an error mess…
Browse files Browse the repository at this point in the history
…age is added to the messages instance with this text.
  • Loading branch information
yoandyshyno committed Sep 30, 2019
1 parent 5a1018e commit 5817ba7
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions two_factor/gateways/twilio/gateway.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import

from django.conf import settings
from django.contrib import messages
from django.urls import reverse
from django.utils import translation
from django.utils.translation import pgettext, ugettext
Expand Down Expand Up @@ -38,9 +39,9 @@ class Twilio(object):
``TWILIO_CALLER_ID``
Should be set to a verified phone number. Twilio_ differentiates between
numbers verified for making phone calls and sending text messages.
Optionally you may set an error message to be displayed incase of error.
``TWILIO_ERROR_MESSAGE``
Should be set to a string with a custom text.
Expand All @@ -64,10 +65,18 @@ def make_call(self, device, token):

def send_sms(self, device, token):
body = ugettext('Your authentication token is %s') % token
self.client.messages.create(
to=device.number.as_e164,
from_=getattr(settings, 'TWILIO_CALLER_ID'),
body=body)
try:
self.client.messages.create(
to=device.number.as_e164,
from_=getattr(settings, 'TWILIO_CALLER_ID'),
body=body)
except Exception:
twilio_error_message = getattr(settings, 'TWILIO_ERROR_MESSAGE', None)
if twilio_error_message:
request = get_current_request()
messages.add_message(request, messages.ERROR, twilio_error_message)
else:
raise


def validate_voice_locale(locale):
Expand Down

0 comments on commit 5817ba7

Please sign in to comment.