feat: submissions app + django-storages + whitenoise
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
from django.db import models
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
import uuid
|
||||
|
||||
from accounts.models import CustomUser
|
||||
|
||||
|
||||
def submission_file_upload_path(instance, filename):
|
||||
ext = filename.split(".")[-1] if "." in filename else ""
|
||||
|
||||
new_filename = f"{instance.id}_{filename}" if ext else str(instance.id)
|
||||
|
||||
return f"submissions/{instance.submission.id}/{new_filename}"
|
||||
|
||||
|
||||
class Submission(models.Model):
|
||||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
|
||||
user = models.ForeignKey(
|
||||
CustomUser,
|
||||
on_delete=models.CASCADE,
|
||||
null=True,
|
||||
blank=True,
|
||||
help_text="User who made the submission (null for anonymous)",
|
||||
related_name="submissions",
|
||||
)
|
||||
|
||||
# Submission metadata
|
||||
notes = models.TextField(
|
||||
null=True,
|
||||
blank=True,
|
||||
help_text="Optional notes about the submission",
|
||||
)
|
||||
|
||||
# Timestamps
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
|
||||
class SubmissionFile(models.Model):
|
||||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
|
||||
submission = models.ForeignKey(
|
||||
Submission,
|
||||
on_delete=models.CASCADE,
|
||||
related_name="files",
|
||||
)
|
||||
|
||||
file = models.FileField(
|
||||
upload_to=submission_file_upload_path,
|
||||
help_text=_("Uploaded file"),
|
||||
)
|
||||
|
||||
original_filename = models.CharField(
|
||||
max_length=255, help_text=_("Original filename as uploaded by user")
|
||||
)
|
||||
file_size = models.PositiveIntegerField(help_text=_("File size in bytes"))
|
||||
content_type = models.CharField(
|
||||
max_length=100,
|
||||
help_text=_("MIME type of the file"),
|
||||
)
|
||||
|
||||
# OCR metadata
|
||||
ocr_processed = models.BooleanField(
|
||||
default=False, help_text="Whether OCR has been processed for this file"
|
||||
)
|
||||
ocr_raw_data = models.JSONField(
|
||||
null=True, blank=True, help_text="Raw OCR data as JSON"
|
||||
)
|
||||
ocr_error = models.TextField(blank=True, help_text="OCR processing error message")
|
||||
|
||||
# Timestamps
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["file", "created_at"]
|
||||
verbose_name = "Submission File"
|
||||
verbose_name_plural = "Submission Files"
|
||||
|
||||
@property
|
||||
def file_url(self) -> str | None:
|
||||
if self.file:
|
||||
return self.file.url
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if self.file and not self.file_size:
|
||||
self.file_size = self.file.size
|
||||
|
||||
super().save(*args, **kwargs)
|
||||
Reference in New Issue
Block a user