Fixed #27688 -- Made messages' add_message() request check use ducktyping.
This commit is contained in:
parent
7dc8d9238a
commit
ed8c0c941d
|
@ -1,6 +1,5 @@
|
||||||
from django.contrib.messages import constants
|
from django.contrib.messages import constants
|
||||||
from django.contrib.messages.storage import default_storage
|
from django.contrib.messages.storage import default_storage
|
||||||
from django.http import HttpRequest
|
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
'add_message', 'get_messages',
|
'add_message', 'get_messages',
|
||||||
|
@ -18,16 +17,21 @@ def add_message(request, level, message, extra_tags='', fail_silently=False):
|
||||||
"""
|
"""
|
||||||
Attempts to add a message to the request using the 'messages' app.
|
Attempts to add a message to the request using the 'messages' app.
|
||||||
"""
|
"""
|
||||||
if not isinstance(request, HttpRequest):
|
try:
|
||||||
raise TypeError("add_message() argument must be an HttpRequest object, "
|
messages = request._messages
|
||||||
"not '%s'." % request.__class__.__name__)
|
except AttributeError:
|
||||||
if hasattr(request, '_messages'):
|
if not hasattr(request, 'META'):
|
||||||
return request._messages.add(level, message, extra_tags)
|
raise TypeError(
|
||||||
if not fail_silently:
|
"add_message() argument must be an HttpRequest object, not "
|
||||||
raise MessageFailure(
|
"'%s'." % request.__class__.__name__
|
||||||
'You cannot add messages without installing '
|
)
|
||||||
'django.contrib.messages.middleware.MessageMiddleware'
|
if not fail_silently:
|
||||||
)
|
raise MessageFailure(
|
||||||
|
'You cannot add messages without installing '
|
||||||
|
'django.contrib.messages.middleware.MessageMiddleware'
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
return messages.add(level, message, extra_tags)
|
||||||
|
|
||||||
|
|
||||||
def get_messages(request):
|
def get_messages(request):
|
||||||
|
|
|
@ -42,3 +42,24 @@ class ApiTests(SimpleTestCase):
|
||||||
def test_middleware_missing_silently(self):
|
def test_middleware_missing_silently(self):
|
||||||
messages.add_message(self.request, messages.DEBUG, 'some message', fail_silently=True)
|
messages.add_message(self.request, messages.DEBUG, 'some message', fail_silently=True)
|
||||||
self.assertEqual(self.storage.store, [])
|
self.assertEqual(self.storage.store, [])
|
||||||
|
|
||||||
|
|
||||||
|
class CustomRequest(object):
|
||||||
|
def __init__(self, request):
|
||||||
|
self._request = request
|
||||||
|
|
||||||
|
def __getattribute__(self, attr):
|
||||||
|
try:
|
||||||
|
return super(CustomRequest, self).__getattribute__(attr)
|
||||||
|
except AttributeError:
|
||||||
|
return getattr(self._request, attr)
|
||||||
|
|
||||||
|
|
||||||
|
class CustomRequestApiTests(ApiTests):
|
||||||
|
"""
|
||||||
|
add_message() should use ducktyping to allow request wrappers such as the
|
||||||
|
one in Django REST framework.
|
||||||
|
"""
|
||||||
|
def setUp(self):
|
||||||
|
super(CustomRequestApiTests, self).setUp()
|
||||||
|
self.request = CustomRequest(self.request)
|
||||||
|
|
Loading…
Reference in New Issue