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-1.3.1.tar.gz (16.5 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-1.3.1-py3-none-any.whl (47.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for invenio_notifications-1.3.1.tar.gz
Algorithm Hash digest
SHA256 e10561c83f452a3786e0a18558a946b9f55f94652be215f8aa24360512bf4191
MD5 f4a4b10b2bd4b0e9fc9ee19c48bf47c4
BLAKE2b-256 57e5f9c7799715a22bafebb05ae4a0fb31dd75a5a880c476b7b86fcf0537e906

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for invenio_notifications-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5902bcdf508ffe8c94173dfbce8b2f7653403d6f21cfab2120c85e4ea7e5854d
MD5 e742016ac9af37d68d109b8dca78402e
BLAKE2b-256 d1d7b7eb3bd25a92478c7cf2e8f0eef58592592b86fefd018858bf27d3b1bed7

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