Giter Site home page Giter Site logo

django-user-messages's Introduction

django-user-messages - Offline addon for django.contrib.messages

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 user_messages.context_processors.messages context processor transparently concatenate 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.

Installation

  • Install django-user-messages using pip into your virtualenv.
  • Add user_messages to INSTALLED_APPS and run migrate.
  • Add the user_messages.context_processors.messages message processor somewhere after the default messages processor. Django's admin app checks for the presence of the latter so you cannot simply remove it (except if you want to silence the "admin.E404" system check).
  • 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">
  {% for message in messages %}
    <li class="{{ message.tags }}".>
      {% if message.meta.url %}<a href="{{ message.meta.url }}">{% endif %}
      {{ message }}
      {% if message.meta.url %}</a>{% endif %}
    </li>
  {% endfor %}
  </ul>
{% endif %}

django-user-messages' messages are also evaluated lazily.

django-user-messages's People

Contributors

matthiask avatar seantrue avatar

Stargazers

David Cooper avatar Nikolaus Schlemm avatar Luis Correa B. avatar  avatar Henri Dickson avatar  avatar Vladimir Myshkovski avatar Thierry BOULOGNE avatar Willis Allstead avatar Jacob Chen avatar Darlin Alberto avatar beres avatar Robert avatar Jannik Waschkau avatar  avatar Wai Yi Anthony Leung avatar Mohammad Adil avatar Mike Dingjan avatar Jay Modi avatar Eric Goller avatar kakulukia avatar Jens Diemer avatar Markus Zapke-Gründemann avatar 阳阳 avatar Saurabh Kumar avatar Jannis Leidel avatar

Watchers

 avatar James Cloos avatar  avatar

django-user-messages's Issues

Message to multiple User/All User

Hello,
thanks for the Awesome Plugin. Works realy nice. Is there a way to send Message for alle Users?

Would be very nice.

Thanks for sharing :)

Unicode Support

When using unicode messages, displaying them causes a UnicodeEncodeError.

Cheers

Broken for Django 5.1.x

Hi there,

I installed the latest version of django-user-messages for this GitHub repository, and obverse a broken migration on Django 5.1.x. If I revert back to Django 5.0.x it works file. I believe the error is due to the removal a model's Meta.index_together option.

Here's my error,

root@081059858a5c:/app# ./manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, constance, contenttypes, django_file_form, sessions, tomato, user_messages
Traceback (most recent call last):
  File "/app/./manage.py", line 24, in <module>
    main()
  File "/app/./manage.py", line 20, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.12/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.12/site-packages/django/core/management/base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.12/site-packages/django/core/management/base.py", line 459, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/core/management/base.py", line 107, in wrapper
    res = handle_func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/core/management/commands/migrate.py", line 303, in handle
    pre_migrate_apps = pre_migrate_state.apps
                       ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/utils/functional.py", line 47, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
                                         ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/migrations/state.py", line 565, in apps
    return StateApps(self.real_apps, self.models)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/migrations/state.py", line 626, in __init__
    self.render_multiple([*models.values(), *self.real_models])
  File "/usr/local/lib/python3.12/site-packages/django/db/migrations/state.py", line 664, in render_multiple
    model.render(self)
  File "/usr/local/lib/python3.12/site-packages/django/db/migrations/state.py", line 957, in render
    return type(self.name, bases, body)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/models/base.py", line 143, in __new__
    new_class.add_to_class("_meta", Options(meta, app_label))
  File "/usr/local/lib/python3.12/site-packages/django/db/models/base.py", line 371, in add_to_class
    value.contribute_to_class(cls, name)
  File "/usr/local/lib/python3.12/site-packages/django/db/models/options.py", line 220, in contribute_to_class
    raise TypeError(
TypeError: 'class Meta' got invalid attribute(s): index_together

Thanks!

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.