annotate gpp/bio/signals.py @ 318:c550933ff5b6

Fix a bug where you'd get an error when trying to delete a forum thread (topic does not exist). Apparently when you call topic.delete() the posts would get deleted, but the signal handler for each one would run, and it would try to update the topic's post count or something, but the topic was gone? Reworked the code a bit and explicitly delete the posts first. I also added a sync() call on the parent forum since post counts were not getting adjusted.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 Feb 2011 21:46:52 +0000
parents 3a4bbf9c2cce
children 47f4259ce511
rev   line source
bgneal@44 1 """
bgneal@44 2 Signal handler(s) for the bio application.
bgneal@44 3 """
bgneal@44 4 from django.db.models.signals import post_save
bgneal@44 5 from django.contrib.auth.models import User
bgneal@204 6
bgneal@204 7 import bio.badges
bgneal@44 8 from bio.models import UserProfile
bgneal@204 9 from donations.models import Donation
bgneal@204 10 from weblinks.models import Link
bgneal@204 11 from downloads.models import Download
bgneal@204 12 from news.models import Story
bgneal@204 13
bgneal@44 14
bgneal@44 15 def on_user_save(sender, **kwargs):
bgneal@44 16 """
bgneal@44 17 This signal handler ensures that every User has a corresonding
bgneal@44 18 UserProfile. It is called after User instance is saved. It creates
bgneal@44 19 a UserProfile for the User if the created argument is True.
bgneal@44 20 """
bgneal@44 21 created = kwargs['created']
bgneal@44 22 if created:
bgneal@44 23 user = kwargs['instance']
bgneal@204 24 profile = bio.models.UserProfile()
bgneal@44 25 profile.user = user
bgneal@44 26 profile.save()
bgneal@44 27
bgneal@44 28
bgneal@204 29 def on_donation_save(sender, **kwargs):
bgneal@204 30 """This function is called after a Donation is saved.
bgneal@204 31 If the Donation was newly created and not anonymous,
bgneal@204 32 award the user a contributor pin.
bgneal@204 33 """
bgneal@204 34 if kwargs['created']:
bgneal@204 35 donation = kwargs['instance']
bgneal@204 36 if not donation.is_anonymous and donation.user:
bgneal@204 37 bio.badges.award_badge(bio.badges.CONTRIBUTOR_PIN, donation.user)
bgneal@204 38
bgneal@204 39
bgneal@204 40 def on_link_save(sender, **kwargs):
bgneal@204 41 """This function is called after a Link is saved. If the Link was newly
bgneal@204 42 created, award the user a link pin.
bgneal@204 43 """
bgneal@204 44 if kwargs['created']:
bgneal@204 45 link = kwargs['instance']
bgneal@204 46 bio.badges.award_badge(bio.badges.LINK_PIN, link.user)
bgneal@204 47
bgneal@204 48
bgneal@204 49 def on_download_save(sender, **kwargs):
bgneal@204 50 """This function is called after a Download is saved. If the Download was
bgneal@204 51 newly created, award the user a download pin.
bgneal@204 52 """
bgneal@204 53 if kwargs['created']:
bgneal@204 54 download = kwargs['instance']
bgneal@204 55 bio.badges.award_badge(bio.badges.DOWNLOAD_PIN, download.user)
bgneal@204 56
bgneal@204 57
bgneal@204 58 def on_story_save(sender, **kwargs):
bgneal@204 59 """This function is called after a Story is saved. If the Story was
bgneal@204 60 newly created, award the user a news pin.
bgneal@204 61 """
bgneal@204 62 if kwargs['created']:
bgneal@204 63 story = kwargs['instance']
bgneal@204 64 bio.badges.award_badge(bio.badges.NEWS_PIN, story.submitter)
bgneal@204 65
bgneal@204 66
bgneal@260 67 post_save.connect(on_user_save, sender=User, dispatch_uid='bio.signals')
bgneal@260 68 post_save.connect(on_donation_save, sender=Donation, dispatch_uid='bio.signals')
bgneal@260 69 post_save.connect(on_link_save, sender=Link, dispatch_uid='bio.signals')
bgneal@260 70 post_save.connect(on_download_save, sender=Download, dispatch_uid='bio.signals')
bgneal@260 71 post_save.connect(on_story_save, sender=Story, dispatch_uid='bio.signals')