Git Product home page Git Product logo

django-stored-messages's Introduction

Django Stored Messages

image

image

Documentation Status

image

Django contrib.messages on steroids

The app integrates smoothly with Django's messages framework (django.contrib.messages), but users can decide which messages have to be stored on the database backend and kept available over sessions.

Features

  • Seamless integration with django.contrib.messages
  • All the features are in a mixin you can attach to your existing storage
  • Stored messages are archived in the database or in a Redis instance
  • Users can configure which message levels have to be persisted
  • REST api to retrieve and mark messages as read (needs djangorestframework)
  • Signalling api to perform actions in response to messages activity

Compatibility table

  • Python 3.11
  • Django 4.0
  • Django Rest Framework >= 3.3 (only if you want to use REST endpoints)

Do you use an earlier version of Django or Django Rest Framework? An old version of stored_messages is available even if it's not supported anymore. Anyway, plan a migration to a newer version.

Documentation

The full documentation is at http://django-stored-messages.rtfd.org. It includes migration instructions if you're migrating from an earlier version of stored_messages.

Quickstart

Follow instruction for firing up django.contrib.messages, then install the app:

$ pip install django-stored-messages

Add it to the installed apps:

INSTALLED_APPS = (
    # ...
    'stored_messages',
)

In the settings, tell Django which is the message storage:

MESSAGE_STORAGE = 'stored_messages.storage.PersistentStorage'

As last step, don't forget to run Django migrations:

$ python manage.py migrate

Then use it in a project through the django.contrib.messages api. The app provides for convenience some message levels which are persisted by default:

import stored_messages

from django.contrib import messages

# standard message
messages.add_message(request, messages.INFO, 'Hello world.')
# this will be persisted and marked as 'unread'
messages.add_message(request, stored_messages.STORED_INFO, 'Hello world, to the database!')

stored_messages expose the same api as well, so one can do:

import stored_messages
stored_messages.add_message(request, stored_messages.INFO, 'Hello!')

If you want to use standard message levels but persist the messages, just add something like this to the settings:

from django.contrib import messages

STORED_MESSAGES = {
    # persist standard infos and standard errors
    'STORE_LEVELS': (messages.INFO, messages.ERROR,),
}

Iterating the messages will automatically mark them as read (but still persisted):

storage = messages.get_messages(request)
for unread_message in storage:
    # unread_message could be a stored message or a "standard" Django message
    do_something_with(unread_message)

...unless you mark the storage as not used:

storage.used = False

You can mark a stored message as read at any time:

stored_messages.mark_read(request.user, message)

Want to store your messages on Redis instead of your database? Here you go:

STORED_MESSAGES = {
    'STORAGE_BACKEND': 'stored_messages.backends.RedisBackend',
    'REDIS_URL': 'redis://localhost:6379/0',
}

Examples

GitHub-like notifications with Stored Messages and AngularJS

django-stored-messages's People

Contributors

avimeir avatar dnivra avatar evlanov-alex avatar extremoburo avatar gadybadger avatar jnishiyama avatar masci avatar mrabedini avatar palazzem avatar pmbadger avatar rasca avatar stard0g avatar synasius avatar xrmx avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.