Commit edf7ad36 authored by Preston Holmes's avatar Preston Holmes
Browse files

Fixed #18658 -- Improved ModelAdmin.message_user API

Thanks to Lowe Thiderman for the patch and tests
parent 8b659e43
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -531,6 +531,7 @@ answer newbie questions, and generally made Django that much better:
    Terry Huang <terryh.tp@gmail.com>
    Travis Terry <tdterry7@gmail.com>
    thebjorn <bp@datakortet.no>
    Lowe Thiderman <lowe.thiderman@gmail.com>
    Zach Thompson <zthompson47@gmail.com>
    Michael Thornhill <michael.thornhill@gmail.com>
    Deepak Thukral <deep.thukral@gmail.com>
+20 −2
Original line number Diff line number Diff line
@@ -691,12 +691,30 @@ class ModelAdmin(BaseModelAdmin):
        change_message = ' '.join(change_message)
        return change_message or _('No fields changed.')

    def message_user(self, request, message):
    def message_user(self, request, message, level=messages.INFO, extra_tags='',
                     fail_silently=False):
        """
        Send a message to the user. The default implementation
        posts a message using the django.contrib.messages backend.

        Exposes almost the same API as messages.add_message(), but accepts the
        positional arguments in a different order to maintain backwards
        compatibility. For convenience, it accepts the `level` argument as
        a string rather than the ususal level number.
        """
        messages.info(request, message)

        if not isinstance(level, int):
            # attempt to get the level if passed a string
            try:
                level = getattr(messages.constants, level.upper())
            except AttributeError:
                levels = messages.constants.DEFAULT_TAGS.values()
                levels_repr = ', '.join('`%s`' % l for l in levels)
                raise ValueError('Bad message level string: `%s`. '
                        'Possible values are: %s' % (level, levels_repr))

        messages.add_message(request, level, message, extra_tags=extra_tags,
                fail_silently=fail_silently)

    def save_form(self, request, form, change):
        """
+9 −0
Original line number Diff line number Diff line
@@ -140,6 +140,15 @@ That's really all there is to it! If you're itching to write your own actions,
you now know enough to get started. The rest of this document just covers more
advanced techniques.

Handling errors in actions
--------------------------

If there are foreseeable error conditions that may occur while running your
action, you should gracefully inform the user of the problem. This means
handling exceptions and and using
:meth:`django.contrib.admin.ModelAdmin.message_user` to display a user friendly
description of the problem in the response.

Advanced action techniques
==========================

+12 −4
Original line number Diff line number Diff line
@@ -1303,11 +1303,19 @@ templates used by the :class:`ModelAdmin` views:
                    return qs
                return qs.filter(author=request.user)

.. method:: ModelAdmin.message_user(request, message)
.. method:: ModelAdmin.message_user(request, message, level=messages.INFO, extra_tags='', fail_silently=False)

    Sends a message to the user. The default implementation creates a message
    using the :mod:`django.contrib.messages` backend. See the
    :ref:`custom ModelAdmin example <custom-admin-action>`.
    Sends a message to the user using the :mod:`django.contrib.messages`
    backend.  See the :ref:`custom ModelAdmin example <custom-admin-action>`.

    .. versionadded:: 1.5

    Keyword arguments allow you to change the message level, add extra CSS
    tags, or fail silently if the ``contrib.messages`` framework is not
    installed. These keyword arguments match those for
    :func:`django.contrib.messages.add_message`, see that function's
    documentation for more details. One difference is that the level may be
    passed as a string label in addition to integer/constant.

.. method:: ModelAdmin.get_paginator(queryset, per_page, orphans=0, allow_empty_first_page=True)

+5 −0
Original line number Diff line number Diff line
@@ -313,6 +313,11 @@ Django 1.5 also includes several smaller improvements worth noting:
  DeprecationWarnings should be printed to the console in development
  environments the way they have been in Python versions < 2.7.

* The API for :meth:`django.contrib.admin.ModelAdmin.message_user` method has
  been modified to accept additional arguments adding capabilities similar to
  :func:`django.contrib.messages.add_message`. This is useful for generating
  error messages from admin actions.

Backwards incompatible changes in 1.5
=====================================

Loading