Esempio n. 1
0
def _create_broadcast_event(broadcast_message):
    """
    Creates a broadcast event, stores it in the database, and triggers the task to send the CAP XML off
    """
    msg_types = {
        BroadcastStatusType.BROADCASTING: BroadcastEventMessageType.ALERT,
        BroadcastStatusType.CANCELLED: BroadcastEventMessageType.CANCEL,
    }

    event = BroadcastEvent(
        service=broadcast_message.service,
        broadcast_message=broadcast_message,
        message_type=msg_types[broadcast_message.status],
        transmitted_content={"body": broadcast_message.content},
        transmitted_areas=broadcast_message.areas,
        # TODO: Probably move this somewhere more standalone too and imply that it shouldn't change. Should it include
        # a service based identifier too? eg "*****@*****.**" or similar
        transmitted_sender='notifications.service.gov.uk',

        # TODO: Should this be set to now? Or the original starts_at?
        transmitted_starts_at=broadcast_message.starts_at,
        transmitted_finishes_at=broadcast_message.finishes_at,
    )

    dao_save_object(event)

    if not broadcast_message.stubbed or current_app.config['NOTIFY_ENVIRONMENT'] in ['preview', 'development']:
        send_broadcast_event.apply_async(
            kwargs={'broadcast_event_id': str(event.id)},
            queue=QueueNames.BROADCASTS
        )
Esempio n. 2
0
def _create_broadcast_event(broadcast_message):
    """
    Creates a broadcast event, stores it in the database, and triggers the task to send the CAP XML off
    """
    msg_types = {
        BroadcastStatusType.BROADCASTING: BroadcastEventMessageType.ALERT,
        BroadcastStatusType.CANCELLED: BroadcastEventMessageType.CANCEL,
    }

    if broadcast_message.status == BroadcastStatusType.CANCELLED:
        transmitted_finishes_at = broadcast_message.cancelled_at
    else:
        transmitted_finishes_at = broadcast_message.finishes_at

    # TODO: Remove this if statement after broadcast message content is guaranteed to always be populated.
    if broadcast_message.content:
        content = broadcast_message.content
    else:
        content = broadcast_message.template._as_utils_template_with_personalisation(
            broadcast_message.personalisation
        ).content_with_placeholders_filled_in

    event = BroadcastEvent(
        service=broadcast_message.service,
        broadcast_message=broadcast_message,
        message_type=msg_types[broadcast_message.status],
        transmitted_content={"body": content},
        transmitted_areas=broadcast_message.areas,
        # TODO: Probably move this somewhere more standalone too and imply that it shouldn't change. Should it include
        # a service based identifier too? eg "*****@*****.**" or similar
        transmitted_sender='notifications.service.gov.uk',

        # TODO: Should this be set to now? Or the original starts_at?
        transmitted_starts_at=broadcast_message.starts_at,
        # TODO: When cancelling, do we need to set this to now? Or should we keep it as the original time.
        transmitted_finishes_at=transmitted_finishes_at,
    )

    dao_save_object(event)

    send_broadcast_event.apply_async(
        kwargs={'broadcast_event_id': str(event.id)},
        queue=QueueNames.NOTIFY
    )
Esempio n. 3
0
def _create_broadcast_event(broadcast_message):
    """
    If the service is live and the broadcast message is not stubbed, creates a broadcast event, stores it in the
    database, and triggers the task to send the CAP XML off.
    """
    service = broadcast_message.service

    if not broadcast_message.stubbed and not service.restricted:
        msg_types = {
            BroadcastStatusType.BROADCASTING: BroadcastEventMessageType.ALERT,
            BroadcastStatusType.CANCELLED: BroadcastEventMessageType.CANCEL,
        }

        event = BroadcastEvent(
            service=service,
            broadcast_message=broadcast_message,
            message_type=msg_types[broadcast_message.status],
            transmitted_content={"body": broadcast_message.content},
            transmitted_areas=broadcast_message.areas,
            # TODO: Probably move this somewhere more standalone too and imply that it shouldn't change. Should it
            # include a service based identifier too? eg "*****@*****.**" or similar
            transmitted_sender='notifications.service.gov.uk',

            # TODO: Should this be set to now? Or the original starts_at?
            transmitted_starts_at=broadcast_message.starts_at,
            transmitted_finishes_at=broadcast_message.finishes_at,
        )

        dao_save_object(event)

        send_broadcast_event.apply_async(
            kwargs={'broadcast_event_id': str(event.id)},
            queue=QueueNames.BROADCASTS)
    elif broadcast_message.stubbed != service.restricted:
        # It's possible for a service to create a broadcast in trial mode, and then approve it after the
        # service is live (or vice versa). We don't think it's safe to send such broadcasts, as the service
        # has changed since they were created. Log an error instead.
        current_app.logger.error(
            f'Broadcast event not created. Stubbed status of broadcast message was {broadcast_message.stubbed}'
            f' but service was {"in trial mode" if service.restricted else "live"}'
        )
Esempio n. 4
0
def _create_broadcast_event(broadcast_message):
    """
    Creates a broadcast event, stores it in the database, and triggers the task to send the CAP XML off
    """
    msg_types = {
        BroadcastStatusType.BROADCASTING: BroadcastEventMessageType.ALERT,
        BroadcastStatusType.CANCELLED: BroadcastEventMessageType.CANCEL,
    }

    if broadcast_message.status == BroadcastStatusType.CANCELLED:
        transmitted_finishes_at = broadcast_message.cancelled_at
    else:
        transmitted_finishes_at = broadcast_message.finishes_at

    # TODO: This doesn't support placeholders yet. We shouldn't use BroadcastMessageTemplate when we add placeholders
    # as that just outputs XML, we need the raw text.
    event = BroadcastEvent(
        service=broadcast_message.service,
        broadcast_message=broadcast_message,
        message_type=msg_types[broadcast_message.status],
        transmitted_content={"body": broadcast_message.template.content},
        transmitted_areas=broadcast_message.areas,
        # TODO: Probably move this somewhere more standalone too and imply that it shouldn't change. Should it include
        # a service based identifier too? eg "*****@*****.**" or similar
        transmitted_sender='notifications.service.gov.uk',

        # TODO: Should this be set to now? Or the original starts_at?
        transmitted_starts_at=broadcast_message.starts_at,
        # TODO: When cancelling, do we need to set this to now? Or should we keep it as the original time.
        transmitted_finishes_at=transmitted_finishes_at,
    )

    dao_save_object(event)

    send_broadcast_event.apply_async(
        kwargs={'broadcast_event_id': str(event.id)}, queue=QueueNames.NOTIFY)