Development of an internal social media platform with personalised dashboards for students
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.

messages.py 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Levels
  2. DEBUG = 10
  3. INFO = 20
  4. WARNING = 30
  5. ERROR = 40
  6. CRITICAL = 50
  7. class CheckMessage:
  8. def __init__(self, level, msg, hint=None, obj=None, id=None):
  9. assert isinstance(level, int), "The first argument should be level."
  10. self.level = level
  11. self.msg = msg
  12. self.hint = hint
  13. self.obj = obj
  14. self.id = id
  15. def __eq__(self, other):
  16. return (
  17. isinstance(other, self.__class__) and
  18. all(getattr(self, attr) == getattr(other, attr)
  19. for attr in ['level', 'msg', 'hint', 'obj', 'id'])
  20. )
  21. def __str__(self):
  22. from django.db import models
  23. if self.obj is None:
  24. obj = "?"
  25. elif isinstance(self.obj, models.base.ModelBase):
  26. # We need to hardcode ModelBase and Field cases because its __str__
  27. # method doesn't return "applabel.modellabel" and cannot be changed.
  28. obj = self.obj._meta.label
  29. else:
  30. obj = str(self.obj)
  31. id = "(%s) " % self.id if self.id else ""
  32. hint = "\n\tHINT: %s" % self.hint if self.hint else ''
  33. return "%s: %s%s%s" % (obj, id, self.msg, hint)
  34. def __repr__(self):
  35. return "<%s: level=%r, msg=%r, hint=%r, obj=%r, id=%r>" % \
  36. (self.__class__.__name__, self.level, self.msg, self.hint, self.obj, self.id)
  37. def is_serious(self, level=ERROR):
  38. return self.level >= level
  39. def is_silenced(self):
  40. from django.conf import settings
  41. return self.id in settings.SILENCED_SYSTEM_CHECKS
  42. class Debug(CheckMessage):
  43. def __init__(self, *args, **kwargs):
  44. super().__init__(DEBUG, *args, **kwargs)
  45. class Info(CheckMessage):
  46. def __init__(self, *args, **kwargs):
  47. super().__init__(INFO, *args, **kwargs)
  48. class Warning(CheckMessage):
  49. def __init__(self, *args, **kwargs):
  50. super().__init__(WARNING, *args, **kwargs)
  51. class Error(CheckMessage):
  52. def __init__(self, *args, **kwargs):
  53. super().__init__(ERROR, *args, **kwargs)
  54. class Critical(CheckMessage):
  55. def __init__(self, *args, **kwargs):
  56. super().__init__(CRITICAL, *args, **kwargs)