Skip to content

helper

django_spire.notification.sms.helper

BulkTwilioSMSHelper

Source code in django_spire/notification/sms/helper.py
def __init__(self, notifications: list[Notification], client: Client):
    self.notifications = notifications
    self.client = client
    self.notification_segments = defaultdict(int)

notifications = notifications instance-attribute

client = client instance-attribute

notification_segments = defaultdict(int) instance-attribute

send_notifications

Source code in django_spire/notification/sms/helper.py
def send_notifications(self):
    self._find_notification_segments()

    sent_segments = 0
    for notification, segments in self.notification_segments.items():
        try:
            TwilioSMSHelper(notification, self.client).send()
            notification.status = NotificationStatusChoices.SENT
            notification.sent_datetime = now()

            sent_segments += segments

            if sent_segments >= settings.TWILIO_SMS_BATCH_SIZE:
                time.sleep(60)
                sent_segments = 0

        except Exception as e:
            if isinstance(e, TwilioAPIConcurrentException):
                notification.status = NotificationStatusChoices.PENDING

            if isinstance(e, TwilioException):
                notification.status_message = str(e)
                notification.status = NotificationStatusChoices.ERRORED
                raise e

            else:
                notification.status_message = str(e)
                notification.status = NotificationStatusChoices.FAILED
                raise e

TwilioSMSHelper

Source code in django_spire/notification/sms/helper.py
def __init__(self, notification: Notification, client: Client):
    self.to_phone_number = self._format_phone_number(
        notification.sms.to_phone_number
    )
    self.notification = notification
    self.message = f'{notification.title}: {notification.body}'
    self.client = client

to_phone_number = self._format_phone_number(notification.sms.to_phone_number) instance-attribute

notification = notification instance-attribute

message = f'{notification.title}: {notification.body}' instance-attribute

client = client instance-attribute

send

Source code in django_spire/notification/sms/helper.py
def send(self):
    response = self._attempt_send()
    self._handle_response(response)