Skip to main content

Offline addon for django.contrib.messages

Project description

https://travis-ci.org/matthiask/django-user-messages.svg?branch=master

django-user-messages adds offline messaging support to Django’s messaging framework. It achieves this by allowing to save messages in the database. The user_messages.api.get_messages utility and the context processor mentioned above transparently merge Django’s messages and our own messages in a single list, therefore existing code works without any changes and without causing database writes. django-user-messages’ functions have to be used explicitly. I consider this a feature, not a bug.

It is PostgreSQL-only because Django currently only has builtin support for PostgreSQL’s JSON field.

Installation

  • Install django-user-messages using pip into your virtualenv.

  • Add user_messages to INSTALLED_APPS and run migrate.

  • Replace the default messages context processor with user_messages.context_processors.messages. The context processor provides both django.contrib.messages’ and django-user-messages’ messages.

  • Use user_messages.api as you would use django.contrib.messages except that you pass the user model or ID as first parameter, not the current request.

Usage

Pretty much the same as Django’s messaging framework:

from user_messages import api

api.info(user, 'Hey there')
api.warning(user, 'Stop this')
api.error(user, 'Not nice!')

# Passing the ID is also possible; the user instance does not
# have to be instantiated at all:
api.success(user.id, 'Yay!')

django-user-messages’ messages supports two additional features not available in Django’s messages framework:

  • Messages can be delivered more than once by passing deliver_once=False. These messages have to be acknowledged explicitly. django-user-messages does not contain any code to do this.

  • It is possible to attach additional data by passing a dictionary as meta:

    api.debug(user, 'Oww', meta={
        'url': 'http://example.com',
    })

For convenience, our messages have the same tags and level_tag properties as Django’s messages. Meta properties are also accessible in templates:

{% if messages %}
  <ul class="messages">
    <li class="{{ message.tags }}".>
      {% if message.meta.url %}<a href="{{ message.meta.url }}">{% endif %}
      {{ message }}
      {% if message.meta.url %}</a>{% endif %}
    </li>
  </ul>
{% endif %}

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-user-messages-0.3.1.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

django_user_messages-0.3.1-py2.py3-none-any.whl (9.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-user-messages-0.3.1.tar.gz.

File metadata

File hashes

Hashes for django-user-messages-0.3.1.tar.gz
Algorithm Hash digest
SHA256 9ccffc7d64cba7133d5ec3baf00c7f5d12d571926daf6cbcdf6ab4956287c8da
MD5 dcca12e0c4f736d0ea42c9b5044cdfdb
BLAKE2b-256 8bc658f2c2a1635b67e82bcf5645a96b9aaba7ffaf79f44aee60583c874c5ff2

See more details on using hashes here.

File details

Details for the file django_user_messages-0.3.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_user_messages-0.3.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 82d54bb64cc49a6ac6451723fe02a53d13a4bc73842f07cabc83b1ff0cec100b
MD5 f729b96236e01284278c166c09e34449
BLAKE2b-256 a8b89c38fe88fd6c6eb064dc5c371b49769c31f0a8d0597143a737f370855eaa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page