2016-11-20 04:54:19 +08:00
|
|
|
from django.utils.encoding import force_str
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
# Levels
|
|
|
|
DEBUG = 10
|
|
|
|
INFO = 20
|
|
|
|
WARNING = 30
|
|
|
|
ERROR = 40
|
|
|
|
CRITICAL = 50
|
|
|
|
|
|
|
|
|
|
|
|
class CheckMessage(object):
|
|
|
|
|
2014-03-03 19:16:19 +08:00
|
|
|
def __init__(self, level, msg, hint=None, obj=None, id=None):
|
2014-01-20 10:45:21 +08:00
|
|
|
assert isinstance(level, int), "The first argument should be level."
|
|
|
|
self.level = level
|
|
|
|
self.msg = msg
|
|
|
|
self.hint = hint
|
|
|
|
self.obj = obj
|
|
|
|
self.id = id
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
2016-08-05 21:18:12 +08:00
|
|
|
return (
|
|
|
|
isinstance(other, self.__class__) and
|
|
|
|
all(getattr(self, attr) == getattr(other, attr)
|
|
|
|
for attr in ['level', 'msg', 'hint', 'obj', 'id'])
|
|
|
|
)
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
if self.obj is None:
|
|
|
|
obj = "?"
|
|
|
|
elif isinstance(self.obj, models.base.ModelBase):
|
|
|
|
# We need to hardcode ModelBase and Field cases because its __str__
|
|
|
|
# method doesn't return "applabel.modellabel" and cannot be changed.
|
2015-04-27 05:05:50 +08:00
|
|
|
obj = self.obj._meta.label
|
2014-01-20 10:45:21 +08:00
|
|
|
else:
|
|
|
|
obj = force_str(self.obj)
|
|
|
|
id = "(%s) " % self.id if self.id else ""
|
|
|
|
hint = "\n\tHINT: %s" % self.hint if self.hint else ''
|
|
|
|
return "%s: %s%s%s" % (obj, id, self.msg, hint)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<%s: level=%r, msg=%r, hint=%r, obj=%r, id=%r>" % \
|
|
|
|
(self.__class__.__name__, self.level, self.msg, self.hint, self.obj, self.id)
|
|
|
|
|
2015-10-05 02:16:12 +08:00
|
|
|
def is_serious(self, level=ERROR):
|
|
|
|
return self.level >= level
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
def is_silenced(self):
|
|
|
|
from django.conf import settings
|
|
|
|
return self.id in settings.SILENCED_SYSTEM_CHECKS
|
|
|
|
|
|
|
|
|
|
|
|
class Debug(CheckMessage):
|
|
|
|
def __init__(self, *args, **kwargs):
|
2014-11-13 04:26:31 +08:00
|
|
|
super(Debug, self).__init__(DEBUG, *args, **kwargs)
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Info(CheckMessage):
|
|
|
|
def __init__(self, *args, **kwargs):
|
2014-11-13 04:26:31 +08:00
|
|
|
super(Info, self).__init__(INFO, *args, **kwargs)
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Warning(CheckMessage):
|
|
|
|
def __init__(self, *args, **kwargs):
|
2014-11-13 04:26:31 +08:00
|
|
|
super(Warning, self).__init__(WARNING, *args, **kwargs)
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Error(CheckMessage):
|
|
|
|
def __init__(self, *args, **kwargs):
|
2014-11-13 04:26:31 +08:00
|
|
|
super(Error, self).__init__(ERROR, *args, **kwargs)
|
2014-01-20 10:45:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Critical(CheckMessage):
|
|
|
|
def __init__(self, *args, **kwargs):
|
2014-11-13 04:26:31 +08:00
|
|
|
super(Critical, self).__init__(CRITICAL, *args, **kwargs)
|