diff downloads/signals.py @ 664:929d0e637a37

For issue #43, delete uploaded files when downloads are deleted.
author Brian Neal <bgneal@gmail.com>
date Fri, 24 May 2013 15:09:53 -0500
parents ee87ea74d46b
children
line wrap: on
line diff
--- 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')