import uuid from django.conf import settings from django.db import models class Bookmark(models.Model): """A saved location in a book that the user can return to.""" id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) user = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="bookmarks", db_index=True, ) book = models.ForeignKey( "books.Book", on_delete=models.CASCADE, related_name="bookmarks", db_index=True, ) page = models.PositiveIntegerField() location_text = models.TextField( blank=True, default="", help_text="The selected passage text at this location", ) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: db_table = "annotations_bookmark" verbose_name = "Bookmark" verbose_name_plural = "Bookmarks" ordering = ["-created_at"] constraints = [ models.UniqueConstraint( fields=["user", "book", "page"], name="uq_bookmark_user_book_page", ) ] def __str__(self) -> str: return f"{self.user} @ {self.book} p.{self.page}" class Note(models.Model): """A user-written note attached to a specific location in a book.""" id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) user = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="notes", db_index=True, ) book = models.ForeignKey( "books.Book", on_delete=models.CASCADE, related_name="notes", db_index=True, ) page = models.PositiveIntegerField() location_text = models.TextField( blank=True, default="", help_text="The selected passage text this note refers to", ) content = models.TextField( help_text="The note body content" ) created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: db_table = "annotations_note" verbose_name = "Note" verbose_name_plural = "Notes" ordering = ["-created_at"] def __str__(self) -> str: preview = self.content[:50] return f"{self.user} @ {self.book} p.{self.page}: {preview}"