bgneal@44: """
bgneal@471: Signal handlers & signals for the bio application.
bgneal@471: 
bgneal@44: """
bgneal@44: from django.db.models.signals import post_save
bgneal@44: from django.contrib.auth.models import User
bgneal@471: import django.dispatch
bgneal@204: 
bgneal@204: from donations.models import Donation
bgneal@204: from weblinks.models import Link
bgneal@204: from downloads.models import Download
bgneal@204: from news.models import Story
bgneal@400: from potd.models import Photo
bgneal@204: 
bgneal@44: 
bgneal@44: def on_user_save(sender, **kwargs):
bgneal@44:     """
bgneal@44:     This signal handler ensures that every User has a corresonding
bgneal@44:     UserProfile. It is called after User instance is saved. It creates
bgneal@44:     a UserProfile for the User if the created argument is True.
bgneal@400: 
bgneal@44:     """
bgneal@44:     created = kwargs['created']
bgneal@44:     if created:
bgneal@44:         user = kwargs['instance']
bgneal@562:         profile = UserProfile()
bgneal@44:         profile.user = user
bgneal@44:         profile.save()
bgneal@44: 
bgneal@44: 
bgneal@204: def on_donation_save(sender, **kwargs):
bgneal@400:     """
bgneal@400:     This function is called after a Donation is saved.
bgneal@204:     If the Donation was newly created and not anonymous,
bgneal@204:     award the user a contributor pin.
bgneal@400: 
bgneal@204:     """
bgneal@204:     if kwargs['created']:
bgneal@204:         donation = kwargs['instance']
bgneal@204:         if not donation.is_anonymous and donation.user:
bgneal@204:             bio.badges.award_badge(bio.badges.CONTRIBUTOR_PIN, donation.user)
bgneal@204: 
bgneal@204: 
bgneal@204: def on_link_save(sender, **kwargs):
bgneal@400:     """
bgneal@400:     This function is called after a Link is saved. If the Link was newly
bgneal@204:     created, award the user a link pin.
bgneal@400: 
bgneal@204:     """
bgneal@204:     if kwargs['created']:
bgneal@204:         link = kwargs['instance']
bgneal@204:         bio.badges.award_badge(bio.badges.LINK_PIN, link.user)
bgneal@204: 
bgneal@204: 
bgneal@204: def on_download_save(sender, **kwargs):
bgneal@400:     """
bgneal@400:     This function is called after a Download is saved. If the Download was
bgneal@204:     newly created, award the user a download pin.
bgneal@400: 
bgneal@204:     """
bgneal@204:     if kwargs['created']:
bgneal@204:         download = kwargs['instance']
bgneal@204:         bio.badges.award_badge(bio.badges.DOWNLOAD_PIN, download.user)
bgneal@204: 
bgneal@204: 
bgneal@204: def on_story_save(sender, **kwargs):
bgneal@400:     """
bgneal@400:     This function is called after a Story is saved. If the Story was
bgneal@204:     newly created, award the user a news pin.
bgneal@400: 
bgneal@204:     """
bgneal@204:     if kwargs['created']:
bgneal@204:         story = kwargs['instance']
bgneal@204:         bio.badges.award_badge(bio.badges.NEWS_PIN, story.submitter)
bgneal@204: 
bgneal@204: 
bgneal@400: def on_photo_save(sender, **kwargs):
bgneal@400:     """
bgneal@400:     This function is called after a Photo is saved. If the Photo was
bgneal@400:     newly created, award the user a POTD pin.
bgneal@400: 
bgneal@400:     """
bgneal@400:     if kwargs['created']:
bgneal@400:         photo = kwargs['instance']
bgneal@400:         bio.badges.award_badge(bio.badges.POTD_PIN, photo.user)
bgneal@400: 
bgneal@400: 
bgneal@260: post_save.connect(on_user_save, sender=User, dispatch_uid='bio.signals')
bgneal@260: post_save.connect(on_donation_save, sender=Donation, dispatch_uid='bio.signals')
bgneal@260: post_save.connect(on_link_save, sender=Link, dispatch_uid='bio.signals')
bgneal@260: post_save.connect(on_download_save, sender=Download, dispatch_uid='bio.signals')
bgneal@260: post_save.connect(on_story_save, sender=Story, dispatch_uid='bio.signals')
bgneal@400: post_save.connect(on_photo_save, sender=Photo, dispatch_uid='bio.signals')
bgneal@471: 
bgneal@471: # Signals for the bio application
bgneal@471: #
bgneal@471: # This signal is sent whenever a profile has had its textual content updated.
bgneal@471: # The provided arguments to the receiver function are:
bgneal@471: #   - sender - the profile model instance
bgneal@471: 
bgneal@471: profile_content_update = django.dispatch.Signal(providing_args=[])
bgneal@471: 
bgneal@471: 
bgneal@471: def notify_profile_content_update(profile):
bgneal@471:     """
bgneal@471:     Convenience function to send the profile content update signal.
bgneal@471: 
bgneal@471:     """
bgneal@471:     profile_content_update.send_robust(profile)
bgneal@562: 
bgneal@562: 
bgneal@562: # To avoid circular imports
bgneal@562: import bio.badges
bgneal@562: from bio.models import UserProfile