🎨 Code structure, trip attachments folder creation, early return
This commit is contained in:
parent
6522a0e373
commit
3173d867e2
@ -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,
|
||||
)
|
||||
|
||||
@ -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():
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user