Python django.contrib.messages() Examples

The following are 21 code examples for showing how to use django.contrib.messages(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.contrib , or try the search function .

Example 1
Project: StormOnline   Author: stormsha   File: base.py    License: Apache License 2.0 5 votes vote down vote up
def message_user(self, message, level='info'):
        """
        Send a message to the user. The default implementation
        posts a message using the django.contrib.messages backend.
        """
        if hasattr(messages, level) and callable(getattr(messages, level)):
            getattr(messages, level)(self.request, message) 
Example 2
Project: weibo-analysis-system   Author: Superbsco   File: base.py    License: MIT License 5 votes vote down vote up
def message_user(self, message, level='info'):
        """
        Send a message to the user. The default implementation
        posts a message using the django.contrib.messages backend.
        """
        if hasattr(messages, level) and callable(getattr(messages, level)):
            getattr(messages, level)(self.request, message) 
Example 3
Project: clist   Author: aropan   File: request_logger.py    License: Apache License 2.0 5 votes vote down vote up
def __getattr__(self, attr):
        ret = getattr(messages, attr)
        if callable(ret):
            ret = partial(ret, self.request_)
        return ret 
Example 4
Project: myblog   Author: myminwang   File: base.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def message_user(self, message, level='info'):
        """
        Send a message to the user. The default implementation
        posts a message using the django.contrib.messages backend.
        """
        if hasattr(messages, level) and callable(getattr(messages, level)):
            getattr(messages, level)(self.request, message) 
Example 5
Project: CTF_AWD_Platform   Author: xuchaoa   File: base.py    License: MIT License 5 votes vote down vote up
def message_user(self, message, level='info'):
        """
        Send a message to the user. The default implementation
        posts a message using the django.contrib.messages backend.
        """
        if hasattr(messages, level) and callable(getattr(messages, level)):
            getattr(messages, level)(self.request, message) 
Example 6
Project: django_OA   Author: abaoMAO   File: base.py    License: GNU General Public License v3.0 5 votes vote down vote up
def message_user(self, message, level='info'):
        """
        Send a message to the user. The default implementation
        posts a message using the django.contrib.messages backend.
        """
        if hasattr(messages, level) and callable(getattr(messages, level)):
            getattr(messages, level)(self.request, message) 
Example 7
Project: Mxonline3   Author: mtianyan   File: base.py    License: Apache License 2.0 5 votes vote down vote up
def message_user(self, message, level='info'):
        """
        Send a message to the user. The default implementation
        posts a message using the django.contrib.messages backend.
        """
        if hasattr(messages, level) and callable(getattr(messages, level)):
            getattr(messages, level)(self.request, message) 
Example 8
Project: imoocc   Author: iopsgroup   File: base.py    License: GNU General Public License v2.0 5 votes vote down vote up
def message_user(self, message, level='info'):
        """
        Send a message to the user. The default implementation
        posts a message using the django.contrib.messages backend.
        """
        if hasattr(messages, level) and callable(getattr(messages, level)):
            getattr(messages, level)(self.request, message) 
Example 9
Project: devops   Author: madre   File: base.py    License: MIT License 5 votes vote down vote up
def message_user(self, message, level='info'):
        """
        Send a message to the user. The default implementation
        posts a message using the django.contrib.messages backend.
        """
        if hasattr(messages, level) and callable(getattr(messages, level)):
            getattr(messages, level)(self.request, message) 
Example 10
Project: online   Author: myminwang   File: base.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def message_user(self, message, level='info'):
        """
        Send a message to the user. The default implementation
        posts a message using the django.contrib.messages backend.
        """
        if hasattr(messages, level) and callable(getattr(messages, level)):
            getattr(messages, level)(self.request, message) 
Example 11
Project: edx-enterprise   Author: edx   File: mixins.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def _get_messages_from_response_cookies(self, response):
        """
        Get django messages set in response cookies.
        """
        # pylint: disable=protected-access
        try:
            return messages.storage.cookie.CookieStorage(response)._decode(response.cookies['messages'].value)
        except KeyError:
            # No messages stored in cookies
            return None 
Example 12
Project: edx-enterprise   Author: edx   File: mixins.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def _assert_django_test_client_messages(self, test_client_response, expected_log_messages):
        """
        Verify that expected messages are included in the context of response.
        """
        response_messages = [
            (msg.level, msg.message) for msg in test_client_response.context['messages']  # pylint: disable=no-member
        ]
        assert response_messages == expected_log_messages 
Example 13
Project: Dailyfresh-B2C   Author: BeanWei   File: base.py    License: Apache License 2.0 5 votes vote down vote up
def message_user(self, message, level='info'):
        """
        Send a message to the user. The default implementation
        posts a message using the django.contrib.messages backend.
        """
        if hasattr(messages, level) and callable(getattr(messages, level)):
            getattr(messages, level)(self.request, message) 
Example 14
Project: djongo   Author: nesdis   File: urls.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def add(request, message_type):
    # Don't default to False here to test that it defaults to False if
    # unspecified.
    fail_silently = request.POST.get('fail_silently', None)
    for msg in request.POST.getlist('messages'):
        if fail_silently is not None:
            getattr(messages, message_type)(request, msg, fail_silently=fail_silently)
        else:
            getattr(messages, message_type)(request, msg)
    return HttpResponseRedirect(reverse('show_message')) 
Example 15
Project: djongo   Author: nesdis   File: urls.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def add_template_response(request, message_type):
    for msg in request.POST.getlist('messages'):
        getattr(messages, message_type)(request, msg)
    return HttpResponseRedirect(reverse('show_template_response')) 
Example 16
Project: djongo   Author: nesdis   File: urls.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def add(request, message_type):
    # Don't default to False here to test that it defaults to False if
    # unspecified.
    fail_silently = request.POST.get('fail_silently', None)
    for msg in request.POST.getlist('messages'):
        if fail_silently is not None:
            getattr(messages, message_type)(request, msg, fail_silently=fail_silently)
        else:
            getattr(messages, message_type)(request, msg)
    return HttpResponseRedirect(reverse('show_message')) 
Example 17
Project: djongo   Author: nesdis   File: urls.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def add_template_response(request, message_type):
    for msg in request.POST.getlist('messages'):
        getattr(messages, message_type)(request, msg)
    return HttpResponseRedirect(reverse('show_template_response')) 
Example 18
Project: ImitationTmall_Django   Author: Liweimin0512   File: base.py    License: GNU General Public License v3.0 5 votes vote down vote up
def message_user(self, message, level='info'):
        """
        Send a message to the user. The default implementation
        posts a message using the django.contrib.messages backend.
        """
        if hasattr(messages, level) and callable(getattr(messages, level)):
            getattr(messages, level)(self.request, message) 
Example 19
Project: mitoc-trips   Author: DavidCain   File: test_signups.py    License: GNU General Public License v3.0 5 votes vote down vote up
def test_adds_message_on_request(self):
        request = RequestFactory().get('/')
        signup = factories.SignUpFactory.create(on_trip=False)
        with patch.object(messages, 'success') as success:
            wl_signup = signup_utils.add_to_waitlist(signup, request=request)
        success.assert_called_once_with(request, "Added to waitlist.")
        self.assertEqual(wl_signup.signup, signup)
        self.assertFalse(wl_signup.signup.on_trip) 
Example 20
Project: django-leonardo   Author: django-leonardo   File: horizon.py    License: BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def process_response(self, request, response):
        """Convert HttpResponseRedirect to HttpResponse if request is via ajax
        to allow ajax request to redirect url
        """

        if request.is_ajax() and hasattr(request, 'horizon'):
            queued_msgs = request.horizon['async_messages']
            if type(response) == http.HttpResponseRedirect:
                # Drop our messages back into the session as per usual so they
                # don't disappear during the redirect. Not that we explicitly
                # use django's messages methods here.
                for tag, message, extra_tags in queued_msgs:
                    getattr(django_messages, tag)(request, message, extra_tags)
                # if response['location'].startswith(settings.LOGOUT_URL):
                #     redirect_response = http.HttpResponse(status=401)
                #     # This header is used for handling the logout in JS
                #     redirect_response['logout'] = True
                #     if self.logout_reason is not None:
                #         utils.add_logout_reason(
                #             request, redirect_response, self.logout_reason)
                # else:
                redirect_response = http.HttpResponse()
                # Use a set while checking if we want a cookie's attributes
                # copied
                cookie_keys = set(('max_age', 'expires', 'path', 'domain',
                                   'secure', 'httponly', 'logout_reason'))
                # Copy cookies from HttpResponseRedirect towards HttpResponse
                for cookie_name, cookie in six.iteritems(response.cookies):
                    cookie_kwargs = dict((
                        (key, value) for key, value in six.iteritems(cookie)
                        if key in cookie_keys and value
                    ))
                    redirect_response.set_cookie(
                        cookie_name, cookie.value, **cookie_kwargs)
                redirect_response['X-Horizon-Location'] = response['location']
                upload_url_key = 'X-File-Upload-URL'
                if upload_url_key in response:
                    self.copy_headers(response, redirect_response,
                                      (upload_url_key, 'X-Auth-Token'))
                return redirect_response
            if queued_msgs:
                # TODO(gabriel): When we have an async connection to the
                # client (e.g. websockets) this should be pushed to the
                # socket queue rather than being sent via a header.
                # The header method has notable drawbacks (length limits,
                # etc.) and is not meant as a long-term solution.
                response['X-Horizon-Messages'] = json.dumps(queued_msgs)
        return response 
Example 21
Project: avos   Author: CiscoSystems   File: middleware.py    License: Apache License 2.0 4 votes vote down vote up
def process_response(self, request, response):
        """Convert HttpResponseRedirect to HttpResponse if request is via ajax
        to allow ajax request to redirect url
        """
        if request.is_ajax() and hasattr(request, 'horizon'):
            queued_msgs = request.horizon['async_messages']
            if type(response) == http.HttpResponseRedirect:
                # Drop our messages back into the session as per usual so they
                # don't disappear during the redirect. Not that we explicitly
                # use django's messages methods here.
                for tag, message, extra_tags in queued_msgs:
                    getattr(django_messages, tag)(request, message, extra_tags)
                if response['location'].startswith(settings.LOGOUT_URL):
                    redirect_response = http.HttpResponse(status=401)
                    # This header is used for handling the logout in JS
                    redirect_response['logout'] = True
                    if self.logout_reason is not None:
                        utils.add_logout_reason(
                            request, redirect_response, self.logout_reason)
                else:
                    redirect_response = http.HttpResponse()
                # Use a set while checking if we want a cookie's attributes
                # copied
                cookie_keys = set(('max_age', 'expires', 'path', 'domain',
                                   'secure', 'httponly', 'logout_reason'))
                # Copy cookies from HttpResponseRedirect towards HttpResponse
                for cookie_name, cookie in six.iteritems(response.cookies):
                    cookie_kwargs = dict((
                        (key, value) for key, value in six.iteritems(cookie)
                        if key in cookie_keys and value
                    ))
                    redirect_response.set_cookie(
                        cookie_name, cookie.value, **cookie_kwargs)
                redirect_response['X-Horizon-Location'] = response['location']
                return redirect_response
            if queued_msgs:
                # TODO(gabriel): When we have an async connection to the
                # client (e.g. websockets) this should be pushed to the
                # socket queue rather than being sent via a header.
                # The header method has notable drawbacks (length limits,
                # etc.) and is not meant as a long-term solution.
                response['X-Horizon-Messages'] = json.dumps(queued_msgs)
        return response