20 lines
585 B
Python
20 lines
585 B
Python
from django.db import transaction
|
|
from django.db.models.signals import post_delete
|
|
from django.dispatch import receiver
|
|
|
|
from commerce.models import ProductImage
|
|
|
|
|
|
def _delete_file_after_commit(field_file):
|
|
if not field_file or not field_file.name:
|
|
return
|
|
storage = field_file.storage
|
|
name = field_file.name
|
|
transaction.on_commit(lambda: storage.delete(name))
|
|
|
|
|
|
@receiver(post_delete, sender=ProductImage)
|
|
def delete_product_image_files(sender, instance, **kwargs):
|
|
_delete_file_after_commit(instance.file)
|
|
_delete_file_after_commit(instance.thumbnail)
|