annotate bio/receivers.py @ 1078:6bf83070b19f

Convert member search to V3 design.
author Brian Neal <bgneal@gmail.com>
date Fri, 22 Apr 2016 22:20:47 -0500
parents 0b6bf9c5a982
children
rev   line source
bgneal@919 1 """
bgneal@919 2 Signal handlers & signals for the bio application.
bgneal@919 3
bgneal@919 4 """
bgneal@919 5 from django.db.models.signals import post_save
bgneal@919 6 from django.contrib.auth.models import User
bgneal@919 7
bgneal@919 8 import bio.badges
bgneal@919 9 from bio.models import UserProfile
bgneal@919 10
bgneal@919 11 from donations.models import Donation
bgneal@919 12 from weblinks.models import Link
bgneal@919 13 from downloads.models import Download
bgneal@919 14 from news.models import Story
bgneal@919 15 from potd.models import Photo
bgneal@919 16
bgneal@919 17
bgneal@919 18 def on_user_save(sender, **kwargs):
bgneal@919 19 """
bgneal@919 20 This signal handler ensures that every User has a corresonding
bgneal@919 21 UserProfile. It is called after User instance is saved. It creates
bgneal@919 22 a UserProfile for the User if the created argument is True.
bgneal@919 23
bgneal@919 24 """
bgneal@919 25 created = kwargs['created']
bgneal@919 26 if created:
bgneal@919 27 user = kwargs['instance']
bgneal@919 28 profile = UserProfile()
bgneal@919 29 profile.user = user
bgneal@919 30 profile.save()
bgneal@919 31
bgneal@919 32
bgneal@919 33 def on_donation_save(sender, **kwargs):
bgneal@919 34 """
bgneal@919 35 This function is called after a Donation is saved.
bgneal@919 36 If the Donation was newly created and not anonymous,
bgneal@919 37 award the user a contributor pin.
bgneal@919 38
bgneal@919 39 """
bgneal@919 40 if kwargs['created']:
bgneal@919 41 donation = kwargs['instance']
bgneal@919 42 if not donation.is_anonymous and donation.user:
bgneal@919 43 bio.badges.award_badge(bio.badges.CONTRIBUTOR_PIN, donation.user)
bgneal@919 44
bgneal@919 45
bgneal@919 46 def on_link_save(sender, **kwargs):
bgneal@919 47 """
bgneal@919 48 This function is called after a Link is saved. If the Link was newly
bgneal@919 49 created, award the user a link pin.
bgneal@919 50
bgneal@919 51 """
bgneal@919 52 if kwargs['created']:
bgneal@919 53 link = kwargs['instance']
bgneal@919 54 bio.badges.award_badge(bio.badges.LINK_PIN, link.user)
bgneal@919 55
bgneal@919 56
bgneal@919 57 def on_download_save(sender, **kwargs):
bgneal@919 58 """
bgneal@919 59 This function is called after a Download is saved. If the Download was
bgneal@919 60 newly created, award the user a download pin.
bgneal@919 61
bgneal@919 62 """
bgneal@919 63 if kwargs['created']:
bgneal@919 64 download = kwargs['instance']
bgneal@919 65 bio.badges.award_badge(bio.badges.DOWNLOAD_PIN, download.user)
bgneal@919 66
bgneal@919 67
bgneal@919 68 def on_story_save(sender, **kwargs):
bgneal@919 69 """
bgneal@919 70 This function is called after a Story is saved. If the Story was
bgneal@919 71 newly created, award the user a news pin.
bgneal@919 72
bgneal@919 73 """
bgneal@919 74 if kwargs['created']:
bgneal@919 75 story = kwargs['instance']
bgneal@919 76 bio.badges.award_badge(bio.badges.NEWS_PIN, story.submitter)
bgneal@919 77
bgneal@919 78
bgneal@919 79 def on_photo_save(sender, **kwargs):
bgneal@919 80 """
bgneal@919 81 This function is called after a Photo is saved. If the Photo was
bgneal@919 82 newly created, award the user a POTD pin.
bgneal@919 83
bgneal@919 84 """
bgneal@919 85 if kwargs['created']:
bgneal@919 86 photo = kwargs['instance']
bgneal@919 87 bio.badges.award_badge(bio.badges.POTD_PIN, photo.user)
bgneal@919 88
bgneal@919 89
bgneal@919 90 post_save.connect(on_user_save, sender=User, dispatch_uid='bio.receivers')
bgneal@919 91 post_save.connect(on_donation_save, sender=Donation, dispatch_uid='bio.receivers')
bgneal@919 92 post_save.connect(on_link_save, sender=Link, dispatch_uid='bio.receivers')
bgneal@919 93 post_save.connect(on_download_save, sender=Download, dispatch_uid='bio.receivers')
bgneal@919 94 post_save.connect(on_story_save, sender=Story, dispatch_uid='bio.receivers')
bgneal@919 95 post_save.connect(on_photo_save, sender=Photo, dispatch_uid='bio.receivers')