uploadcare/pyuploadcare

Image not deleted

zerc opened this issue · 1 comments

zerc commented

Assume we have a Photo model like this:

# myapp/models.py
from pyuploadcare.dj import ImageField
class Photo(models.Model):
    image = ImageField(validators=[size_validator, extension_validator])

Then we delete it from our database. But file in cdn still alive. It easy to reproduce:

>>> from myapp.models import *
>>> a = Photo.objects.get(pk=23)
>>> a.image.cdn_url
u'https://ucarecdn.com/1ada3776-40fd-4193-8aa1-00955a50a26c/'
>>> a.delete()
>>> a.pk
>>> a.image.cdn_url
u'https://ucarecdn.com/1ada3776-40fd-4193-8aa1-00955a50a26c/'

Photo object was successful deleted from database. But image.cdn_url still active.

The solution what i see is subscribe on post_delete signal inside Field.contribute_to_class method's and execute File.delete.

But i think in some cases it right behaviour (e.g. backup). For this case we can specify setting e.g. PYUPLOADCARE_REMOVE_FILES which will be turn on this functionality.

This represents how Django is working with local files.
Cleaning up is user's job.

We may add an optional hook to do that.