from django.conf import settings from django.db import models class Document(models.Model): """A digital document (ebook, PDF, etc.) uploaded by a user.""" class FileType(models.TextChoices): PDF = "pdf", "PDF" EPUB = "epub", "EPUB" MOBI = "mobi", "MOBI" TXT = "txt", "Plain Text" DOCX = "docx", "Word Document" title = models.CharField(max_length=500) author = models.CharField(max_length=300, blank=True, null=True) description = models.TextField(blank=True, default="") cover = models.ImageField(upload_to="covers/", blank=True, null=True) file = models.FileField(upload_to="documents/") file_type = models.CharField(max_length=10, choices=FileType.choices) file_size = models.PositiveIntegerField(help_text="File size in bytes") page_count = models.PositiveIntegerField(blank=True, null=True) tags = models.JSONField(default=list, blank=True) is_public = models.BooleanField(default=False) owner = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="documents", ) uploaded_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: db_table = "documents_document" ordering = ["-uploaded_at"] indexes = [ models.Index(fields=["owner", "-uploaded_at"]), models.Index(fields=["file_type"]), ] def __str__(self) -> str: return self.title @property def cover_url(self) -> str | None: if self.cover: return self.cover.url return None