def send_template(recipient, slug='', context=None, **sms_attrs):
    """
    Use this function to send an SMS template to a given number.
    """
    context = context or {}
    try:
        sms_template = config.get_sms_template_model().objects.get(slug=slug)
        output_sms = config.get_output_sms_model().objects.create(
            recipient=recipient,
            template_slug=slug,
            content=Template(sms_template.body).render(Context(context)),
            state=(config.ATS_STATES.DEBUG if settings.ATS_SMS_DEBUG and recipient not in config.ATS_WHITELIST
                   else config.ATS_STATES.PROCESSING),
            **sms_attrs
        )
        if not settings.ATS_SMS_DEBUG or recipient in config.ATS_WHITELIST:
            parsed_response = send_and_parse_response(output_sms)
            update_sms_state_from_response(output_sms, parsed_response)
            output_sms.save()
        return output_sms
    except config.get_sms_template_model().DoesNotExist:
        LOGGER.error(ugettext('SMS message template with slug {slug} does not exist. '
                              'The message to {recipient} cannot be sent.').format(recipient=recipient, slug=slug))
        output_sms.state = config.ATS_STATES.LOCAL_ERROR
        output_sms.save()
        raise SMSSendingError(ugettext('SMS message template with slug {} does not exist').format(slug))
    except SMSSendingError:
        output_sms.state = config.ATS_STATES.LOCAL_TO_SEND
        output_sms.save()
        raise
def send_and_update_sms_states(*ats_requests):
    """
    Higher-level function performing serialization of ATS requests, parsing ATS server response and updating
    SMS messages state according the received response.
    """
    for uniq, state in send_and_parse_response(*ats_requests).items():
        sms = get_object_or_none(config.get_output_sms_model(), pk=uniq)
        if sms:
            sms.state = state if state in config.ATS_STATES.all else config.ATS_STATES.LOCAL_UNKNOWN_ATS_STATE
            sms.sent_at = timezone.now()
            sms.save()
        else:
            raise SMSValidationError(ugettext('SMS with uniq "{}" not found in DB.').format(uniq))
class OutputATSSMSmesssageISCore(UIRESTModelISCore):

    model = config.get_output_sms_model()
    list_display = ('created_at', 'sent_at', 'sender', 'recipient', 'content', 'state')
    abstract = True
    ui_add_view = OutputATSSMSMessageAddView
    update_permission = False
    delete_permission = False

    def get_form_fields(self, request, obj=None):
        return (
            super(OutputATSSMSmesssageISCore, self).get_form_fields(request, obj) if obj else ('recipient', 'content')
        )
class OutputATSSMSmesssageISCore(UIRESTModelISCore):
    model = config.get_output_sms_model()
    list_display = ('created_at', 'sent_at', 'sender', 'recipient', 'content',
                    'state')
    abstract = True

    def has_create_permission(self, request, obj=None):
        return False

    def has_update_permission(self, request, obj=None):
        return False

    def has_delete_permission(self, request, obj=None):
        return False
def send_and_update_sms_states(*ats_requests):
    """
    Higher-level function performing serialization of ATS requests, parsing ATS server response and updating
    SMS messages state according the received response.
    """
    for uniq, state in send_and_parse_response(*ats_requests).items():
        sms = get_object_or_none(config.get_output_sms_model(), pk=uniq)
        if sms:
            sms.state = state if state in config.ATS_STATES.all else config.ATS_STATES.LOCAL_UNKNOWN_ATS_STATE
            sms.sent_at = timezone.now()
            sms.save()
        else:
            raise SMSValidationError(
                ugettext('SMS with uniq "{}" not found in DB.').format(uniq))
Example #6
0
def update_sms_states(parsed_response):
    """
    Higher-level function performing serialization of ATS requests, parsing ATS server response and updating
    SMS messages state according the received response.
    """
    for uniq, state in parsed_response.items():
        sms = get_object_or_none(get_output_sms_model(), pk=uniq)
        if sms:
            change_and_save(sms,
                            state=state if state in ATS_STATES.all else
                            ATS_STATES.LOCAL_UNKNOWN_ATS_STATE,
                            sent_at=timezone.now())
        else:
            raise SMSValidationError(
                ugettext('SMS with uniq "{}" not found in DB.').format(uniq))
    def handle(self, *args, **options):
        OutputSMSModel = get_output_sms_model()
        to_check = OutputSMSModel.objects.filter(
            state__in=(ATS_STATES.OK, ATS_STATES.NOT_SENT, ATS_STATES.SENT))
        if to_check.exists():
            send_and_update_sms_states(
                *[DeliveryRequest(sms) for sms in to_check])

        idle_output_sms = OutputSMSModel.objects.filter(
            state=ATS_STATES.OK,
            created_at__lt=now() -
            timedelta(minutes=settings.IDLE_MESSAGES_TIMEOUT_MINUTES))
        if settings.LOG_IDLE_MESSAGES and idle_output_sms.exists():
            LOGGER.warning(
                '{count_sms} Output SMS is more than {timeout} minutes in state "OK"'
                .format(count_sms=idle_output_sms.count(),
                        timeout=settings.IDLE_MESSAGES_TIMEOUT_MINUTES))
def send_template(recipient, slug='', context=None, **sms_attrs):
    """
    Use this function to send a SMS template to a given number.
    """
    context = context or {}
    try:
        sms_template = config.get_sms_template_model().objects.get(slug=slug)
        output_sms = config.get_output_sms_model().objects.create(
            recipient=recipient,
            content=Template(sms_template.body).render(Context(context)),
            state=config.ATS_STATES.DEBUG if settings.ATS_SMS_DEBUG else config.ATS_STATES.LOCAL_TO_SEND,
            **sms_attrs
        )
        return output_sms
    except config.get_sms_template_model().DoesNotExist:
        LOGGER.error(ugettext('SMS message template with slug {slug} does not exist. '
                              'The message to {recipient} cannot be sent.').format(recipient=recipient, slug=slug))
        raise SMSSendingError(ugettext('SMS message template with slug {} does not exist').format(slug))
def send_template(recipient, slug='', context=None, **sms_attrs):
    """
    Use this function to send a SMS template to a given number.
    """
    context = context or {}
    try:
        sms_template = config.get_sms_template_model().objects.get(slug=slug)
        output_sms = config.get_output_sms_model().objects.create(
            recipient=recipient,
            content=Template(sms_template.body).render(Context(context)),
            state=config.ATS_STATES.DEBUG
            if settings.ATS_SMS_DEBUG else config.ATS_STATES.LOCAL_TO_SEND,
            **sms_attrs)
        return output_sms
    except config.get_sms_template_model().DoesNotExist:
        LOGGER.error(
            ugettext('SMS message template with slug {slug} does not exist. '
                     'The message to {recipient} cannot be sent.').format(
                         recipient=recipient, slug=slug))
        raise SMSSendingError(
            ugettext('SMS message template with slug {} does not exist').
            format(slug))
Example #10
0
def send_template(recipient, slug='', context=None, **sms_attrs):
    """
    Use this function to send an SMS template to a given number.
    """
    context = context or {}
    try:
        sms_template = get_sms_template_model().objects.get(slug=slug)
        output_sms = get_output_sms_model().objects.create(
            recipient=recipient,
            template_slug=slug,
            content=Template(sms_template.body).render(Context(context)),
            state=(ATS_STATES.DEBUG
                   if settings.DEBUG and recipient not in settings.WHITELIST
                   else ATS_STATES.PROCESSING),
            **sms_attrs)
        return send(output_sms)
    except get_sms_template_model().DoesNotExist:
        LOGGER.error(
            ugettext('SMS message template with slug {slug} does not exist. '
                     'The message to {recipient} cannot be sent.').format(
                         recipient=recipient, slug=slug))
        raise SMSSendingError(
            ugettext('SMS message template with slug {} does not exist').
            format(slug))
 def handle(self, *args, **options):
     messages = get_output_sms_model().objects.filter(
         state=ATS_STATES.LOCAL_TO_SEND)
     if messages.exists():
         send_and_update_sms_states(*list(messages))
 def handle(self, *args, **options):
     to_check = get_output_sms_model().objects.filter(
         state__in=(ATS_STATES.OK, ATS_STATES.NOT_SENT, ATS_STATES.SENT)
     )
     if to_check.exists():
         send_and_update_sms_states(*[DeliveryRequest(sms) for sms in to_check])
 def handle(self, *args, **options):
     get_output_sms_model().objects.filter(
         state=ATS_STATES.PROCESSING, changed_at__lt=timezone.now() - timedelta(seconds=ATS_PROCESSING_TIMEOUT)
     ).update(state=ATS_STATES.TIMEOUT)
 def handle(self, *args, **options):
     get_output_sms_model().objects.filter(
         state=ATS_STATES.PROCESSING,
         changed_at__lt=timezone.now() -
         timedelta(seconds=settings.PROCESSING_TIMEOUT)).update(
             state=ATS_STATES.TIMEOUT)
Example #15
0
 def handle(self, *args, **options):
     to_check = get_output_sms_model().objects.filter(
         state__in=(ATS_STATES.OK, ATS_STATES.NOT_SENT, ATS_STATES.SENT))
     if to_check.exists():
         send_and_update_sms_states(
             *[DeliveryRequest(sms) for sms in to_check])
 def handle(self, *args, **options):
     messages = get_output_sms_model().objects.filter(state=ATS_STATES.LOCAL_TO_SEND)
     if messages.exists():
         send_and_update_sms_states(*list(messages))