Skip to content

fix: remove old image from s3 when updating or deleting image#23

Merged
ngovinh2k2 merged 1 commit into
devfrom
fix/remove-old-image-from-s3-when-updating-or-deleting-image
Jun 8, 2026
Merged

fix: remove old image from s3 when updating or deleting image#23
ngovinh2k2 merged 1 commit into
devfrom
fix/remove-old-image-from-s3-when-updating-or-deleting-image

Conversation

@ngovinh2k2

Copy link
Copy Markdown
Member

What?

Remove old image from s3 when updating or deleting image

Why?

From the request

How?

Testing?

  • Functional Testing
  • Security
  • Performance
  • Error Handling
  • Code Quality
  • Documentation
  • Database
  • Deployment
  • Final Review

Anything Else?

@ngovinh2k2
ngovinh2k2 merged commit 1153d34 into dev Jun 8, 2026
1 check passed

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds S3 logo deletion and presigned URL generation for organizations. It overrides the update method in OrganizationSerializer to trigger a Celery task for deleting the old logo when updated, and overrides to_representation to include a presigned URL for the logo. It also registers the new deletion task in Celery. Feedback suggests executing the deletion task only after a successful database transaction commit using transaction.on_commit to prevent premature deletion on rollbacks, and simplifying the truthiness check for instance.logo to align with PEP 8 guidelines.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +33 to +40
if old_logo and old_logo != new_logo:
send_task(
name=constants.CONSOLE_SERVICE_DELETE_UPLOAD_FILE,
message={
"bucket_name": settings.AWS_S3.get("AWS_STORAGE_BUCKET_NAME"),
"link_file": f"uploads/{old_logo}",
},
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

To prevent S3 files from being deleted when a database transaction is rolled back (e.g., due to subsequent errors in the request), trigger the Celery task using transaction.on_commit.

Suggested change
if old_logo and old_logo != new_logo:
send_task(
name=constants.CONSOLE_SERVICE_DELETE_UPLOAD_FILE,
message={
"bucket_name": settings.AWS_S3.get("AWS_STORAGE_BUCKET_NAME"),
"link_file": f"uploads/{old_logo}",
},
)
if old_logo and old_logo != new_logo:
transaction.on_commit(
lambda: send_task(
name=constants.CONSOLE_SERVICE_DELETE_UPLOAD_FILE,
message={
"bucket_name": settings.AWS_S3.get("AWS_STORAGE_BUCKET_NAME"),
"link_file": f"uploads/{old_logo}",
},
)
)

from common.apps.upload_file.service import get_presigned_url
from common.celery import constants
from common.celery.task_senders import send_task
from django.conf import settings

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Import transaction from django.db to enable executing the S3 deletion task only after the database transaction successfully commits.

Suggested change
from django.conf import settings
from django.conf import settings
from django.db import transaction


def to_representation(self, instance):
data = super().to_representation(instance)
if instance.logo and instance.logo not in ["", None]:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Simplify the truthiness check for instance.logo. In Python, empty strings and None are already falsy, so if instance.logo: is more idiomatic and adheres to PEP 8 programming recommendations.

Suggested change
if instance.logo and instance.logo not in ["", None]:
if instance.logo:
References
  1. PEP 8 recommends using the fact that empty sequences/strings are false, rather than comparing them explicitly. (link)

@ngovinh2k2
ngovinh2k2 deleted the fix/remove-old-image-from-s3-when-updating-or-deleting-image branch July 3, 2026 08:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant