Archived
- Backend: Django REST Framework API with Bookmark and Note models - ViewSets with user-scoped querysets and select_related for N+1 prevention - Create/List/Detail/Update/Delete endpoints - Batch delete operations - Unique constraint on user+book+page for bookmarks - IsOwner permission class for object-level access control - Full serializer validation (page > 0, non-empty content, duplicate check) - 30+ pytest-django tests covering CRUD, auth, filtering, edge cases - Frontend: React TypeScript components - AnnotationsContext with useReducer for state management - BookmarkList, NoteList, AddAnnotationForm, AnnotationsDashboard - Inline note editing with immediate save - Batch delete support - API client with JWT auto-refresh interceptors - Paginated query hook for infinite scroll support - Responsive CSS with loading/empty states - Infrastructure: Django project with custom User model, JWT auth, CORS - PostgreSQL database models with proper FK and indexes - Django admin configuration for all models
84 lines
2.4 KiB
Python
84 lines
2.4 KiB
Python
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}" |