annotate downloads/receivers.py @ 977:ba0f4a5f1fca

Fix MarkItUp! CSS for removing image icon.
author Brian Neal <bgneal@gmail.com>
date Mon, 05 Oct 2015 20:20:34 -0500
parents 1a832625c047
children
rev   line source
bgneal@922 1 """Signal handlers for the downloads application.
bgneal@922 2
bgneal@203 3 We use signals to compute the denormalized category counts whenever a download
bgneal@922 4 is saved.
bgneal@922 5
bgneal@922 6 """
bgneal@203 7 from django.db.models.signals import post_save
bgneal@203 8 from django.db.models.signals import post_delete
bgneal@203 9
bgneal@664 10 from downloads.models import Category, Download, PendingDownload
bgneal@203 11
bgneal@203 12
bgneal@203 13 def on_download_save(sender, **kwargs):
bgneal@203 14 """This function updates the count field for all categories.
bgneal@203 15 It is called whenever a download is saved via a signal.
bgneal@203 16 """
bgneal@203 17 if kwargs['created']:
bgneal@203 18 # we only have to update the parent category
bgneal@203 19 download = kwargs['instance']
bgneal@203 20 cat = download.category
bgneal@203 21 cat.count = Download.public_objects.filter(category=cat).count()
bgneal@203 22 cat.save()
bgneal@203 23 else:
bgneal@203 24 # update all categories just to be safe (an existing download could
bgneal@203 25 # have been moved from one category to another
bgneal@203 26 cats = Category.objects.all()
bgneal@203 27 for cat in cats:
bgneal@203 28 cat.count = Download.public_objects.filter(category=cat).count()
bgneal@203 29 cat.save()
bgneal@203 30
bgneal@203 31
bgneal@203 32 def on_download_delete(sender, **kwargs):
bgneal@203 33 """This function updates the count field for the download's parent
bgneal@203 34 category. It is called when a download is deleted via a signal.
bgneal@664 35
bgneal@664 36 We now delete the uploaded file when the download is deleted.
bgneal@203 37 """
bgneal@203 38 # update the parent category
bgneal@203 39 download = kwargs['instance']
bgneal@203 40 cat = download.category
bgneal@203 41 cat.count = Download.public_objects.filter(category=cat).count()
bgneal@203 42 cat.save()
bgneal@203 43
bgneal@664 44 # delete the actual file
bgneal@664 45 if download.file:
bgneal@664 46 download.file.delete(save=False)
bgneal@664 47
bgneal@664 48
bgneal@664 49 def on_pending_download_delete(sender, **kwargs):
bgneal@664 50 """Delete the uploaded file if it exists."""
bgneal@664 51
bgneal@664 52 download = kwargs['instance']
bgneal@664 53 # delete the actual file
bgneal@664 54 if download.file:
bgneal@664 55 download.file.delete(save=False)
bgneal@664 56
bgneal@203 57
bgneal@260 58 post_save.connect(on_download_save, sender=Download,
bgneal@922 59 dispatch_uid='downloads.receivers')
bgneal@260 60 post_delete.connect(on_download_delete, sender=Download,
bgneal@922 61 dispatch_uid='downloads.receivers')
bgneal@664 62 post_delete.connect(on_pending_download_delete, sender=PendingDownload,
bgneal@922 63 dispatch_uid='downloads.receivers')