Mercurial > public > sg101
comparison gpp/bio/signals.py @ 400:47f4259ce511
Fixing #197; add a badge for photo of the day.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 26 Mar 2011 21:29:27 +0000 |
parents | 3a4bbf9c2cce |
children | d83296cac940 |
comparison
equal
deleted
inserted
replaced
399:24f1230f3ee3 | 400:47f4259ce511 |
---|---|
8 from bio.models import UserProfile | 8 from bio.models import UserProfile |
9 from donations.models import Donation | 9 from donations.models import Donation |
10 from weblinks.models import Link | 10 from weblinks.models import Link |
11 from downloads.models import Download | 11 from downloads.models import Download |
12 from news.models import Story | 12 from news.models import Story |
13 from potd.models import Photo | |
13 | 14 |
14 | 15 |
15 def on_user_save(sender, **kwargs): | 16 def on_user_save(sender, **kwargs): |
16 """ | 17 """ |
17 This signal handler ensures that every User has a corresonding | 18 This signal handler ensures that every User has a corresonding |
18 UserProfile. It is called after User instance is saved. It creates | 19 UserProfile. It is called after User instance is saved. It creates |
19 a UserProfile for the User if the created argument is True. | 20 a UserProfile for the User if the created argument is True. |
21 | |
20 """ | 22 """ |
21 created = kwargs['created'] | 23 created = kwargs['created'] |
22 if created: | 24 if created: |
23 user = kwargs['instance'] | 25 user = kwargs['instance'] |
24 profile = bio.models.UserProfile() | 26 profile = bio.models.UserProfile() |
25 profile.user = user | 27 profile.user = user |
26 profile.save() | 28 profile.save() |
27 | 29 |
28 | 30 |
29 def on_donation_save(sender, **kwargs): | 31 def on_donation_save(sender, **kwargs): |
30 """This function is called after a Donation is saved. | 32 """ |
33 This function is called after a Donation is saved. | |
31 If the Donation was newly created and not anonymous, | 34 If the Donation was newly created and not anonymous, |
32 award the user a contributor pin. | 35 award the user a contributor pin. |
36 | |
33 """ | 37 """ |
34 if kwargs['created']: | 38 if kwargs['created']: |
35 donation = kwargs['instance'] | 39 donation = kwargs['instance'] |
36 if not donation.is_anonymous and donation.user: | 40 if not donation.is_anonymous and donation.user: |
37 bio.badges.award_badge(bio.badges.CONTRIBUTOR_PIN, donation.user) | 41 bio.badges.award_badge(bio.badges.CONTRIBUTOR_PIN, donation.user) |
38 | 42 |
39 | 43 |
40 def on_link_save(sender, **kwargs): | 44 def on_link_save(sender, **kwargs): |
41 """This function is called after a Link is saved. If the Link was newly | 45 """ |
46 This function is called after a Link is saved. If the Link was newly | |
42 created, award the user a link pin. | 47 created, award the user a link pin. |
48 | |
43 """ | 49 """ |
44 if kwargs['created']: | 50 if kwargs['created']: |
45 link = kwargs['instance'] | 51 link = kwargs['instance'] |
46 bio.badges.award_badge(bio.badges.LINK_PIN, link.user) | 52 bio.badges.award_badge(bio.badges.LINK_PIN, link.user) |
47 | 53 |
48 | 54 |
49 def on_download_save(sender, **kwargs): | 55 def on_download_save(sender, **kwargs): |
50 """This function is called after a Download is saved. If the Download was | 56 """ |
57 This function is called after a Download is saved. If the Download was | |
51 newly created, award the user a download pin. | 58 newly created, award the user a download pin. |
59 | |
52 """ | 60 """ |
53 if kwargs['created']: | 61 if kwargs['created']: |
54 download = kwargs['instance'] | 62 download = kwargs['instance'] |
55 bio.badges.award_badge(bio.badges.DOWNLOAD_PIN, download.user) | 63 bio.badges.award_badge(bio.badges.DOWNLOAD_PIN, download.user) |
56 | 64 |
57 | 65 |
58 def on_story_save(sender, **kwargs): | 66 def on_story_save(sender, **kwargs): |
59 """This function is called after a Story is saved. If the Story was | 67 """ |
68 This function is called after a Story is saved. If the Story was | |
60 newly created, award the user a news pin. | 69 newly created, award the user a news pin. |
70 | |
61 """ | 71 """ |
62 if kwargs['created']: | 72 if kwargs['created']: |
63 story = kwargs['instance'] | 73 story = kwargs['instance'] |
64 bio.badges.award_badge(bio.badges.NEWS_PIN, story.submitter) | 74 bio.badges.award_badge(bio.badges.NEWS_PIN, story.submitter) |
75 | |
76 | |
77 def on_photo_save(sender, **kwargs): | |
78 """ | |
79 This function is called after a Photo is saved. If the Photo was | |
80 newly created, award the user a POTD pin. | |
81 | |
82 """ | |
83 if kwargs['created']: | |
84 photo = kwargs['instance'] | |
85 bio.badges.award_badge(bio.badges.POTD_PIN, photo.user) | |
65 | 86 |
66 | 87 |
67 post_save.connect(on_user_save, sender=User, dispatch_uid='bio.signals') | 88 post_save.connect(on_user_save, sender=User, dispatch_uid='bio.signals') |
68 post_save.connect(on_donation_save, sender=Donation, dispatch_uid='bio.signals') | 89 post_save.connect(on_donation_save, sender=Donation, dispatch_uid='bio.signals') |
69 post_save.connect(on_link_save, sender=Link, dispatch_uid='bio.signals') | 90 post_save.connect(on_link_save, sender=Link, dispatch_uid='bio.signals') |
70 post_save.connect(on_download_save, sender=Download, dispatch_uid='bio.signals') | 91 post_save.connect(on_download_save, sender=Download, dispatch_uid='bio.signals') |
71 post_save.connect(on_story_save, sender=Story, dispatch_uid='bio.signals') | 92 post_save.connect(on_story_save, sender=Story, dispatch_uid='bio.signals') |
93 post_save.connect(on_photo_save, sender=Photo, dispatch_uid='bio.signals') |