Coverage for /Users/davegaeddert/Developer/dropseed/plain/plain/plain/preflight/messages.py: 56%

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 try:

27 from plain import models

28

29 ModelBase = models.base.ModelBase

30 using_db = True

31 except ImportError:

32 using_db = False

33 ModelBase = object

34

35 if self.obj is None:

36 obj = "?"

37 elif using_db and isinstance(self.obj, ModelBase):

38 # We need to hardcode ModelBase and Field cases because its __str__

39 # method doesn't return "applabel.modellabel" and cannot be changed.

40 obj = self.obj._meta.label

41 else:

42 obj = str(self.obj)

43 id = f"({self.id}) " if self.id else ""

44 hint = f"\n\tHINT: {self.hint}" if self.hint else ""

45 return f"{obj}: {id}{self.msg}{hint}"

46

47 def __repr__(self):

48 return f"<{self.__class__.__name__}: level={self.level! r}, msg={self.msg! r}, hint={self.hint! r}, obj={self.obj! r}, id={self.id! r}>"

49

50 def is_serious(self, level=ERROR):

51 return self.level >= level

52

53 def is_silenced(self):

54 from plain.runtime import settings

55

56 return self.id in settings.SILENCED_PREFLIGHT_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)