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
3from django.conf import settings
4from django.db import models
5from django.utils.translation import gettext_lazy as _
6from uuid import uuid4
8# Exports
10__all__ = (
11 "ErrorModel",
12)
14# Constants
16AUTH_USER_MODEL = settings.AUTH_USER_MODEL
18# Models
21class ErrorModel(models.Model):
22 """Captures exception data to the database."""
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 )
30 identifier = models.UUIDField(
31 verbose_name=_("Unique ID"),
32 default=uuid4,
33 help_text=_("A unique identifier."),
34 editable=False,
35 )
37 line = models.PositiveIntegerField(
38 _("line number"),
39 blank=True,
40 help_text=_("The line number where the error occurred."),
41 null=True
42 )
44 name = models.CharField(
45 _("name"),
46 help_text=_("The name (exception type) of the error."),
47 max_length=256
48 )
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 )
58 traceback = models.TextField(
59 _("traceback"),
60 blank=True,
61 help_text=_("The traceback of the error."),
62 null=True
63 )
65 class Meta:
66 abstract = True
68 def __str__(self):
69 return self.get_display_name()
71 def get_display_name(self):
72 """Get the display name for the error.
74 :rtype: str
76 """
77 a = list()
78 a.append("[%s]" % self.added_dt)
79 a.append(self.name)
81 if self.path:
82 a.append(self.path)
84 if self.line:
85 a.append(self.line)
87 return " ".join(a)
89 @classmethod
90 def log(cls, report):
91 """Create a new error record.
93 :param report: An exception reporter instance for the current exception(s).
94 :type report: django.views.debug.ExceptionReporter
96 :returns: The error record instance.
98 """
99 context = report.get_traceback_data()
101 line = None
102 path = None
103 if 'lastframe' in context:
104 line = context['lastframe']['lineno']
105 path = context['lastframe']['filename']
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()
115 return instance