Mercurial > public > sg101
comparison 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 |
comparison
equal
deleted
inserted
replaced
203:40e5903903e1 | 204:b4305e18d3af |
---|---|
1 """ | 1 """ |
2 Signal handler(s) for the bio application. | 2 Signal handler(s) for the bio application. |
3 """ | 3 """ |
4 from django.db.models.signals import post_save | 4 from django.db.models.signals import post_save |
5 from django.contrib.auth.models import User | 5 from django.contrib.auth.models import User |
6 | |
7 import bio.badges | |
6 from bio.models import UserProfile | 8 from bio.models import UserProfile |
9 from donations.models import Donation | |
10 from weblinks.models import Link | |
11 from downloads.models import Download | |
12 from news.models import Story | |
13 | |
7 | 14 |
8 def on_user_save(sender, **kwargs): | 15 def on_user_save(sender, **kwargs): |
9 """ | 16 """ |
10 This signal handler ensures that every User has a corresonding | 17 This signal handler ensures that every User has a corresonding |
11 UserProfile. It is called after User instance is saved. It creates | 18 UserProfile. It is called after User instance is saved. It creates |
12 a UserProfile for the User if the created argument is True. | 19 a UserProfile for the User if the created argument is True. |
13 """ | 20 """ |
14 created = kwargs['created'] | 21 created = kwargs['created'] |
15 if created: | 22 if created: |
16 user = kwargs['instance'] | 23 user = kwargs['instance'] |
17 profile = UserProfile() | 24 profile = bio.models.UserProfile() |
18 profile.user = user | 25 profile.user = user |
19 profile.save() | 26 profile.save() |
20 | 27 |
21 | 28 |
29 def on_donation_save(sender, **kwargs): | |
30 """This function is called after a Donation is saved. | |
31 If the Donation was newly created and not anonymous, | |
32 award the user a contributor pin. | |
33 """ | |
34 if kwargs['created']: | |
35 donation = kwargs['instance'] | |
36 if not donation.is_anonymous and donation.user: | |
37 bio.badges.award_badge(bio.badges.CONTRIBUTOR_PIN, donation.user) | |
38 | |
39 | |
40 def on_link_save(sender, **kwargs): | |
41 """This function is called after a Link is saved. If the Link was newly | |
42 created, award the user a link pin. | |
43 """ | |
44 if kwargs['created']: | |
45 link = kwargs['instance'] | |
46 bio.badges.award_badge(bio.badges.LINK_PIN, link.user) | |
47 | |
48 | |
49 def on_download_save(sender, **kwargs): | |
50 """This function is called after a Download is saved. If the Download was | |
51 newly created, award the user a download pin. | |
52 """ | |
53 if kwargs['created']: | |
54 download = kwargs['instance'] | |
55 bio.badges.award_badge(bio.badges.DOWNLOAD_PIN, download.user) | |
56 | |
57 | |
58 def on_story_save(sender, **kwargs): | |
59 """This function is called after a Story is saved. If the Story was | |
60 newly created, award the user a news pin. | |
61 """ | |
62 if kwargs['created']: | |
63 story = kwargs['instance'] | |
64 bio.badges.award_badge(bio.badges.NEWS_PIN, story.submitter) | |
65 | |
66 | |
22 post_save.connect(on_user_save, sender=User) | 67 post_save.connect(on_user_save, sender=User) |
68 post_save.connect(on_donation_save, sender=Donation) | |
69 post_save.connect(on_link_save, sender=Link) | |
70 post_save.connect(on_download_save, sender=Download) | |
71 post_save.connect(on_story_save, sender=Story) |