from django.db import models class OutboxMail(models.Model): """One e-mail waiting to be sent, being sent, sent or failed. Owned by `mailer` (AD-19).""" class Status(models.TextChoices): QUEUED = "queued" SENDING = "sending" SENT = "sent" FAILED = "failed" template = models.CharField(max_length=100) recipient = models.CharField(max_length=254) subject = models.CharField(max_length=300) # Kept only until the mail is sent: it may hold a one-time link. body_text = models.TextField(blank=True) status = models.CharField(max_length=10, choices=Status.choices, default=Status.QUEUED) attempts = models.PositiveSmallIntegerField(default=0) created_at = models.DateTimeField() next_attempt_at = models.DateTimeField() claimed_at = models.DateTimeField(null=True, blank=True) sent_at = models.DateTimeField(null=True, blank=True) # What went wrong, in words without the recipient or the text of the mail. last_error = models.CharField(max_length=200, blank=True) class Meta: indexes = [models.Index(fields=["status", "next_attempt_at"])] ordering = ["id"]