-
Notifications
You must be signed in to change notification settings - Fork 0
fix: remove old image from s3 when updating or deleting image #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,3 +1,7 @@ | ||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||
| from rest_framework import serializers | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| from apps.organization.models import Organization | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -21,3 +25,26 @@ def validate_slug_name(self, data): | |||||||||||||||||||||||||||||||||||||
| if "_" in data: | ||||||||||||||||||||||||||||||||||||||
| raise serializers.ValidationError(detail="slug name is invalid") | ||||||||||||||||||||||||||||||||||||||
| return data | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| def update(self, instance, validated_data): | ||||||||||||||||||||||||||||||||||||||
| old_logo = instance.logo | ||||||||||||||||||||||||||||||||||||||
| new_logo = validated_data.get("logo", old_logo) | ||||||||||||||||||||||||||||||||||||||
| instance = super().update(instance, validated_data) | ||||||||||||||||||||||||||||||||||||||
| 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}", | ||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+33
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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
Suggested change
|
||||||||||||||||||||||||||||||||||||||
| return instance | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simplify the truthiness check for
Suggested change
References
|
||||||||||||||||||||||||||||||||||||||
| data["url_logo"] = get_presigned_url( | ||||||||||||||||||||||||||||||||||||||
| settings.AWS_S3.get("AWS_STORAGE_BUCKET_NAME"), | ||||||||||||||||||||||||||||||||||||||
| f"uploads/{instance.logo}", | ||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||
| return data | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import
transactionfromdjango.dbto enable executing the S3 deletion task only after the database transaction successfully commits.