Skip to main content

Send asynchronous messages to users (eg from offline scripts). Useful for integration with Celery.

Project description

Simple asynchronous messages for django. Plays nicely with Celery. Fork of https://github.com/codeinthehole/django-async-messages

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.

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?

  • A user may miss the message if they navigating quickly between pages. But this is a general problem of the Django messages framework.

Install

From PyPI (stable):

pip install django-async-messages-redux

Add 'async_messages.middleware.AsyncMiddleware' to your MIDDLEWARE or 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. Note that local memory caching is not suitable as each process has its own private cache and a Celery task can’t communicate with the webserver process cache.

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)

Send multiple messages 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 queued up for processing")
>>> # do more awesome stuff
>>> message_user(barry, "Barry, your report is ready")

Alternative way to send a message to a single user, imitating the django.contrib.messages API:

>>> from async_messages import messages
>>> barry = User.objects.get(username='barry')
>>> messages.debug(barry, "Barry was here")
>>> messages.info(barry, "Hi, Barry")
>>> messages.success(barry, "Barry, your report is ready")
>>> messages.warning(barry, "Barry, you didn't lock your session")
>>> messages.error(barry, "You are not Barry")

Contributing

Fork, clone and create a virtualenv. Then run:

make install

Run tests with:

python manage.py test tests

Please submit pull requests using ‘develop’ as the target branch.

License

MIT

Changelog

0.4.1

  • Improvements to Django 2+ and Python 3+ support

0.4.0

  • Forked, added support for up to Django 2.0

0.3.1

  • Fix bug around request instances that don’t have a user attribute

0.3

  • Mimic django.contrib.messages API for sending a message to a user

0.2

  • Added possibility to queue multiple messages

0.1.2

  • Altered dependency on Django to be only 1.2+

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-redux-0.4.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distributions

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

django_async_messages_redux-0.4.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

django_async_messages_redux-0.4.1-py2-none-any.whl (7.5 kB view details)

Uploaded Python 2

File details

Details for the file django-async-messages-redux-0.4.1.tar.gz.

File metadata

  • Download URL: django-async-messages-redux-0.4.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.9.1 pkginfo/1.4.1 requests/2.18.4 setuptools/40.3.0 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/2.7.14

File hashes

Hashes for django-async-messages-redux-0.4.1.tar.gz
Algorithm Hash digest
SHA256 5ec913fdf70ca6657b59ab681c0d1b84892043e8c0785cc2ad313f721d414f86
MD5 a95b464d0517435d19fc743d66ec46d1
BLAKE2b-256 d281f1887e4494222c80b3d6d96506924901bd0349f7678879c53e2f22992a36

See more details on using hashes here.

File details

Details for the file django_async_messages_redux-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: django_async_messages_redux-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.9.1 pkginfo/1.4.1 requests/2.18.4 setuptools/40.3.0 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/2.7.14

File hashes

Hashes for django_async_messages_redux-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ff72d5070e9c2a1073c8661fd4a05b65c4c57c1c2f85d97a3d9d0cd83e0c4769
MD5 b25d7cfaef93b22fe33adf5b87042dac
BLAKE2b-256 a3429f7164c17687f886e143c75f8e41e179f442529cb75d4c8294966e5a0e8f

See more details on using hashes here.

File details

Details for the file django_async_messages_redux-0.4.1-py2-none-any.whl.

File metadata

  • Download URL: django_async_messages_redux-0.4.1-py2-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.9.1 pkginfo/1.4.1 requests/2.18.4 setuptools/40.3.0 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/2.7.14

File hashes

Hashes for django_async_messages_redux-0.4.1-py2-none-any.whl
Algorithm Hash digest
SHA256 38c35b741bb235b474a00791b31bd14ec07d61ec845b9b2e0ca60baae5e2d67b
MD5 979e2eb3be78d1245f051793b5c5fa88
BLAKE2b-256 58f2143acebef4643cf1c8f1514cab7b33d2b177d8a7afde365f0d3254c9fa0d

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