Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# Imports 

2 

3from django.conf import settings 

4from django.db import models 

5from django.utils.translation import gettext_lazy as _ 

6from uuid import uuid4 

7 

8# Exports 

9 

10__all__ = ( 

11 "ErrorModel", 

12) 

13 

14# Constants 

15 

16AUTH_USER_MODEL = settings.AUTH_USER_MODEL 

17 

18# Models 

19 

20 

21class ErrorModel(models.Model): 

22 """Captures exception data to the database.""" 

23 

24 added_dt = models.DateTimeField( 

25 _("added date/time"), 

26 auto_now_add=True, 

27 help_text=_("Date and time the record was created.") 

28 ) 

29 

30 identifier = models.UUIDField( 

31 verbose_name=_("Unique ID"), 

32 default=uuid4, 

33 help_text=_("A unique identifier."), 

34 editable=False, 

35 ) 

36 

37 line = models.PositiveIntegerField( 

38 _("line number"), 

39 blank=True, 

40 help_text=_("The line number where the error occurred."), 

41 null=True 

42 ) 

43 

44 name = models.CharField( 

45 _("name"), 

46 help_text=_("The name (exception type) of the error."), 

47 max_length=256 

48 ) 

49 

50 path = models.CharField( 

51 _("path"), 

52 blank=True, 

53 help_text=_("The path where the error occurred."), 

54 max_length=1024, 

55 null=True 

56 ) 

57 

58 traceback = models.TextField( 

59 _("traceback"), 

60 blank=True, 

61 help_text=_("The traceback of the error."), 

62 null=True 

63 ) 

64 

65 class Meta: 

66 abstract = True 

67 

68 def __str__(self): 

69 return self.get_display_name() 

70 

71 def get_display_name(self): 

72 """Get the display name for the error. 

73 

74 :rtype: str 

75 

76 """ 

77 a = list() 

78 a.append("[%s]" % self.added_dt) 

79 a.append(self.name) 

80 

81 if self.path: 

82 a.append(self.path) 

83 

84 if self.line: 

85 a.append(self.line) 

86 

87 return " ".join(a) 

88 

89 @classmethod 

90 def log(cls, report): 

91 """Create a new error record. 

92 

93 :param report: An exception reporter instance for the current exception(s). 

94 :type report: django.views.debug.ExceptionReporter 

95 

96 :returns: The error record instance. 

97 

98 """ 

99 context = report.get_traceback_data() 

100 

101 line = None 

102 path = None 

103 if 'lastframe' in context: 

104 line = context['lastframe']['lineno'] 

105 path = context['lastframe']['filename'] 

106 

107 instance = cls( 

108 line=line, 

109 path=path, 

110 name=report.exc_type.__name__, 

111 traceback=report.get_traceback_text() 

112 ) 

113 instance.save() 

114 

115 return instance