# HG changeset patch # User Brian Neal # Date 1428456042 18000 # Node ID 1a832625c0476b0d4f84056cd00f91ff7f7c6881 # Parent 6d08b1476a52a16497c21b1cac9eb8bdc13c5f3a Downloads app refactor. For Django 1.7.7 upgrade. diff -r 6d08b1476a52 -r 1a832625c047 downloads/__init__.py --- a/downloads/__init__.py Tue Apr 07 20:16:06 2015 -0500 +++ b/downloads/__init__.py Tue Apr 07 20:20:42 2015 -0500 @@ -1,1 +1,1 @@ -import signals +default_app_config = 'downloads.apps.DownloadsConfig' diff -r 6d08b1476a52 -r 1a832625c047 downloads/apps.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/downloads/apps.py Tue Apr 07 20:20:42 2015 -0500 @@ -0,0 +1,8 @@ +from django.apps import AppConfig + + +class DownloadsConfig(AppConfig): + name = 'downloads' + + def ready(self): + import downloads.receivers diff -r 6d08b1476a52 -r 1a832625c047 downloads/receivers.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/downloads/receivers.py Tue Apr 07 20:20:42 2015 -0500 @@ -0,0 +1,63 @@ +"""Signal handlers for the downloads application. + +We use signals to compute the denormalized category counts whenever a download +is saved. + +""" +from django.db.models.signals import post_save +from django.db.models.signals import post_delete + +from downloads.models import Category, Download, PendingDownload + + +def on_download_save(sender, **kwargs): + """This function updates the count field for all categories. + It is called whenever a download is saved via a signal. + """ + if kwargs['created']: + # we only have to update the parent category + download = kwargs['instance'] + cat = download.category + cat.count = Download.public_objects.filter(category=cat).count() + cat.save() + else: + # update all categories just to be safe (an existing download could + # have been moved from one category to another + cats = Category.objects.all() + for cat in cats: + cat.count = Download.public_objects.filter(category=cat).count() + cat.save() + + +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'] + cat = download.category + 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.receivers') +post_delete.connect(on_download_delete, sender=Download, + dispatch_uid='downloads.receivers') +post_delete.connect(on_pending_download_delete, sender=PendingDownload, + dispatch_uid='downloads.receivers') diff -r 6d08b1476a52 -r 1a832625c047 downloads/signals.py --- a/downloads/signals.py Tue Apr 07 20:16:06 2015 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,60 +0,0 @@ -"""Signals for the downloads application. -We use signals to compute the denormalized category counts whenever a download -is saved.""" -from django.db.models.signals import post_save -from django.db.models.signals import post_delete - -from downloads.models import Category, Download, PendingDownload - - -def on_download_save(sender, **kwargs): - """This function updates the count field for all categories. - It is called whenever a download is saved via a signal. - """ - if kwargs['created']: - # we only have to update the parent category - download = kwargs['instance'] - cat = download.category - cat.count = Download.public_objects.filter(category=cat).count() - cat.save() - else: - # update all categories just to be safe (an existing download could - # have been moved from one category to another - cats = Category.objects.all() - for cat in cats: - cat.count = Download.public_objects.filter(category=cat).count() - cat.save() - - -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'] - cat = download.category - 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')