messages.py 2.4 KB

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