You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

108 lines
3.1 KiB

  1. from django.contrib.messages import constants
  2. from django.contrib.messages.storage import default_storage
  3. from django.http import HttpRequest
  4. __all__ = (
  5. 'add_message', 'get_messages',
  6. 'get_level', 'set_level',
  7. 'debug', 'info', 'success', 'warning', 'error',
  8. 'MessageFailure',
  9. )
  10. class MessageFailure(Exception):
  11. pass
  12. def add_message(request, level, message, extra_tags='', fail_silently=False):
  13. """
  14. Attempts to add a message to the request using the 'messages' app.
  15. """
  16. if not isinstance(request, HttpRequest):
  17. raise TypeError("add_message() argument must be an HttpRequest object, "
  18. "not '%s'." % request.__class__.__name__)
  19. if hasattr(request, '_messages'):
  20. return request._messages.add(level, message, extra_tags)
  21. if not fail_silently:
  22. raise MessageFailure('You cannot add messages without installing '
  23. 'django.contrib.messages.middleware.MessageMiddleware')
  24. def get_messages(request):
  25. """
  26. Returns the message storage on the request if it exists, otherwise returns
  27. an empty list.
  28. """
  29. if hasattr(request, '_messages'):
  30. return request._messages
  31. else:
  32. return []
  33. def get_level(request):
  34. """
  35. Returns the minimum level of messages to be recorded.
  36. The default level is the ``MESSAGE_LEVEL`` setting. If this is not found,
  37. the ``INFO`` level is used.
  38. """
  39. if hasattr(request, '_messages'):
  40. storage = request._messages
  41. else:
  42. storage = default_storage(request)
  43. return storage.level
  44. def set_level(request, level):
  45. """
  46. Sets the minimum level of messages to be recorded, returning ``True`` if
  47. the level was recorded successfully.
  48. If set to ``None``, the default level will be used (see the ``get_level``
  49. method).
  50. """
  51. if not hasattr(request, '_messages'):
  52. return False
  53. request._messages.level = level
  54. return True
  55. def debug(request, message, extra_tags='', fail_silently=False):
  56. """
  57. Adds a message with the ``DEBUG`` level.
  58. """
  59. add_message(request, constants.DEBUG, message, extra_tags=extra_tags,
  60. fail_silently=fail_silently)
  61. def info(request, message, extra_tags='', fail_silently=False):
  62. """
  63. Adds a message with the ``INFO`` level.
  64. """
  65. add_message(request, constants.INFO, message, extra_tags=extra_tags,
  66. fail_silently=fail_silently)
  67. def success(request, message, extra_tags='', fail_silently=False):
  68. """
  69. Adds a message with the ``SUCCESS`` level.
  70. """
  71. add_message(request, constants.SUCCESS, message, extra_tags=extra_tags,
  72. fail_silently=fail_silently)
  73. def warning(request, message, extra_tags='', fail_silently=False):
  74. """
  75. Adds a message with the ``WARNING`` level.
  76. """
  77. add_message(request, constants.WARNING, message, extra_tags=extra_tags,
  78. fail_silently=fail_silently)
  79. def error(request, message, extra_tags='', fail_silently=False):
  80. """
  81. Adds a message with the ``ERROR`` level.
  82. """
  83. add_message(request, constants.ERROR, message, extra_tags=extra_tags,
  84. fail_silently=fail_silently)