diff gpp/bio/signals.py @ 204:b4305e18d3af

Resolve ticket #74. Add user badges. Some extra credit was done here: also refactored how pending news, links, and downloads are handled.
author Brian Neal <bgneal@gmail.com>
date Sat, 01 May 2010 21:53:59 +0000
parents 08cd19c1ee50
children 3a4bbf9c2cce
line wrap: on
line diff
--- a/gpp/bio/signals.py	Wed Apr 28 03:00:31 2010 +0000
+++ b/gpp/bio/signals.py	Sat May 01 21:53:59 2010 +0000
@@ -3,7 +3,14 @@
 """
 from django.db.models.signals import post_save
 from django.contrib.auth.models import User
+
+import bio.badges
 from bio.models import UserProfile
+from donations.models import Donation
+from weblinks.models import Link
+from downloads.models import Download
+from news.models import Story
+
 
 def on_user_save(sender, **kwargs):
     """
@@ -14,9 +21,51 @@
     created = kwargs['created']
     if created:
         user = kwargs['instance']
-        profile = UserProfile()
+        profile = bio.models.UserProfile()
         profile.user = user
         profile.save()
 
 
+def on_donation_save(sender, **kwargs):
+    """This function is called after a Donation is saved.
+    If the Donation was newly created and not anonymous,
+    award the user a contributor pin.
+    """
+    if kwargs['created']:
+        donation = kwargs['instance']
+        if not donation.is_anonymous and donation.user:
+            bio.badges.award_badge(bio.badges.CONTRIBUTOR_PIN, donation.user)
+
+
+def on_link_save(sender, **kwargs):
+    """This function is called after a Link is saved. If the Link was newly
+    created, award the user a link pin.
+    """
+    if kwargs['created']:
+        link = kwargs['instance']
+        bio.badges.award_badge(bio.badges.LINK_PIN, link.user)
+
+
+def on_download_save(sender, **kwargs):
+    """This function is called after a Download is saved. If the Download was
+    newly created, award the user a download pin.
+    """
+    if kwargs['created']:
+        download = kwargs['instance']
+        bio.badges.award_badge(bio.badges.DOWNLOAD_PIN, download.user)
+
+
+def on_story_save(sender, **kwargs):
+    """This function is called after a Story is saved. If the Story was
+    newly created, award the user a news pin.
+    """
+    if kwargs['created']:
+        story = kwargs['instance']
+        bio.badges.award_badge(bio.badges.NEWS_PIN, story.submitter)
+
+
 post_save.connect(on_user_save, sender=User)
+post_save.connect(on_donation_save, sender=Donation)
+post_save.connect(on_link_save, sender=Link)
+post_save.connect(on_download_save, sender=Download)
+post_save.connect(on_story_save, sender=Story)