Subscription model for a django model instance.
Project description
https://python-3-patterns-idioms-test.readthedocs.io/en/latest/Observer.html
Table of contents
- Motivation
- Installation
- Usage
- Decorators
- Setup Subscribers using App.ready
- Setup Subscribers with auto discovery
Motivation
- Extending django models using Observer Pattern.
- Decouple Business logic from Models.save
- Support for bulk actions (Not available using django signals.)
- Use noop subscribers when
settings.SUBSCRIPTION_RUN_EXTERNAL
isFalse
which prevents having to mock external service subscribers 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.
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 App.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'
TODO's
- Supporting field level subscriptions.
- Support class based subscribers which implements
__call__
- Extend to include custom OperationType.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for django-model-subscription-0.0.9.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e4952424fa91c4ec27e2103dbeea56d99eff95f18212408c6001ed505515391d |
|
MD5 | c53cb086ab6080695a5a53b489773a5c |
|
BLAKE2b-256 | 3c50e88eea869892ef041e2afd4e22751b7f4732a3e0567d4a733d1432bb0396 |
Close
Hashes for django_model_subscription-0.0.9-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | af445465fbf6c76cbd9d21de0b32e63ec42ff39c1e08f1b9fbccc545c1c0a5bf |
|
MD5 | 7f86268275dcae905f4c89c15247dc9a |
|
BLAKE2b-256 | 5b2d5b2b15ca0b8566299a9bfbeadb9a07dd5f5e6f3f305438761567dfac43a3 |