annotate bio/signals.py @ 887:9a15f7c27526

Actually save model object upon change. This commit was tested on the comments model. Additional logging added. Added check for Markdown image references. Added TODOs after observing behavior on comments.
author Brian Neal <bgneal@gmail.com>
date Tue, 03 Feb 2015 21:09:44 -0600
parents ee87ea74d46b
children 0b6bf9c5a982
rev   line source
bgneal@44 1 """
bgneal@471 2 Signal handlers & signals for the bio application.
bgneal@471 3
bgneal@44 4 """
bgneal@44 5 from django.db.models.signals import post_save
bgneal@44 6 from django.contrib.auth.models import User
bgneal@471 7 import django.dispatch
bgneal@204 8
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@400 13 from potd.models import Photo
bgneal@204 14
bgneal@44 15
bgneal@44 16 def on_user_save(sender, **kwargs):
bgneal@44 17 """
bgneal@44 18 This signal handler ensures that every User has a corresonding
bgneal@44 19 UserProfile. It is called after User instance is saved. It creates
bgneal@44 20 a UserProfile for the User if the created argument is True.
bgneal@400 21
bgneal@44 22 """
bgneal@44 23 created = kwargs['created']
bgneal@44 24 if created:
bgneal@44 25 user = kwargs['instance']
bgneal@562 26 profile = UserProfile()
bgneal@44 27 profile.user = user
bgneal@44 28 profile.save()
bgneal@44 29
bgneal@44 30
bgneal@204 31 def on_donation_save(sender, **kwargs):
bgneal@400 32 """
bgneal@400 33 This function is called after a Donation is saved.
bgneal@204 34 If the Donation was newly created and not anonymous,
bgneal@204 35 award the user a contributor pin.
bgneal@400 36
bgneal@204 37 """
bgneal@204 38 if kwargs['created']:
bgneal@204 39 donation = kwargs['instance']
bgneal@204 40 if not donation.is_anonymous and donation.user:
bgneal@204 41 bio.badges.award_badge(bio.badges.CONTRIBUTOR_PIN, donation.user)
bgneal@204 42
bgneal@204 43
bgneal@204 44 def on_link_save(sender, **kwargs):
bgneal@400 45 """
bgneal@400 46 This function is called after a Link is saved. If the Link was newly
bgneal@204 47 created, award the user a link pin.
bgneal@400 48
bgneal@204 49 """
bgneal@204 50 if kwargs['created']:
bgneal@204 51 link = kwargs['instance']
bgneal@204 52 bio.badges.award_badge(bio.badges.LINK_PIN, link.user)
bgneal@204 53
bgneal@204 54
bgneal@204 55 def on_download_save(sender, **kwargs):
bgneal@400 56 """
bgneal@400 57 This function is called after a Download is saved. If the Download was
bgneal@204 58 newly created, award the user a download pin.
bgneal@400 59
bgneal@204 60 """
bgneal@204 61 if kwargs['created']:
bgneal@204 62 download = kwargs['instance']
bgneal@204 63 bio.badges.award_badge(bio.badges.DOWNLOAD_PIN, download.user)
bgneal@204 64
bgneal@204 65
bgneal@204 66 def on_story_save(sender, **kwargs):
bgneal@400 67 """
bgneal@400 68 This function is called after a Story is saved. If the Story was
bgneal@204 69 newly created, award the user a news pin.
bgneal@400 70
bgneal@204 71 """
bgneal@204 72 if kwargs['created']:
bgneal@204 73 story = kwargs['instance']
bgneal@204 74 bio.badges.award_badge(bio.badges.NEWS_PIN, story.submitter)
bgneal@204 75
bgneal@204 76
bgneal@400 77 def on_photo_save(sender, **kwargs):
bgneal@400 78 """
bgneal@400 79 This function is called after a Photo is saved. If the Photo was
bgneal@400 80 newly created, award the user a POTD pin.
bgneal@400 81
bgneal@400 82 """
bgneal@400 83 if kwargs['created']:
bgneal@400 84 photo = kwargs['instance']
bgneal@400 85 bio.badges.award_badge(bio.badges.POTD_PIN, photo.user)
bgneal@400 86
bgneal@400 87
bgneal@260 88 post_save.connect(on_user_save, sender=User, dispatch_uid='bio.signals')
bgneal@260 89 post_save.connect(on_donation_save, sender=Donation, dispatch_uid='bio.signals')
bgneal@260 90 post_save.connect(on_link_save, sender=Link, dispatch_uid='bio.signals')
bgneal@260 91 post_save.connect(on_download_save, sender=Download, dispatch_uid='bio.signals')
bgneal@260 92 post_save.connect(on_story_save, sender=Story, dispatch_uid='bio.signals')
bgneal@400 93 post_save.connect(on_photo_save, sender=Photo, dispatch_uid='bio.signals')
bgneal@471 94
bgneal@471 95 # Signals for the bio application
bgneal@471 96 #
bgneal@471 97 # This signal is sent whenever a profile has had its textual content updated.
bgneal@471 98 # The provided arguments to the receiver function are:
bgneal@471 99 # - sender - the profile model instance
bgneal@471 100
bgneal@471 101 profile_content_update = django.dispatch.Signal(providing_args=[])
bgneal@471 102
bgneal@471 103
bgneal@471 104 def notify_profile_content_update(profile):
bgneal@471 105 """
bgneal@471 106 Convenience function to send the profile content update signal.
bgneal@471 107
bgneal@471 108 """
bgneal@471 109 profile_content_update.send_robust(profile)
bgneal@562 110
bgneal@562 111
bgneal@562 112 # To avoid circular imports
bgneal@562 113 import bio.badges
bgneal@562 114 from bio.models import UserProfile