Ejemplo n.º 1
0
    def test_bad_resolver(self):
        """
        assert that we get exception when trying to use the bad resolver
        """

        register_namespace_resolver(BadNotificationNamespaceResolver())
        with self.assertRaises(NotImplementedError):
            resolve_namespace('foo')
    def test_bad_resolver(self):
        """
        assert that we get exception when trying to use the bad resolver
        """

        register_namespace_resolver(BadNotificationNamespaceResolver())
        with self.assertRaises(NotImplementedError):
            resolve_namespace('foo')
Ejemplo n.º 3
0
    def test_default_resolver(self):
        """
        Assert that the default works as expected
        """

        register_namespace_resolver(DefaultNotificationNamespaceResolver())

        namespace = 'foo'
        response = resolve_namespace(namespace)

        self.assertIsNotNone(response)
        self.assertEqual(
            response, {
                'namespace': namespace,
                'display_name': namespace,
                'features': {
                    'digests': False,
                },
                'default_user_resolver': None
            })
    def test_default_resolver(self):
        """
        Assert that the default works as expected
        """

        register_namespace_resolver(DefaultNotificationNamespaceResolver())

        namespace = 'foo'
        response = resolve_namespace(namespace)

        self.assertIsNotNone(response)
        self.assertEqual(
            response,
            {
                'namespace': namespace,
                'display_name': namespace,
                'features': {
                    'digests': False,
                },
                'default_user_resolver': None
            }
        )
Ejemplo n.º 5
0
 def test_no_resolver(self):
     """
     Assert that None is returned
     """
     register_namespace_resolver(None)
     self.assertIsNone(resolve_namespace('foo'))
Ejemplo n.º 6
0
def send_notifications_namespace_digest(namespace, from_timestamp, to_timestamp,
                                        preference_name, subject, from_email, unread_only=True):
    """
    For a particular namespace, send a notification digest, if so configured
    """

    log.info(
        'Inspecting digest for namespace "{namespace}". time ranges '
        '{from_timestamp} to {to_timestamp} preference_name='
        '{preference_name}'.format(
            namespace=namespace,
            from_timestamp=from_timestamp,
            to_timestamp=to_timestamp,
            preference_name=preference_name
        )
    )

    # Resolve the namespace to get information about it
    namespace_info = resolve_namespace(namespace)
    if not namespace_info:
        log.info(
            'Could not resolve namespace "{namespace}". Skipping...'.format(namespace=namespace)
        )
        return 0

    # see if digests are enabled for this namespace
    if not namespace_info['features'].get('digests'):
        log.info(
            'Namespace "{namespace}" does not have the digests feature enabled. '
            'Skipping...'.format(namespace=namespace)
        )
        return 0

    # make sure we can resolve the users in the namespace
    resolver = namespace_info['default_user_resolver']
    if not resolver:
        log.info(
            'Namespace "{namespace}" does not have a default_user_resolver defined. '
            'Skipping...'.format(namespace=namespace)
        )
        return 0

    # see what the default preference is
    notification_preference = get_notification_preference(preference_name)
    default_wants_digest = notification_preference.default_value.lower() == 'true'

    # Get a collection (cursor) of users within this namespace scope
    users = resolver.resolve(
        const.NOTIFICATION_NAMESPACE_USER_SCOPE_NAME,
        {
            'namespace': namespace,
            'fields': {
                'id': True,
                'email': True,
                'first_name': True,
                'last_name': True
            }
        },
        None
    )

    digests_sent = 0

    # Loop over all users that are within the scope of the namespace
    # and specify that we want id, email, first_name, and last_name fields
    for user in users:
        user_id = int(user['id'])
        email = user['email']
        first_name = user['first_name']
        last_name = user['last_name']

        # check preferences for user to get a digest
        user_wants_digest = default_wants_digest
        try:
            user_preference = get_user_preference_by_name(user_id, preference_name)
            user_wants_digest = user_preference.value.lower() == 'true'
        except ItemNotFoundError:
            # use the default
            pass

        if user_wants_digest:
            log.debug(
                'Sending digest email from namespace "{namespace}" '
                'to user_id = {user_id} at email '
                '{email}...'.format(namespace=namespace, user_id=user_id, email=email)
            )
            digests_sent += _send_user_digest(
                namespace_info,
                from_timestamp,
                to_timestamp,
                user_id,
                email,
                first_name,
                last_name,
                subject,
                from_email,
                unread_only=unread_only
            )

    return digests_sent
 def test_no_resolver(self):
     """
     Assert that None is returned
     """
     register_namespace_resolver(None)
     self.assertIsNone(resolve_namespace('foo'))
Ejemplo n.º 8
0
def send_notifications_namespace_digest(namespace, from_timestamp, to_timestamp,
                                        preference_name, subject, from_email, unread_only=True):
    """
    For a particular namespace, send a notification digest, if so configured
    """

    log.info(
        'Inspecting digest for namespace "{namespace}". time ranges '
        '{from_timestamp} to {to_timestamp} preference_name='
        '{preference_name}'.format(
            namespace=namespace,
            from_timestamp=from_timestamp,
            to_timestamp=to_timestamp,
            preference_name=preference_name
        )
    )

    # Resolve the namespace to get information about it
    namespace_info = resolve_namespace(namespace)
    if not namespace_info:
        log.info(
            'Could not resolve namespace "{namespace}". Skipping...'.format(namespace=namespace)
        )
        return 0

    # see if digests are enabled for this namespace
    if not namespace_info['features'].get('digests'):
        log.info(
            'Namespace "{namespace}" does not have the digests feature enabled. '
            'Skipping...'.format(namespace=namespace)
        )
        return 0

    # make sure we can resolve the users in the namespace
    resolver = namespace_info['default_user_resolver']
    if not resolver:
        log.info(
            'Namespace "{namespace}" does not have a default_user_resolver defined. '
            'Skipping...'.format(namespace=namespace)
        )
        return 0

    # see what the default preference is
    notification_preference = get_notification_preference(preference_name)
    default_wants_digest = notification_preference.default_value.lower() == 'true'

    # Get a collection (cursor) of users within this namespace scope
    users = resolver.resolve(
        const.NOTIFICATION_NAMESPACE_USER_SCOPE_NAME,
        {
            'namespace': namespace,
            'fields': {
                'id': True,
                'email': True,
                'first_name': True,
                'last_name': True
            }
        },
        None
    )

    digests_sent = 0

    # Loop over all users that are within the scope of the namespace
    # and specify that we want id, email, first_name, and last_name fields
    for user in users:
        user_id = int(user['id'])
        email = user['email']
        first_name = user['first_name']
        last_name = user['last_name']

        # check preferences for user to get a digest
        user_wants_digest = default_wants_digest
        try:
            user_preference = get_user_preference_by_name(user_id, preference_name)
            user_wants_digest = user_preference.value.lower() == 'true'
        except ItemNotFoundError:
            # use the default
            pass

        if user_wants_digest:
            log.debug(
                'Sending digest email from namespace "{namespace}" '
                'to user_id = {user_id} at email '
                '{email}...'.format(namespace=namespace, user_id=user_id, email=email)
            )
            digests_sent += _send_user_digest(
                namespace_info,
                from_timestamp,
                to_timestamp,
                user_id,
                email,
                first_name,
                last_name,
                subject,
                from_email,
                unread_only=unread_only
            )

    return digests_sent