| 1 |
# Levels |
| 2 |
DEBUG = 10 |
| 3 |
INFO = 20 |
| 4 |
WARNING = 30 |
| 5 |
ERROR = 40 |
| 6 |
CRITICAL = 50 |
| 7 |
|
| 8 |
|
| 9 |
class CheckMessage: |
| 10 |
def __init__(self, level, msg, hint=None, obj=None, id=None): |
| 11 |
if not isinstance(level, int): |
| 12 |
raise TypeError("The first argument should be level.") |
| 13 |
self.level = level |
| 14 |
self.msg = msg |
| 15 |
self.hint = hint |
| 16 |
self.obj = obj |
| 17 |
self.id = id |
| 18 |
|
| 19 |
def __eq__(self, other): |
| 20 |
return isinstance(other, self.__class__) and all( |
| 21 |
getattr(self, attr) == getattr(other, attr) |
| 22 |
for attr in ["level", "msg", "hint", "obj", "id"] |
| 23 |
) |
| 24 |
|
| 25 |
def __str__(self): |
| 26 |
from django.db import models |
| 27 |
|
| 28 |
if self.obj is None: |
| 29 |
obj = "?" |
| 30 |
elif isinstance(self.obj, models.base.ModelBase): |
| 31 |
# We need to hardcode ModelBase and Field cases because its __str__ |
| 32 |
# method doesn't return "applabel.modellabel" and cannot be changed. |
| 33 |
obj = self.obj._meta.label |
| 34 |
else: |
| 35 |
obj = 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 |
|
| 40 |
def __repr__(self): |
| 41 |
return "<%s: level=%r, msg=%r, hint=%r, obj=%r, id=%r>" % ( |
| 42 |
self.__class__.__name__, |
| 43 |
self.level, |
| 44 |
self.msg, |
| 45 |
self.hint, |
| 46 |
self.obj, |
| 47 |
self.id, |
| 48 |
) |
| 49 |
|
| 50 |
def is_serious(self, level=ERROR): |
| 51 |
return self.level >= level |
| 52 |
|
| 53 |
def is_silenced(self): |
| 54 |
from django.conf import settings |
| 55 |
|
| 56 |
return self.id in settings.SILENCED_SYSTEM_CHECKS |
| 57 |
|
| 58 |
|
| 59 |
class Debug(CheckMessage): |
| 60 |
def __init__(self, *args, **kwargs): |
| 61 |
super().__init__(DEBUG, *args, **kwargs) |
| 62 |
|
| 63 |
|
| 64 |
class Info(CheckMessage): |
| 65 |
def __init__(self, *args, **kwargs): |
| 66 |
super().__init__(INFO, *args, **kwargs) |
| 67 |
|
| 68 |
|
| 69 |
class Warning(CheckMessage): |
| 70 |
def __init__(self, *args, **kwargs): |
| 71 |
super().__init__(WARNING, *args, **kwargs) |
| 72 |
|
| 73 |
|
| 74 |
class Error(CheckMessage): |
| 75 |
def __init__(self, *args, **kwargs): |
| 76 |
super().__init__(ERROR, *args, **kwargs) |
| 77 |
|
| 78 |
|
| 79 |
class Critical(CheckMessage): |
| 80 |
def __init__(self, *args, **kwargs): |
| 81 |
super().__init__(CRITICAL, *args, **kwargs) |