Skip to content

Commit

Permalink
fix: delete attachments while samples are deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
gary-Shen committed Feb 12, 2025
1 parent cbb9fbc commit 4675594
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions labelu/internal/application/service/sample.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
import uuid
from datetime import datetime
from typing import List, Tuple, Union
Expand All @@ -12,7 +13,7 @@
from labelu.internal.common.converter import converter
from labelu.internal.common.error_code import ErrorCode
from labelu.internal.common.error_code import LabelUException
from labelu.internal.adapter.persistence import crud_pre_annotation, crud_task
from labelu.internal.adapter.persistence import crud_attachment, crud_pre_annotation, crud_task
from labelu.internal.adapter.persistence import crud_sample
from labelu.internal.domain.models.pre_annotation import TaskPreAnnotation
from labelu.internal.domain.models.user import User
Expand Down Expand Up @@ -253,6 +254,18 @@ async def delete(
) -> CommonDataResp:

with db.begin():
# delete media
samples = crud_sample.get_by_ids(db=db, sample_ids=sample_ids)
attachment_ids = [sample.file_id for sample in samples if sample.file_id]
attachments = crud_attachment.get_by_ids(db=db, attachment_ids=attachment_ids)

attachments = crud_attachment.get_by_ids(
db=db, attachment_ids=attachment_ids
)
for attachment in attachments:
file_full_path = Path(settings.MEDIA_ROOT).joinpath(attachment.path)
os.remove(file_full_path)

crud_sample.delete(db=db, sample_ids=sample_ids)
# response
return CommonDataResp(ok=True)
Expand Down Expand Up @@ -293,4 +306,4 @@ async def export(
)

# response
return file_full_path
return file_full_path

0 comments on commit 4675594

Please sign in to comment.