from django.conf import settings from django.db import models class Notification(models.Model): """Tells `recipient` that `actor` did something (`verb`) involving `moment` (and, for comment/reply verbs, the specific `comment`). Created by signal handlers in signals.py — never directly by API consumers.""" class Verb(models.TextChoices): LIKE = "like", "Like" COMMENT = "comment", "Comment" REPLY = "reply", "Reply" recipient = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="notifications", on_delete=models.CASCADE) actor = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="+", on_delete=models.CASCADE) verb = models.CharField(max_length=10, choices=Verb.choices) moment = models.ForeignKey("moments.Moment", related_name="notifications", on_delete=models.CASCADE) comment = models.ForeignKey( "moments.Comment", null=True, blank=True, related_name="notifications", on_delete=models.CASCADE ) is_read = models.BooleanField(default=False) created_at = models.DateTimeField(auto_now_add=True) class Meta: ordering = ["-created_at"] indexes = [models.Index(fields=["recipient", "is_read"])] def __str__(self): return f"{self.actor} {self.verb} -> {self.recipient}"