Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.2KB

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