1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fixed #18658 -- Improved ModelAdmin.message_user API

Thanks to Lowe Thiderman for the patch and tests
This commit is contained in:
Preston Holmes
2012-11-17 22:53:31 +01:00
parent 8b659e439b
commit edf7ad36fa
8 changed files with 133 additions and 7 deletions

View File

@@ -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):
"""