# Imports
from django.conf import settings
from django.db import models
from django.utils.translation import gettext_lazy as _
from uuid import uuid4
# Exports
__all__ = (
"ErrorModel",
)
# Constants
AUTH_USER_MODEL = settings.AUTH_USER_MODEL
# Models
[docs]class ErrorModel(models.Model):
"""Captures exception data to the database."""
added_dt = models.DateTimeField(
_("added date/time"),
auto_now_add=True,
help_text=_("Date and time the record was created.")
)
identifier = models.UUIDField(
verbose_name=_("Unique ID"),
default=uuid4,
help_text=_("A unique identifier."),
editable=False,
)
line = models.PositiveIntegerField(
_("line number"),
blank=True,
help_text=_("The line number where the error occurred."),
null=True
)
name = models.CharField(
_("name"),
help_text=_("The name (exception type) of the error."),
max_length=256
)
path = models.CharField(
_("path"),
blank=True,
help_text=_("The path where the error occurred."),
max_length=1024,
null=True
)
traceback = models.TextField(
_("traceback"),
blank=True,
help_text=_("The traceback of the error."),
null=True
)
class Meta:
abstract = True
def __str__(self):
return self.get_display_name()
[docs] def get_display_name(self):
"""Get the display name for the error.
:rtype: str
"""
a = list()
a.append("[%s]" % self.added_dt)
a.append(self.name)
if self.path:
a.append(self.path)
if self.line:
a.append(self.line)
return " ".join(a)
[docs] @classmethod
def log(cls, report):
"""Create a new error record.
:param report: An exception reporter instance for the current exception(s).
:type report: django.views.debug.ExceptionReporter
:returns: The error record instance.
"""
context = report.get_traceback_data()
line = None
path = None
if 'lastframe' in context:
line = context['lastframe']['lineno']
path = context['lastframe']['filename']
instance = cls(
line=line,
path=path,
name=report.exc_type.__name__,
traceback=report.get_traceback_text()
)
instance.save()
return instance