1
0
mirror of https://github.com/django/django.git synced 2025-01-08 17:37:20 +00:00

Fixed #13376: Added kwargs argument to messages api to allow easier extension of functionality

This commit is contained in:
Andrew Miller 2024-07-02 13:03:52 +01:00
parent aa74c4083e
commit 016cabdc3a
2 changed files with 16 additions and 10 deletions

View File

@ -19,7 +19,7 @@ class MessageFailure(Exception):
pass
def add_message(request, level, message, extra_tags="", fail_silently=False):
def add_message(request, level, message, extra_tags="", fail_silently=False, **kwargs):
"""
Attempt to add a message to the request using the 'messages' app.
"""
@ -37,7 +37,7 @@ def add_message(request, level, message, extra_tags="", fail_silently=False):
"django.contrib.messages.middleware.MessageMiddleware"
)
else:
return messages.add(level, message, extra_tags)
return messages.add(level, message, extra_tags, **kwargs)
def get_messages(request):
@ -72,7 +72,7 @@ def set_level(request, level):
return True
def debug(request, message, extra_tags="", fail_silently=False):
def debug(request, message, extra_tags="", fail_silently=False, **kwargs):
"""Add a message with the ``DEBUG`` level."""
add_message(
request,
@ -80,10 +80,11 @@ def debug(request, message, extra_tags="", fail_silently=False):
message,
extra_tags=extra_tags,
fail_silently=fail_silently,
**kwargs,
)
def info(request, message, extra_tags="", fail_silently=False):
def info(request, message, extra_tags="", fail_silently=False, **kwargs):
"""Add a message with the ``INFO`` level."""
add_message(
request,
@ -91,10 +92,11 @@ def info(request, message, extra_tags="", fail_silently=False):
message,
extra_tags=extra_tags,
fail_silently=fail_silently,
**kwargs,
)
def success(request, message, extra_tags="", fail_silently=False):
def success(request, message, extra_tags="", fail_silently=False, **kwargs):
"""Add a message with the ``SUCCESS`` level."""
add_message(
request,
@ -102,10 +104,11 @@ def success(request, message, extra_tags="", fail_silently=False):
message,
extra_tags=extra_tags,
fail_silently=fail_silently,
**kwargs,
)
def warning(request, message, extra_tags="", fail_silently=False):
def warning(request, message, extra_tags="", fail_silently=False, **kwargs):
"""Add a message with the ``WARNING`` level."""
add_message(
request,
@ -113,10 +116,11 @@ def warning(request, message, extra_tags="", fail_silently=False):
message,
extra_tags=extra_tags,
fail_silently=fail_silently,
**kwargs,
)
def error(request, message, extra_tags="", fail_silently=False):
def error(request, message, extra_tags="", fail_silently=False, **kwargs):
"""Add a message with the ``ERROR`` level."""
add_message(
request,
@ -124,4 +128,5 @@ def error(request, message, extra_tags="", fail_silently=False):
message,
extra_tags=extra_tags,
fail_silently=fail_silently,
**kwargs,
)

View File

@ -12,10 +12,11 @@ class Message:
or template.
"""
def __init__(self, level, message, extra_tags=None):
def __init__(self, level, message, extra_tags=None, **kwargs):
self.level = int(level)
self.message = message
self.extra_tags = extra_tags
self.extra_kwargs = kwargs
def _prepare(self):
"""
@ -139,7 +140,7 @@ class BaseStorage:
messages = self._loaded_messages + self._queued_messages
return self._store(messages, response)
def add(self, level, message, extra_tags=""):
def add(self, level, message, extra_tags="", **kwargs):
"""
Queue a message to be stored.
@ -154,7 +155,7 @@ class BaseStorage:
return
# Add the message.
self.added_new = True
message = Message(level, message, extra_tags=extra_tags)
message = Message(level, message, extra_tags=extra_tags, **kwargs)
self._queued_messages.append(message)
def _get_level(self):