bgneal@919: """ bgneal@919: Signal handlers & signals for the bio application. bgneal@919: bgneal@919: """ bgneal@919: from django.db.models.signals import post_save bgneal@919: from django.contrib.auth.models import User bgneal@919: bgneal@919: import bio.badges bgneal@919: from bio.models import UserProfile bgneal@919: bgneal@919: from donations.models import Donation bgneal@919: from weblinks.models import Link bgneal@919: from downloads.models import Download bgneal@919: from news.models import Story bgneal@919: from potd.models import Photo bgneal@919: bgneal@919: bgneal@919: def on_user_save(sender, **kwargs): bgneal@919: """ bgneal@919: This signal handler ensures that every User has a corresonding bgneal@919: UserProfile. It is called after User instance is saved. It creates bgneal@919: a UserProfile for the User if the created argument is True. bgneal@919: bgneal@919: """ bgneal@919: created = kwargs['created'] bgneal@919: if created: bgneal@919: user = kwargs['instance'] bgneal@919: profile = UserProfile() bgneal@919: profile.user = user bgneal@919: profile.save() bgneal@919: bgneal@919: bgneal@919: def on_donation_save(sender, **kwargs): bgneal@919: """ bgneal@919: This function is called after a Donation is saved. bgneal@919: If the Donation was newly created and not anonymous, bgneal@919: award the user a contributor pin. bgneal@919: bgneal@919: """ bgneal@919: if kwargs['created']: bgneal@919: donation = kwargs['instance'] bgneal@919: if not donation.is_anonymous and donation.user: bgneal@919: bio.badges.award_badge(bio.badges.CONTRIBUTOR_PIN, donation.user) bgneal@919: bgneal@919: bgneal@919: def on_link_save(sender, **kwargs): bgneal@919: """ bgneal@919: This function is called after a Link is saved. If the Link was newly bgneal@919: created, award the user a link pin. bgneal@919: bgneal@919: """ bgneal@919: if kwargs['created']: bgneal@919: link = kwargs['instance'] bgneal@919: bio.badges.award_badge(bio.badges.LINK_PIN, link.user) bgneal@919: bgneal@919: bgneal@919: def on_download_save(sender, **kwargs): bgneal@919: """ bgneal@919: This function is called after a Download is saved. If the Download was bgneal@919: newly created, award the user a download pin. bgneal@919: bgneal@919: """ bgneal@919: if kwargs['created']: bgneal@919: download = kwargs['instance'] bgneal@919: bio.badges.award_badge(bio.badges.DOWNLOAD_PIN, download.user) bgneal@919: bgneal@919: bgneal@919: def on_story_save(sender, **kwargs): bgneal@919: """ bgneal@919: This function is called after a Story is saved. If the Story was bgneal@919: newly created, award the user a news pin. bgneal@919: bgneal@919: """ bgneal@919: if kwargs['created']: bgneal@919: story = kwargs['instance'] bgneal@919: bio.badges.award_badge(bio.badges.NEWS_PIN, story.submitter) bgneal@919: bgneal@919: bgneal@919: def on_photo_save(sender, **kwargs): bgneal@919: """ bgneal@919: This function is called after a Photo is saved. If the Photo was bgneal@919: newly created, award the user a POTD pin. bgneal@919: bgneal@919: """ bgneal@919: if kwargs['created']: bgneal@919: photo = kwargs['instance'] bgneal@919: bio.badges.award_badge(bio.badges.POTD_PIN, photo.user) bgneal@919: bgneal@919: bgneal@919: post_save.connect(on_user_save, sender=User, dispatch_uid='bio.receivers') bgneal@919: post_save.connect(on_donation_save, sender=Donation, dispatch_uid='bio.receivers') bgneal@919: post_save.connect(on_link_save, sender=Link, dispatch_uid='bio.receivers') bgneal@919: post_save.connect(on_download_save, sender=Download, dispatch_uid='bio.receivers') bgneal@919: post_save.connect(on_story_save, sender=Story, dispatch_uid='bio.receivers') bgneal@919: post_save.connect(on_photo_save, sender=Photo, dispatch_uid='bio.receivers')