comparison 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
comparison
equal deleted inserted replaced
663:84865fcd7c26 664:929d0e637a37
2 We use signals to compute the denormalized category counts whenever a download 2 We use signals to compute the denormalized category counts whenever a download
3 is saved.""" 3 is saved."""
4 from django.db.models.signals import post_save 4 from django.db.models.signals import post_save
5 from django.db.models.signals import post_delete 5 from django.db.models.signals import post_delete
6 6
7 from downloads.models import Category, Download 7 from downloads.models import Category, Download, PendingDownload
8 8
9 9
10 def on_download_save(sender, **kwargs): 10 def on_download_save(sender, **kwargs):
11 """This function updates the count field for all categories. 11 """This function updates the count field for all categories.
12 It is called whenever a download is saved via a signal. 12 It is called whenever a download is saved via a signal.
27 27
28 28
29 def on_download_delete(sender, **kwargs): 29 def on_download_delete(sender, **kwargs):
30 """This function updates the count field for the download's parent 30 """This function updates the count field for the download's parent
31 category. It is called when a download is deleted via a signal. 31 category. It is called when a download is deleted via a signal.
32
33 We now delete the uploaded file when the download is deleted.
32 """ 34 """
33 # update the parent category 35 # update the parent category
34 download = kwargs['instance'] 36 download = kwargs['instance']
35 cat = download.category 37 cat = download.category
36 cat.count = Download.public_objects.filter(category=cat).count() 38 cat.count = Download.public_objects.filter(category=cat).count()
37 cat.save() 39 cat.save()
38 40
41 # delete the actual file
42 if download.file:
43 download.file.delete(save=False)
44
45
46 def on_pending_download_delete(sender, **kwargs):
47 """Delete the uploaded file if it exists."""
48
49 download = kwargs['instance']
50 # delete the actual file
51 if download.file:
52 download.file.delete(save=False)
53
39 54
40 post_save.connect(on_download_save, sender=Download, 55 post_save.connect(on_download_save, sender=Download,
41 dispatch_uid='downloads.signals') 56 dispatch_uid='downloads.signals')
42 post_delete.connect(on_download_delete, sender=Download, 57 post_delete.connect(on_download_delete, sender=Download,
43 dispatch_uid='downloads.signals') 58 dispatch_uid='downloads.signals')
59 post_delete.connect(on_pending_download_delete, sender=PendingDownload,
60 dispatch_uid='downloads.signals')