# HG changeset patch # User Brian Neal # Date 1428455766 18000 # Node ID 6d08b1476a52a16497c21b1cac9eb8bdc13c5f3a # Parent 5902dc5e58a327a0f4627206ace3662528636a0f Weblinks app refactor. For Django 1.7.7 upgrade. diff -r 5902dc5e58a3 -r 6d08b1476a52 weblinks/__init__.py --- a/weblinks/__init__.py Tue Apr 07 20:11:32 2015 -0500 +++ b/weblinks/__init__.py Tue Apr 07 20:16:06 2015 -0500 @@ -1,1 +1,1 @@ -import signals +default_app_config = 'weblinks.apps.WeblinksConfig' diff -r 5902dc5e58a3 -r 6d08b1476a52 weblinks/apps.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/weblinks/apps.py Tue Apr 07 20:16:06 2015 -0500 @@ -0,0 +1,9 @@ +from django.apps import AppConfig + + +class WeblinksConfig(AppConfig): + name = 'weblinks' + verbose_name = 'Web Links' + + def ready(self): + import weblinks.receivers diff -r 5902dc5e58a3 -r 6d08b1476a52 weblinks/receivers.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/weblinks/receivers.py Tue Apr 07 20:16:06 2015 -0500 @@ -0,0 +1,43 @@ +"""Signal handlers for the weblinks application. + +We use signals to compute the denormalized category counts whenever a weblink +is saved. +""" +from django.db.models.signals import post_save +from django.db.models.signals import post_delete + +from weblinks.models import Category, Link + + +def on_link_save(sender, **kwargs): + """This function updates the count field for all categories. + It is called whenever a link is saved via a signal. + """ + if kwargs['created']: + # we only have to update the parent category + link = kwargs['instance'] + cat = link.category + cat.count = Link.public_objects.filter(category=cat).count() + cat.save() + else: + # update all categories just to be safe (an existing link could + # have been moved from one category to another + cats = Category.objects.all() + for cat in cats: + cat.count = Link.public_objects.filter(category=cat).count() + cat.save() + + +def on_link_delete(sender, **kwargs): + """This function updates the count field for the link's parent + category. It is called when a link is deleted via a signal. + """ + # update the parent category + link = kwargs['instance'] + cat = link.category + cat.count = Link.public_objects.filter(category=cat).count() + cat.save() + + +post_save.connect(on_link_save, sender=Link, dispatch_uid='weblinks.receivers') +post_delete.connect(on_link_delete, sender=Link, dispatch_uid='weblinks.receivers') diff -r 5902dc5e58a3 -r 6d08b1476a52 weblinks/signals.py --- a/weblinks/signals.py Tue Apr 07 20:11:32 2015 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,41 +0,0 @@ -"""Signals for the weblinks application. -We use signals to compute the denormalized category counts whenever a weblink -is saved.""" -from django.db.models.signals import post_save -from django.db.models.signals import post_delete - -from weblinks.models import Category, Link - - -def on_link_save(sender, **kwargs): - """This function updates the count field for all categories. - It is called whenever a link is saved via a signal. - """ - if kwargs['created']: - # we only have to update the parent category - link = kwargs['instance'] - cat = link.category - cat.count = Link.public_objects.filter(category=cat).count() - cat.save() - else: - # update all categories just to be safe (an existing link could - # have been moved from one category to another - cats = Category.objects.all() - for cat in cats: - cat.count = Link.public_objects.filter(category=cat).count() - cat.save() - - -def on_link_delete(sender, **kwargs): - """This function updates the count field for the link's parent - category. It is called when a link is deleted via a signal. - """ - # update the parent category - link = kwargs['instance'] - cat = link.category - cat.count = Link.public_objects.filter(category=cat).count() - cat.save() - - -post_save.connect(on_link_save, sender=Link, dispatch_uid='weblinks.signals') -post_delete.connect(on_link_delete, sender=Link, dispatch_uid='weblinks.signals')