Skip to main content

Send asynchronous messages to users. Useful for integration with Celery

Project description

Simple asynchronous messages for django.

Questions

What problem does this solve?

Suppose a user instigates an expensive task that you are processing offline (eg using Celery). This library provides a simple mechanism for notifying the user when the task is finished, utilising Django’s messaging framework.

What’s an example?

You might use Celery to generate a large report and then employ this library to notify the user that their report is ready to be downloaded. The user will see the message you’ve sent them when they make their next request after the message is submitted.

How does it work?

A cache is used to persist the messages, and middleware is used to pick these up and submit them to Django’s messaging framework. All very simple.

Aren’t there other libraries that do this?

Yes, there are - but they solve the problem in different ways:

What’s good about this implementation?

  • It’s simple, fast and easy to use.

  • It works cohesively with existing Django cache and messages framework. It will work no matter what cache backend your are using, and whatever storage backend is used for messages.

What’s bad?

  • Only one message per user at the moment, lists of messages to come later.

  • A user may miss the message if they navigating quickly between pages.

Install

From PyPI (stable):

pip install django-async-messages

From Github (unstable):

pip install git+git://github.com/codeinthehole/django-async-messages#egg=djang-async-messages

Add 'async_messages.middleware.AsyncMiddleware' to your MIDDLEWARE_CLASSES. Ensure it comes after 'django.contrib.messages.middleware.MessageMiddleware'.

You need to have CACHES configured in you settings for this to work. As usual, memcache is the best choice.

Use

Send a message to a single user:

>>> from async_messages import message_user
>>> from django.contrib.auth.models import User
>>> barry = User.objects.get(username='barry')
>>> message_user(barry, "Barry, your report is ready")

Send a message to lots of users:

>>> from async_messages import message_users
>>> staff = User.objects.filter(is_staff=True)
>>> message_users(staff, "All budgets must be spent by the end of the day")

Specify message level:

>>> from django.contrib.messages import constants
>>> message_users(staff, "Boom!", constants.WARNING)

Changelog

0.1.1

  • Altered middleware to use process_response.

  • Better docstrings

0.1

  • Minimum viable product

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-async-messages-0.1.1.tar.gz (3.2 kB view details)

Uploaded Source

File details

Details for the file django-async-messages-0.1.1.tar.gz.

File metadata

File hashes

Hashes for django-async-messages-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e47aea31109feb0ac9f855712bb6190701862a9855db26f7b1fb642d0ed458df
MD5 faf7799284ac5e09b8daed6cb9f1a449
BLAKE2b-256 71ae2a295c4aa4c321d02a663a24cb42a594e67e380c61603917c92154a7ba96

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