From 3173d867e2813e3c084d69140ea0b9eb822d1c6c Mon Sep 17 00:00:00 2001 From: itskovacs Date: Thu, 16 Oct 2025 17:21:46 +0200 Subject: [PATCH] :art: Code structure, trip attachments folder creation, early return --- backend/trip/models/models.py | 10 ++++++++-- backend/trip/utils/utils.py | 6 +++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/backend/trip/models/models.py b/backend/trip/models/models.py index 037592d..9097e0e 100644 --- a/backend/trip/models/models.py +++ b/backend/trip/models/models.py @@ -636,13 +636,13 @@ class TripChecklistItemRead(TripChecklistItemBase): class TripAttachmentBase(SQLModel): filename: str file_size: int + stored_filename: str class TripAttachment(TripAttachmentBase, table=True): id: int | None = Field(default=None, primary_key=True) uploaded_at: datetime = Field(default_factory=lambda: datetime.now(UTC)) uploaded_by: str = Field(foreign_key="user.username", ondelete="CASCADE") - stored_filename: str trip_id: int = Field(foreign_key="trip.id", ondelete="CASCADE", index=True) trip: Trip | None = Relationship(back_populates="attachments") @@ -667,4 +667,10 @@ class TripAttachmentRead(TripAttachmentBase): @classmethod def serialize(cls, obj: TripAttachment) -> "TripAttachmentRead": - return cls(id=obj.id, filename=obj.filename, file_size=obj.file_size, uploaded_by=obj.uploaded_by) + return cls( + id=obj.id, + filename=obj.filename, + file_size=obj.file_size, + uploaded_by=obj.uploaded_by, + stored_filename=obj.stored_filename, + ) diff --git a/backend/trip/utils/utils.py b/backend/trip/utils/utils.py index c95c6a0..c36853d 100644 --- a/backend/trip/utils/utils.py +++ b/backend/trip/utils/utils.py @@ -32,7 +32,9 @@ def attachments_folder_path() -> Path: def attachments_trip_folder_path(trip_id: int | str) -> Path: - return attachments_folder_path() / str(trip_id) + path = attachments_folder_path() / str(trip_id) + path.mkdir(parents=True, exist_ok=True) + return path def b64img_decode(data: str) -> bytes: @@ -52,6 +54,8 @@ def remove_attachment(trip_id: int, filename: str): def remove_backup(filename: str): + if not filename: + return try: backup_fp = Path(settings.BACKUPS_FOLDER) / filename if not backup_fp.exists():