Skip to main content

Subscription model for a django model instance.

Project description

CircleCI Actions Status Documentation Status PyPI - License PyPI - Python Version PyPI - Django Version

django-model-subscription

Sreenshot

Table of contents

Motivation

  • Using Observer Pattern notify subscribers about changes to a django model.
  • Decouple Business logic from Models.save
  • Support for bulk actions (Not available using django signals.)
  • Use noop subscribers when settings.SUBSCRIPTION_RUN_EXTERNAL is False which prevents having to mock subscribers that call external services in testing, local development environments.
  • Show changes to the instance after it has been updated i.e diff's the initial state and the current state.

Screenshot

Installation

$ pip install django-model-subscription

Add model_subscription to your INSTALLED_APPS

INSTALLED_APPS = [
    ...,
    'model_subscription',
    ...
]

Usage

Creating subscribers.

  • Using OperationType
import logging
from model_subscription.decorators import subscribe
from model_subscription.constants import OperationType

log = logging.getLogger(__name__)

@subscribe(OperationType.CREATE, TestModel)
def handle_create(instance):
    log.debug('Created {}'.format(instance.name))
  • Using create_subscription directly (succinct version).
import logging
from model_subscription.decorators import create_subscription

log = logging.getLogger(__name__)

@create_subscription(TestModel)
def handle_create(instance):
    log.debug('Created {}'.format(instance.name))

Decorators

  • subscribe: Explicit (Requires a valid OperationType).

(Create, Update, Delete) operations.

  • create_subscription: Subscribes to create operation i.e a new instance.
@create_subscription(TestModel)
def handle_create(instance):
    log.debug('1. Created {}'.format(instance.name))
  • update_subscription: Subscribes to updates also includes (changed_data).
@update_subscription(TestModel)
def handle_update(instance, changed_data):
    log.debug('Updated {} {}'.format(instance.name, changed_data))
  • delete_subscription: Subscribes to delete operation:

NOTE: The instance.pk is already set to None.

@delete_subscription(TestModel)
def handle_delete(instance):
    log.debug('Deleted {}'.format(instance.name))

(Bulk Create, Bulk Update, Bulk Delete) operations.

  • bulk_create_subscription: Subscribe to bulk create operations.
@bulk_create_subscription(TestModel)
def handle_bulk_create(instances):
    for instance in instances:
        log.debug('Bulk Created {}'.format(instance.name))
  • bulk_update_subscription: Subscribe to bulk update operations.
@bulk_update_subscription(TestModel)
def handle_bulk_update(instances):
    for instance in instances:
        log.debug('Updated {}'.format(instance.name))
  • bulk_delete_subscription: Subscribe to bulk delete operations.
@bulk_delete_subscription(TestModel)
def handle_bulk_delete(instances):
    for instance in instances:
        log.debug('Deleted {}'.format(instance.name))

Setup Subscribers using AppConfig.ready (Recomended).

Update you apps.py

from django.apps import AppConfig


class MyAppConfig(AppConfig):
    name = 'myapp'

    def ready(self):
        from myapp import subscriptions

Setup Subscribers using auto discovery.

By default the settings.SUBSCRIPTION_AUTO_DISCOVER is set to False.

To use auto discovery this is not recommended as it would notify the subscribers wherever the model is used i.e IPython notebook, external scripts.

In your settings.py add

SUBSCRIPTION_AUTO_DISCOVER = True

Setting up the SUBSCRIPTION_MODULE

NOTE: This is only required when SUBSCRIPTION_AUTO_DISCOVER = True

SUBSCRIPTION_MODULE  = 'subscription' 

Resources

TODO's

  • Supporting field level subscriptions.
  • Support class based subscribers which implements __call__
  • Extend to include custom OperationType.
  • Add support for using a single class to manage multiple actions i.e MyClass.update, MyClass.create.

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-model-subscription-0.0.10.tar.gz (15.2 kB view details)

Uploaded Source

Built Distribution

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

django_model_subscription-0.0.10-py2.py3-none-any.whl (15.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-model-subscription-0.0.10.tar.gz.

File metadata

  • Download URL: django-model-subscription-0.0.10.tar.gz
  • Upload date:
  • Size: 15.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.17 CPython/3.7.3 Darwin/19.3.0

File hashes

Hashes for django-model-subscription-0.0.10.tar.gz
Algorithm Hash digest
SHA256 14a4d53b5b0fcd01ceb7f1b5d13382043cdc3bdd0092ae670873b50be2e75e1f
MD5 638d8c7c5761265c6da7648f284dcdc8
BLAKE2b-256 e5392d9ff77bb1a0850e8e082379f52099a353fe715e99c0ec2c58e1af4dbc42

See more details on using hashes here.

File details

Details for the file django_model_subscription-0.0.10-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_model_subscription-0.0.10-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3d0890590839f061813897728643fe8feb24e2c0fd9d7a62086ab3f29e9536b4
MD5 9a7393416b5deaa58ae57cbf0303d1ff
BLAKE2b-256 4d38c935a1a5b69de4043609390faf12b1ade6ece535c00612d1f19ef3fd86db

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