Ejemplo n.º 1
0
def push_notification(notification_type, alert, extra_metadata={}, recipient=None, badge=None, request=None):
    """
    `recipient` is a User instance.
    """
    # ### DISABLED ###
    return

    ua = Airship(settings.URBANAIRSHIP_APP_KEY, settings.URBANAIRSHIP_APP_MASTER_SECRET)

    def payload_for_language(language_code):
        try:
            translation.activate(language_code)
        except AttributeError:
            translation.activate('en')
            localized_alert = force_text(alert)
        else:
            localized_alert = force_text(alert)
        finally:
            translation.deactivate()

        _payload = {'aps': {'alert': localized_alert}, 'push_notification_type': notification_type}
        _payload.update(extra_metadata)

        if badge is not None:
            _payload['aps']['badge'] = badge

        return _payload

    payload = payload_for_language('en')

    if notification_type in GLOBAL_PUSH_NOTIFICATION_TYPES:
        if settings.PRODUCTION or settings.STAGING:
            ua.push(payload, tags=[notification_type])

            for locale in settings.ALL_LOCALES:
                localized_payload = payload_for_language(locale)

                ua.push(localized_payload, tags=['{}-{}'.format(notification_type, locale)])

            util.logger.info(u"Sent global push notification with alert: {}".format(alert))
    elif notification_type in PERSONAL_PUSH_NOTIFICATION_TYPES:
        if (settings.PRODUCTION or settings.STAGING) and not is_unsubscribed(recipient, notification_type):
            try:
                ua.push(payload, aliases=[recipient.username])
            except AirshipFailure:
                # UA frequently errors out. Retry.
                try:
                    ua.push(payload, aliases=[recipient.username])
                except AirshipFailure:
                    pass
    else:
        raise ValueError("Invalid push notification type '{}'.".format(notification_type))
Ejemplo n.º 2
0
def push_notification(notification_type, alert, extra_metadata={}, recipient=None, badge=None, request=None):
    """
    `recipient` is a User instance.
    """
    ua = Airship(settings.URBANAIRSHIP_APP_KEY, settings.URBANAIRSHIP_APP_MASTER_SECRET)
    type_ = notification_type

    payload = {'aps': {'alert': alert}, 'push_notification_type': notification_type}
    payload.update(extra_metadata)

    if badge is not None:
        payload['aps']['badge'] = badge

    if type_ in GLOBAL_PUSH_NOTIFICATION_TYPES:
        if settings.PRODUCTION:
            #TODO, only send to this tag with .push: tags=[notification_type])
            ua.broadcast(payload)
            util.logger.info("Sent global push notification with alert: {}".format(alert))
    elif type_ in PERSONAL_PUSH_NOTIFICATION_TYPES:
        if settings.PRODUCTION and not is_unsubscribed(recipient, type_):
            ua.push(payload, aliases=[recipient.username])
    else:
        raise ValueError("Invalid push notification type '{}'.".format(notification_type))
Ejemplo n.º 3
0
def push_notification(notification_type,
                      alert,
                      extra_metadata={},
                      recipient=None,
                      badge=None,
                      request=None):
    """
    `recipient` is a User instance.
    """
    ua = Airship(settings.URBANAIRSHIP_APP_KEY,
                 settings.URBANAIRSHIP_APP_MASTER_SECRET)
    type_ = notification_type

    payload = {
        'aps': {
            'alert': alert
        },
        'push_notification_type': notification_type
    }
    payload.update(extra_metadata)

    if badge is not None:
        payload['aps']['badge'] = badge

    if type_ in GLOBAL_PUSH_NOTIFICATION_TYPES:
        if settings.PRODUCTION:
            #TODO, only send to this tag with .push: tags=[notification_type])
            ua.broadcast(payload)
            util.logger.info(
                "Sent global push notification with alert: {}".format(alert))
    elif type_ in PERSONAL_PUSH_NOTIFICATION_TYPES:
        if settings.PRODUCTION and not is_unsubscribed(recipient, type_):
            ua.push(payload, aliases=[recipient.username])
    else:
        raise ValueError(
            "Invalid push notification type '{}'.".format(notification_type))
Ejemplo n.º 4
0
def invite_user(request, username, payload):
    user = get_object_or_404(User, username=username)
    ua = Airship(settings.URBANAIRSHIP_APP_KEY, settings.URBANAIRSHIP_APP_MASTER_SECRET)
    ua.push(payload, aliases=[username])
    return {}
Ejemplo n.º 5
0
def invite_user(request, username, payload):
    user = get_object_or_404(User, username=username)
    ua = Airship(settings.URBANAIRSHIP_APP_KEY,
                 settings.URBANAIRSHIP_APP_MASTER_SECRET)
    ua.push(payload, aliases=[username])
    return {}
Ejemplo n.º 6
0
def push_notification(notification_type,
                      alert,
                      extra_metadata={},
                      recipient=None,
                      badge=None,
                      request=None):
    """
    `recipient` is a User instance.
    """
    # ### DISABLED ###
    return

    ua = Airship(settings.URBANAIRSHIP_APP_KEY,
                 settings.URBANAIRSHIP_APP_MASTER_SECRET)

    def payload_for_language(language_code):
        try:
            translation.activate(language_code)
        except AttributeError:
            translation.activate('en')
            localized_alert = force_text(alert)
        else:
            localized_alert = force_text(alert)
        finally:
            translation.deactivate()

        _payload = {
            'aps': {
                'alert': localized_alert
            },
            'push_notification_type': notification_type
        }
        _payload.update(extra_metadata)

        if badge is not None:
            _payload['aps']['badge'] = badge

        return _payload

    payload = payload_for_language('en')

    if notification_type in GLOBAL_PUSH_NOTIFICATION_TYPES:
        if settings.PRODUCTION or settings.STAGING:
            ua.push(payload, tags=[notification_type])

            for locale in settings.ALL_LOCALES:
                localized_payload = payload_for_language(locale)

                ua.push(localized_payload,
                        tags=['{}-{}'.format(notification_type, locale)])

            util.logger.info(
                u"Sent global push notification with alert: {}".format(alert))
    elif notification_type in PERSONAL_PUSH_NOTIFICATION_TYPES:
        if (settings.PRODUCTION or settings.STAGING) and not is_unsubscribed(
                recipient, notification_type):
            try:
                ua.push(payload, aliases=[recipient.username])
            except AirshipFailure:
                # UA frequently errors out. Retry.
                try:
                    ua.push(payload, aliases=[recipient.username])
                except AirshipFailure:
                    pass
    else:
        raise ValueError(
            "Invalid push notification type '{}'.".format(notification_type))