SMW-Upload/quotes/models.py

122 lines
3.7 KiB
Python

from django.db import models
from django.utils import timezone
from datetime import timedelta
from pathlib import Path
def default_expiration_time():
"""Default expiration time: 20 days from now."""
return timezone.now() + timedelta(days=20)
class Submission(models.Model):
"""Model for quote request submissions."""
email = models.EmailField(
help_text="Email of the person who submitted the quote request"
)
description = models.TextField(
help_text="Notes, comments, or description provided by the submitter"
)
submitted_at = models.DateTimeField(
auto_now_add=True, help_text="When the submission was created"
)
expiration_time = models.DateTimeField(
default=default_expiration_time,
help_text="When this submission expires (default 20 days from submission)",
)
class Meta:
ordering = ["-submitted_at"]
verbose_name = "Submission"
verbose_name_plural = "Submissions"
def __str__(self):
return f"Submission from {self.email} at {self.submitted_at.strftime('%Y-%m-%d %H:%M')}"
def is_expired(self):
"""Check if the submission has expired."""
return timezone.now() > self.expiration_time
def get_file_paths(self):
"""
Get list of all file paths relative to /data/uploads.
Returns list of relative paths as strings.
"""
return [file.path for file in self.files.all()]
def get_file_urls(self):
"""
Get list of URLs for all files in this submission.
Returns list of URLs that can be used to access the files.
"""
from django.conf import settings
from django.urls import reverse
urls = []
for file in self.files.all():
# Path relative to MEDIA_ROOT
relative_path = file.path
# Construct URL
url = f"{settings.MEDIA_URL}{relative_path}"
urls.append(url)
return urls
def get_absolute_file_paths(self):
"""
Get list of absolute file system paths for all files.
Returns list of absolute Path objects.
"""
from django.conf import settings
paths = []
for file in self.files.all():
# Path relative to MEDIA_ROOT/uploads
relative_path = file.path
# Construct absolute path
absolute_path = Path(settings.MEDIA_ROOT) / relative_path
paths.append(absolute_path)
return paths
class SubmissionFile(models.Model):
"""Model for files associated with a submission."""
submission = models.ForeignKey(
Submission,
on_delete=models.CASCADE,
related_name="files",
help_text="The submission this file belongs to",
)
original_filename = models.CharField(
max_length=255, help_text="Original filename as uploaded by the user"
)
path = models.CharField(
max_length=500,
help_text="Path relative to /data/uploads (e.g., 'filename.pdf')",
)
file_size = models.PositiveIntegerField(help_text="Size of the file in bytes")
uploaded_at = models.DateTimeField(
auto_now_add=True, help_text="When the file was uploaded"
)
class Meta:
ordering = ["uploaded_at"]
verbose_name = "Submission File"
verbose_name_plural = "Submission Files"
def __str__(self):
return f"{self.original_filename} ({self.submission.email})"
def get_absolute_path(self):
"""Get the absolute file system path."""
from django.conf import settings
return Path(settings.MEDIA_ROOT) / self.path
def get_url(self):
"""Get the URL to access this file."""
from django.conf import settings
return f"{settings.MEDIA_URL}{self.path}"