Reference

pynotify.config

class pynotify.config.Settings[source]

Bases: object

Holds default configuration values, the values can be overriden in settings with PYNOTIFY_ prefix.

pynotify.dispatchers

class pynotify.dispatchers.BaseDispatcher[source]

Bases: object

Base class for sending notification over a communication channel (e.g. e-mail, sms, push).

dispatch(notification)[source]

This method should implement actual sending of notification.

pynotify.exceptions

exception pynotify.exceptions.MissingContextVariableError(field_name, variable)[source]

Bases: Exception

Raised when template field cannot be rendered because variable used in it is not present in the context.

pynotify.handlers

class pynotify.handlers.BaseHandler[source]

Bases: object

Base class for handling creation of notification(s). Its purpose is to process signal kwargs sent over a defined signal. There should be typically one handler (inherited from this class) for each signal. The handler must define inner class Meta with following supported attributes:

  • signal: Signal to which handler will be registered
  • allowed_senders: Handler will be called only if signal was sent by allowed sender
  • abstract: If set to True, the handler will not be registered
dispatcher_classes

An iterable of dispatcher classes that will be used to dispatch each notification.

template_slug

Slug of an existing admin template to be used. If not defined, you must define get_template_data() method.

get_dispatcher_classes()[source]

Returns iterable of dispatcher classes used to dispatch notification(s).

get_extra_data()[source]

Returns a dictionary with extra data, the values must be JSON serializable.

get_recipients()[source]

Returns an iterable of recipients for which notification will be created.

Returns a list or dictionary of related objects in format {“name”: object}. Named related objects (i.e. those passed using a dictionary) can be referred in notification template.

get_template_data()[source]

Returns kwargs used to create a template. Not called if template slug is used.

get_template_slug()[source]

Returns slug of an admin template to be used.

handle(signal_kwargs)[source]

Handles creation of notifications from signal_kwargs.

class pynotify.handlers.HandlerMeta[source]

Bases: type

Registers handler for handling of signal defined in handler’s Meta.

pynotify.helpers

class pynotify.helpers.DeletedRelatedObject[source]

Bases: object

Placeholder class that substitutes deleted related object and returns:
  • “[DELETED]” as its string representation
  • itself for any attribute accessed
class pynotify.helpers.SecureRelatedObject(related_object)[source]

Bases: object

Security proxy class allowing to access only string representation of the related object and a set of attributes defined in RELATED_OBJECTS_ALLOWED_ATTRS settings.

class pynotify.helpers.SignalMap[source]

Bases: object

Maps signals to arbitrary values.

pynotify.helpers.autoload()[source]

Attempts to load (import) notification handlers from modules defined in PYNOTIFY_AUTOLOAD_MODULES

pynotify.helpers.get_from_context(variable, context)[source]

Tries to find variable value in given context.

Parameters:
  • variable – Variable to look for. Template format is supported (e.g. “abc.def.ghi”).
  • context – Template context.
Returns:

Variable value or None if not found.

pynotify.helpers.get_import_path(_class)[source]

Returns import path for a given class.

pynotify.helpers.process_task(handler_class, serializer_class, signal_kwargs)[source]

Deserializes signal kwargs using the given serializer and calls given handler. This function is intended to be called from a Celery task.

pynotify.helpers.receive(sender, **kwargs)[source]

Initiates processing of the signal by notification handlers through a receiver.

pynotify.helpers.register(signal, handler_class, allowed_senders=None)[source]

Starts listening to signal and registers handler_class to it.

pynotify.helpers.strip_html(value)[source]

Strips HTML (tags and entities) from string value.

pynotify.models

class pynotify.models.AdminNotificationTemplate(*args, **kwargs)[source]

Bases: pynotify.models.BaseTemplate

Represents a “template of a template”. This model is intended to be managed from administration, hence its name. It is identified by slug, which can be used for notification creation. However, this template is never used to directly render a notification, but instead is used to create NotificationTemplate with same values.

slug

Template slug, with which this template can be referred to.

is_active

Flag that switches on/off creating notifications from this template.

is_locked

Flag that switches on/off this template editing (for admin purposes, requires admin-side support).

send_push

Flag that switches on/off sending push notifications from this template. Currently, it has no effect on its own, but you can use it in your custom push notification solution.

exception DoesNotExist

Bases: django.core.exceptions.ObjectDoesNotExist

exception MultipleObjectsReturned

Bases: django.core.exceptions.MultipleObjectsReturned

class pynotify.models.BaseModel(*args, **kwargs)[source]

Bases: chamber.models.SmartModel

Base class for models that outpus its verbose name and PK.

class pynotify.models.BaseTemplate(*args, **kwargs)[source]

Bases: pynotify.models.BaseModel

Base abstract model for notification template.

title

Title of the notification.

text

Text of the notification.

trigger_action

Arbitrary action performed when user triggers (i.e. clicks/taps) the notification.

class pynotify.models.Notification(*args, **kwargs)[source]

Bases: pynotify.models.BaseModel

Represents the notification.

Attributes specified in NotificationTemplate.TEMPLATE_FIELDS are also available here, as generated properties, that are evaluated at runtime and will return rendered field from the associated template. By default, the context used for rendering is filled with named related objects and extra data, so they can be referenced in the template by their name/key.

recipient

Recipient of the notification.

template

Template used to render generated notification fields.

is_read

Boolean flag indicating that recipitent has seen the notification.

is_triggered

Boolean flag indicating that recipient has triggered the notification (e.g. clicked/tapped)

extra_data

JSON serialized dictionary with extra data.

exception DoesNotExist

Bases: django.core.exceptions.ObjectDoesNotExist

exception MultipleObjectsReturned

Bases: django.core.exceptions.MultipleObjectsReturned

context

Returns context dictionary used for rendering the template.

get_extra_data()[source]

Getter for extra_data field.

Returns:Dictionary with extra data.
related_objects_dict

Returns named related objects as a dictionary where key is name of the related object and value is the object itself. Related objects without name are skipped.

set_extra_data(extra_data)[source]

Setter for extra_data field.

Parameters:extra_data – Dictionary of JSON serializable values.
class pynotify.models.NotificationMeta[source]

Bases: chamber.models.SmartModelBase, type

Creates property for each template field. The property returns rendered template.

class pynotify.models.NotificationQuerySet(model=None, query=None, using=None, hints=None)[source]

Bases: chamber.models.SmartQuerySet

create(recipient, template, related_objects=None, extra_data=None, **kwargs)[source]

Create a new object with the given kwargs, saving it to the database and returning the created object.

class pynotify.models.NotificationRelatedObject(*args, **kwargs)[source]

Bases: pynotify.models.BaseModel

Represents object related to a notification. This object can be then referenced in notification template fields by its name (if not None).

name

String identificator of the object (for referencing in templates).

notification

Related notification.

content_object

The related object itself.

exception DoesNotExist

Bases: django.core.exceptions.ObjectDoesNotExist

exception MultipleObjectsReturned

Bases: django.core.exceptions.MultipleObjectsReturned

class pynotify.models.NotificationTemplate(*args, **kwargs)[source]

Bases: pynotify.models.BaseTemplate

Represents template that is used for rendering notification fields. Each field specified in TEMPLATE_FIELDS is a template string, that can be rendered using the render method.

title

Title of the notification.

text

Text of the notification.

trigger_action

Arbitrary action performed when user triggers (i.e. clicks/taps) the notification.

admin_template

Reference to admin template that was used to create this notification template.

exception DoesNotExist

Bases: django.core.exceptions.ObjectDoesNotExist

exception MultipleObjectsReturned

Bases: django.core.exceptions.MultipleObjectsReturned

render(field, context)[source]

Renders field using context.

pynotify.notify

class pynotify.notify.NotifyHandler[source]

Bases: pynotify.handlers.BaseHandler

Notification handler for notify_signal.

get_dispatcher_classes()[source]

Returns iterable of dispatcher classes used to dispatch notification(s).

get_extra_data()[source]

Returns a dictionary with extra data, the values must be JSON serializable.

get_recipients()[source]

Returns an iterable of recipients for which notification will be created.

Returns a list or dictionary of related objects in format {“name”: object}. Named related objects (i.e. those passed using a dictionary) can be referred in notification template.

get_template_data()[source]

Returns kwargs used to create a template. Not called if template slug is used.

get_template_slug()[source]

Returns slug of an admin template to be used.

pynotify.notify.notify(recipients, title=None, text=None, trigger_action=None, related_objects=None, extra_data=None, template_slug=None, dispatcher_classes=None)[source]

Helper method to create a notification. Simply sends the notify_signal.

pynotify.receivers

class pynotify.receivers.AsynchronousReceiver(handler_class)[source]

Bases: pynotify.receivers.BaseReceiver

Signal receiver that calls notification handler asynchronously via Celery.

receive(signal_kwargs)[source]

This method should implement passing signal_kwargs to the handler.

class pynotify.receivers.BaseReceiver(handler_class)[source]

Bases: object

Base class for receiving signals. Its purpose is to pass signal kwargs to the notification handler.

receive(signal_kwargs)[source]

This method should implement passing signal_kwargs to the handler.

class pynotify.receivers.SynchronousReceiver(handler_class)[source]

Bases: pynotify.receivers.BaseReceiver

Signal receiver that calls notification handler synchronously.

receive(signal_kwargs)[source]

This method should implement passing signal_kwargs to the handler.

pynotify.serializers

class pynotify.serializers.BaseSerializer[source]

Bases: object

Base class for serializing/deserializing signal kwargs. Its puprose is to transform signal kwargs to be directly JSON serializable (for compatible types, see https://docs.python.org/3/library/json.html#py-to-json-table).

deserialize(signal_kwargs)[source]

This method should return deserialized signal_kwargs.

serialize(signal_kwargs)[source]

This method should return serialized signal_kwargs.

class pynotify.serializers.ModelSerializer[source]

Bases: object

Serializes any model instance into its PK and ContentType PK and deserializes by fetching the model instance from database. Works recursively on nested dicts and iterables. Values that are not model instances are left intact.

pynotify.tasks

pynotify.tasks.notification_task(self, *args, **kwargs)[source]

Celery task used in asynchronous mode. Just passes any arguments to process_task function.