# HG changeset patch # User Brian Neal # Date 1369426193 18000 # Node ID 929d0e637a375f41ac640765113061f22023922b # Parent 84865fcd7c2646e333d74dfcf04ed057f3ce938b For issue #43, delete uploaded files when downloads are deleted. diff -r 84865fcd7c26 -r 929d0e637a37 downloads/signals.py --- a/downloads/signals.py Wed May 15 20:56:10 2013 -0500 +++ b/downloads/signals.py Fri May 24 15:09:53 2013 -0500 @@ -4,7 +4,7 @@ from django.db.models.signals import post_save from django.db.models.signals import post_delete -from downloads.models import Category, Download +from downloads.models import Category, Download, PendingDownload def on_download_save(sender, **kwargs): @@ -29,6 +29,8 @@ def on_download_delete(sender, **kwargs): """This function updates the count field for the download's parent category. It is called when a download is deleted via a signal. + + We now delete the uploaded file when the download is deleted. """ # update the parent category download = kwargs['instance'] @@ -36,8 +38,23 @@ cat.count = Download.public_objects.filter(category=cat).count() cat.save() + # delete the actual file + if download.file: + download.file.delete(save=False) + + +def on_pending_download_delete(sender, **kwargs): + """Delete the uploaded file if it exists.""" + + download = kwargs['instance'] + # delete the actual file + if download.file: + download.file.delete(save=False) + post_save.connect(on_download_save, sender=Download, dispatch_uid='downloads.signals') post_delete.connect(on_download_delete, sender=Download, dispatch_uid='downloads.signals') +post_delete.connect(on_pending_download_delete, sender=PendingDownload, + dispatch_uid='downloads.signals')