Skip to main content
https://github.com/inveniosoftware/invenio-notifications/workflows/CI/badge.svg https://img.shields.io/github/tag/inveniosoftware/invenio-notifications.svg https://img.shields.io/pypi/dm/invenio-notifications.svg https://img.shields.io/github/license/inveniosoftware/invenio-notifications.svg

Invenio module for generic notifications support. It can be used to send notifications via various backends (like email or chat, if implemented) to recipients (like users or groups).

Overview

The two main customizable components provided by Invenio-Notifications for controlling notifications are: NotificationBuilder and NotificationBackend.

A NotificationBuilder is used to both initially build a Notification object with some context, as well as later on to process this notification to determine the recipients and backend to use for actually sending off the notification.

A NotificationBackend receives the notification data and its recipient, and takes care of rendering the notification and actually sending it off.

Another customizable component is the EntityResolver which is used to resolve entities that are referenced in a notification’s serialized data For instance, an EntityResolver would be used to find the user referenced by the dictionary {"user": "1"} and expand the notification’s data with further information like the user’s email address. However, for most cases the provided defaults should be sufficient here.

Configuration

For Invenio-Notifications to do its job properly, the following configuration needs to be set (e.g. in invenio.cfg):

from invenio_notifications.backends.email import EmailNotificationBackend
from invenio_records_resources.references.entity_resolvers import ServiceResultResolver
from my_package import MyNotificationBuilder

# the notification builders need to be registered by their type as key, for the
# notification manager (possibly in a background task running in another Python
# interpreter) to look up the right notification builder for processing
NOTIFICATIONS_BUILDERS = {
    MyNotificationBuilder.type: MyNotificationBuilder,
    # ...
}

# notification backends, responsible for actually sending out notifications
NOTIFICATIONS_BACKENDS = {"email": EmailNotificationBackend}

# entity resolvers, used for finding referenced entities like users in the built
# notifications' data
NOTIFICATIONS_ENTITY_RESOLVERS = [
    ServiceResultResolver(service_id="users", type_key="user"),
    ServiceResultResolver(service_id="groups", type_key="group"),
    # ...
]

Example

Here is an annotated example for a NotificationBuilder used as part of a custom request type’s “accept” action:

from invenio_notifications.models import Notification
from invenio_notifications.registry import EntityResolverRegistry
from invenio_notifications.services.builders import NotificationBuilder
from invenio_notifications.services.generators import EmailBackendGenerator, EntityResolverContextGenerator
from invenio_users_resources.notifications.filters import UserPreferencesRecipientFilter
from invenio_users_resources.notifications.generators import UserRecipient

class MyRequestNotificationBuilder(NotificationBuilder):
    """Base notification builder for "accept" events on my custom request type."""

    type = "my-request-type.accept"

    @classmethod
    def build(cls, request, result):
        """Build notification with request context."""
        return Notification(
            type=cls.type,

            # the context is used for determining the recipients, and is also
            # available for rendering the notification template
            context={
                "request": EntityResolverRegistry.reference_entity(request),
                "result": result,
            },
        )

    # first resolve the referenced request and expand the `notification.context` data;
    # afterwards expand the referenced creator, topic, and receiver
    context = [
        EntityResolverContextGenerator(key="request"),
        EntityResolverContextGenerator(key="request.created_by"),
        EntityResolverContextGenerator(key="request.topic"),
        EntityResolverContextGenerator(key="request.receiver"),
    ]

    # create a `Recipient` instance from the resolved request creator
    # (the other expanded entities may be relevant in template rendering)
    recipients = [
        UserRecipient(key="request.created_by"),
    ]

    # filter out recipients that have notifications disabled
    recipient_filters = [
        UserPreferencesRecipientFilter(),
    ]

    # use the email backend, which will render e.g. "my-request-type.accept.jinja"
    recipient_backends = [
        EmailBackendGenerator(),
    ]

# ...
# the builder needs to be registered in the `NOTIFICATIONS_BUILDERS` config;
# here's how the builder would be typically used in request actions:
# ...

import random
from invenio_requests.customizations.actions import AcceptAction
from invenio_notifications.services.uow import NotificationOp

class MyRequestTypeAcceptAction(AcceptAction):
    """Accept action for my custom request type, sending a notification."""

    def execute(self, identity, uow):
        """Calculate an important result on "accept" and send a notification."""
        result = random.randint(0, 100)
        uow.register(
            NotificationOp(
                MyRequestNotificationBuilder.build(
                    request=self.request,
                    result=result,
                )
            )
        )
        super().execute(identity, uow)

# ...
# further code for the customized request type and its registration is omitted
# ...

Usage in the Invenio ecosystem

This package is used heavily in Invenio-Requests (or rather in the packages that implement requests) to send notifications to involved users whenever something happens with a request.

Generally, each request action generates a Notification via a dedicated NotificationBuilder and registers it to the action’s UnitOfWork via a NotificationOp. Upon successful completion of the transaction, that will schedule a background task where the NotificationManager processes the notification. Since the background task runs in another Python interpreter, the manager needs to look up the appropriate NotificationBuilder to use for processing the notification via the notification.type. The builder is then used to evaluate the recipients of the notification and the backend (like email) to use to finally render and send the notifications.

Concrete implementations of notification builders for various request actions can be found in Invenio-RDM-Records.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

invenio_notifications-2.0.1.tar.gz (16.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

invenio_notifications-2.0.1-py3-none-any.whl (47.3 kB view details)

Uploaded Python 3

File details

Details for the file invenio_notifications-2.0.1.tar.gz.

File metadata

  • Download URL: invenio_notifications-2.0.1.tar.gz
  • Upload date:
  • Size: 16.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for invenio_notifications-2.0.1.tar.gz
Algorithm Hash digest
SHA256 90a0db02773511fa819f31234c181cb6369be8be03e705f55da78de4e3378fef
MD5 e7bace913c161a9e2c25c709249263fb
BLAKE2b-256 bff04105f19f7f3ca55803e5091cdf3fa0641b46fbe273c364fa3a749dae8bb4

See more details on using hashes here.

File details

Details for the file invenio_notifications-2.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for invenio_notifications-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9e533eff4ed8807fd4ccd664a45172f08c4d918e3e5e93b96e4727d1aa0f6ad1
MD5 87d0203ff3158e645524d8d2c0bbbb40
BLAKE2b-256 499aae10361cf277e38c938e5d538b1d7b7fad75f130f37ad56fd7774fa24a88

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page