from django.conf import settings from django.db import models class Bookmark(models.Model): """A user bookmark at a specific page in a document.""" document = models.ForeignKey( "documents.Document", on_delete=models.CASCADE, related_name="bookmarks", ) user = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="bookmarks", ) page = models.PositiveIntegerField() label = models.CharField(max_length=300, blank=True, default="") created_at = models.DateTimeField(auto_now_add=True) class Meta: db_table = "reading_bookmark" ordering = ["page"] unique_together = [["document", "user", "page"]] def __str__(self) -> str: return f"{self.document.title} p.{self.page}" class Highlight(models.Model): """A highlighted passage in a document.""" document = models.ForeignKey( "documents.Document", on_delete=models.CASCADE, related_name="highlights", ) user = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="highlights", ) page = models.PositiveIntegerField() color = models.CharField(max_length=20, default="yellow") text = models.TextField() note = models.TextField(blank=True, null=True) created_at = models.DateTimeField(auto_now_add=True) class Meta: db_table = "reading_highlight" ordering = ["-created_at"] def __str__(self) -> str: return f"Highlight on {self.document.title} p.{self.page}" class ReadingProgress(models.Model): """Tracks the user's reading progress through a document.""" document = models.ForeignKey( "documents.Document", on_delete=models.CASCADE, related_name="reading_progress", ) user = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="reading_progress", ) current_page = models.PositiveIntegerField(default=1) total_pages = models.PositiveIntegerField(default=0) percentage = models.FloatField(default=0.0) last_read_at = models.DateTimeField(auto_now=True) class Meta: db_table = "reading_progress" unique_together = [["document", "user"]] verbose_name_plural = "Reading progress" def __str__(self) -> str: return f"{self.document.title} — {self.percentage:.0f}%" def save(self, *args, **kwargs): if self.total_pages > 0: self.percentage = round((self.current_page / self.total_pages) * 100, 1) super().save(*args, **kwargs)