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.

api.py 3.0KB

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