Welcome to django-modelsubscription’s documentation!

django-modelsubscriptio allows you to subscribe users or emails to your Django models modify events

Installation

pip install django-modelsubscription

Configuration

Add modelsubscription and django.contrib.sites to your INSTALLED_APPS

INSTALLED_APPS = (
    'django.contrib.sites',
    'modelsubscription'
)

Usage

Add to your subscritable models the mixin modelsubscription.models.SubscriptableMixin as parent class

from modelsubscription.models import SubscriptableMixin
from django.db import models

class MyModel(models.Model, SubscriptableMixin):
    ...

To subscribe an user to an object use modelsubscription.modelsSubscriptableMixin.subscribe_user

obj = MyModel.objects.get(pk=1)
obj.subscribe_user(request.user, 'email')

To subscribe an email to an object use modelsubscription.modelsSubscriptableMixin.subscribe_email

obj = MyModel.objects.get(pk=1)
obj.subscribe_email('foo@bar.com', 'email')

To unsubscribe an user to an object use modelsubscription.modelsSubscriptableMixin.unsubscribe_user

obj = MyModel.objects.get(pk=1)
# Unsuscribe one type of subscription
obj.unsubscribe_user(request.user, 'email')

# Unsuscribe to all types of subscriptiosn
obj.unsubscribe_user(request.user)

To unsubscribe an email to an object use modelsubscription.modelsSubscriptableMixin.unsubscribe_email

obj = MyModel.objects.get(pk=1)
# Unsuscribe one type of subscription
obj.unsubscribe_email('foo@bar.com', 'email')

# Unsuscribe to all types of subscriptiosn
obj.unsubscribe_email('foo@bar.com')

To run the subscription handlers you can do it automatically saving the object if settings.SUBSCRIPTION_RUN_ON_CHANGE is True

obj = MyModel.objects.get(pk=1)
obj.save()

To run the subscription handlers you can do it manually running

obj = MyModel.objects.get(pk=1)
# for all subscriptions
for subscription in obj.subscriptions.all():
    subscription.run(obj)

# for one subscriptions
obj.subscriptions.all()[1].run(obj)

By default modelsubscription.handlers.emailhandler will render modelsubscription/email/subject.txt and modelsubscription/email/body.txt for the email.

Settings

There are a couple of editable settings

SUBSCRIPTION_EMAIL_FROM
Default :settings.DEFAULT_FROM_EMAIL
Type :str

The email address used to send subscription messages from.

SUBSCRIPTION_RUN_ON_CHANGE
Default :True
Type :boolean

Run automatically the subscriptions handlers on subscriptable objects save.

SUBSCRIPTION_TYPES
Default :{ ‘email’: {‘handlers’: ‘modelsubscription.handlers.emailhandler.EmailHandler’, ‘extra_args’: {}}, ‘db’: {‘handler’: ‘modelsubscription.handlers.dbhandler.DbHandler’, ‘extra_args’: {}} }
Type :dict

Dictionary defining the subscription types

API

Made by Kaleidos.

Project Versions

Table Of Contents

Next topic

Models

This Page