shiftregister/shiftregister/messaging/outbound.py

44 lines
1.0 KiB
Python

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from . import Message
from .backends.abc import Sender
from .exceptions import OutboundMessageError
from .utils import import_class
__all__ = ("send",)
def resolve_backend():
sender_cls = settings.SMS_OUTBOUND_BACKEND
if isinstance(sender_cls, str):
sender_cls = import_class(sender_cls, "Sender")
if not issubclass(sender_cls, Sender):
raise ImproperlyConfigured(
"SMS_OUTBOUND_BACKEND must be a subclass of shiftregister.messaging.backends.abc.Sender"
)
return sender_cls()
sender = resolve_backend()
def send(messages):
if isinstance(messages, Message):
messages = [messages]
else:
messages = list(messages)
if len(messages) == 0:
return
sent_messages = 0
for message in sender.send(messages):
sent_messages += 1
yield message
if sent_messages == 0:
raise OutboundMessageError("no messages have been sent")