Skip to main content

Django Signals extension for unifying data passed to receivers.

Project description

Django Unified Signals

Continuous Integration

This package extends behavior of the Django Signals, by unifying the data passed when the signal is sent. That way both: the sender and the receiver can be sure that the data passed is always of the same type.

Django Signals are not very strict on the data that can be passed with send method. Basically anything can be passed as the arguments and receivers need to be aware of that from the project documentation. It gets even worse when on one side the signal send params change and the receivers' maintainers may not be aware of that. It gets even worse, when using send_robust method, which ignores all exceptions and any errors may pass without being noticed.

Using this package you have to define the message type class which object
is always expected to be passed when sending the signal. That way receiver knows what type of message will be received. This package automates the process of checking if the send message is following the contract.

Installation

The package is available on PyPI:

pip install django-unified-signals

Usage

Let's start by defining the message structure. It can be any class you want. In the example we will use dataclass:

import dataclasses

@dataclasses.dataclass
class UserMessage:
    name: str
    age: int

Now that we have the message structure defined, we can create the signal:

from unified_signals import UnifiedSignal

user_deactivated_signal = UnifiedSignal(UserMessage)

It extends the standard django.dispatch.Signal class, so it can be used in the same way.

user_deactivated_signal.send(sender, UserMessage(name='John', age=30))

The receiver can be defined in the same way as for the standard Django Signal:

@receiver(user_deactivated_signal)
def handle_user_deactivated(sender, message: UserMessage, **kwargs):
    print(message.name)
    print(message.age)
    ...

The difference is that the message is always of the same type, so the receiver can be sure that the message is always of the same type. If the message is not of the expected type when sending the signal, the unified_signals.exceptions.UnifiedSignalMessageTypeError exception will be raised.

user_deactivated_signal.send(sender, 'not a message') # raises UnifiedSignalMessageTypeError

Project details


Download files

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

Source Distribution

django_unified_signals-0.2.1.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

django_unified_signals-0.2.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file django_unified_signals-0.2.1.tar.gz.

File metadata

  • Download URL: django_unified_signals-0.2.1.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.2 Windows/11

File hashes

Hashes for django_unified_signals-0.2.1.tar.gz
Algorithm Hash digest
SHA256 1350551e64662bbb3514464619b1d5808711aa60ad9f33f6bd0f3cb4e6e3fe5b
MD5 98616e139004962031eb81ec0f604b54
BLAKE2b-256 1c3f8132c1e18dafa0f1b88d5738be1ad4197a1b19cb39cce84ea88c283b965b

See more details on using hashes here.

File details

Details for the file django_unified_signals-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_unified_signals-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 45a0c2b64e8a3db7f58cee55cd3ab75c40dcdbec09f118a4ee51ddf26ab50752
MD5 da80fa2e073854500ab9f049576697b0
BLAKE2b-256 52c4b0931453be880e391fd6d42d8f2207269458bf55e302d7d8c140c3b8ecb7

See more details on using hashes here.

Supported by

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