fix: remove old image from s3 when updating or deleting image#23
Conversation
There was a problem hiding this comment.
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.
| 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}", | ||
| }, | ||
| ) |
There was a problem hiding this comment.
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.
| 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 |
|
|
||
| def to_representation(self, instance): | ||
| data = super().to_representation(instance) | ||
| if instance.logo and instance.logo not in ["", None]: |
There was a problem hiding this comment.
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.
| if instance.logo and instance.logo not in ["", None]: | |
| if instance.logo: |
References
- PEP 8 recommends using the fact that empty sequences/strings are false, rather than comparing them explicitly. (link)
What?
Remove old image from s3 when updating or deleting image
Why?
From the request
How?
Testing?
Anything Else?