All Projects → tj-django → django-model-subscription

tj-django / django-model-subscription

Licence: MIT license
Subscribe to django model changes. Using thread safe subscribers.

Programming Languages

python
139335 projects - #7 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to django-model-subscription

Linq2db
Linq to database provider.
Stars: ✭ 2,211 (+11536.84%)
Mutual labels:  bulk
state inspector
State change & method call logger. A debugging tool for instance variables and method calls.
Stars: ✭ 24 (+26.32%)
Mutual labels:  observer-pattern
ParkingDemo
Taipei City Parking Lot Information Query System Demo
Stars: ✭ 18 (-5.26%)
Mutual labels:  observer-pattern
SwiftObserver
Elegant Reactive Primitives for Clean Swift Architecture #NoRx
Stars: ✭ 14 (-26.32%)
Mutual labels:  observer-pattern
OOP-Design-Patterns
MET CS665 - OOP Design Patterns Code Examples
Stars: ✭ 74 (+289.47%)
Mutual labels:  observer-pattern
wordpress-plugin
Speed up your WordPress website. Optimize your JPEG and PNG images automatically with TinyPNG.
Stars: ✭ 78 (+310.53%)
Mutual labels:  bulk
multi-gitter
CLI to update multiple repositories in bulk
Stars: ✭ 99 (+421.05%)
Mutual labels:  bulk
electron-download-manager
Manage downloadItems from Electron's BrowserWindows without user interaction, allowing single file download and bulk downloading
Stars: ✭ 113 (+494.74%)
Mutual labels:  bulk
pmod
Native cross platform library with language projection support for native code.
Stars: ✭ 22 (+15.79%)
Mutual labels:  observer-pattern
WPWatcher
Wordpress Watcher is a wrapper for WPScan that manages scans on multiple sites and reports by email and/or syslog. Schedule scans and get notified when vulnerabilities, outdated plugins and other risks are found.
Stars: ✭ 34 (+78.95%)
Mutual labels:  bulk
MusicPlayer
Android music player example.
Stars: ✭ 20 (+5.26%)
Mutual labels:  observer-pattern
reactive state
An easy to understand reactive state management solution for Flutter.
Stars: ✭ 19 (+0%)
Mutual labels:  observer-pattern
minimalist
Observable Property and Signal for building data-driven UI without Rx
Stars: ✭ 88 (+363.16%)
Mutual labels:  observer-pattern
design-patterns-php
All Design Patterns Samples in PHP
Stars: ✭ 43 (+126.32%)
Mutual labels:  observer-pattern
ARF-Converter
Bulk ARF file converter
Stars: ✭ 15 (-21.05%)
Mutual labels:  bulk
Efcore.bulkextensions
Entity Framework Core Bulk Batch Extensions for Insert Update Delete Read (CRUD), Truncate and SaveChanges operations on SQL Server, PostgreSQL, SQLite
Stars: ✭ 2,295 (+11978.95%)
Mutual labels:  bulk
Mr.Dclutterer
A minimal looking cross-platform desktop application made with Electron that handles quick file aggregation and bulk renaming.
Stars: ✭ 32 (+68.42%)
Mutual labels:  bulk
elasticbulk
Add data in bulk to elasticsearch. It supports data streaming from PostgreSQL or Filesystem
Stars: ✭ 27 (+42.11%)
Mutual labels:  bulk
Games
The Games app has two features which are listing and showing detail of games.
Stars: ✭ 15 (-21.05%)
Mutual labels:  observer-pattern
django-clone
Controlled Django model instance replication.
Stars: ✭ 89 (+368.42%)
Mutual labels:  django-model

PyPI Actions Status Documentation Status

Codacy Badge Codacy Badge codecov PyPI - License

Total alerts Language grade: Python pre-commit.ci status

PyPI - Python Version PyPI - Django Version Downloads

django-model-subscription

Subscribe to django model changes include bulk create/update/delete.

Table of contents

Features

  • 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_DISABLE_SUBSCRIBERS is True which prevents having to mock subscribers that call external services in testing or local development environments.
  • Show changes to the instance after it has been updated i.e diff's the initial state and the current state.

Subscriber

Installation

$ pip install django-model-subscription

Add model_subscription to your INSTALLED_APPS

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

Usage

Using the SubscriptionModelMixin and SubscriptionQuerySet
from model_subscription.mixin import SubscriptionModelMixin
from model_subscription.model import SubscriptionQuerySet


class TestModel(SubscriptionModelMixin, models.Model):
    name = models.CharField(max_length=255)

    objects = SubscriptionQuerySet.as_manager()
Subclassing the SubscriptionModel base class.
from model_subscription.model import SubscriptionModel


class TestModel(SubscriptionModel):
    name = models.CharField(max_length=255)

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'

Credits

If you feel generous and want to show some extra appreciation:

Buy me a coffee

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].