Mercurial > public > sg101
comparison bio/signals.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/bio/signals.py@98b373ca09f3 |
children | 0b6bf9c5a982 |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """ | |
2 Signal handlers & signals for the bio application. | |
3 | |
4 """ | |
5 from django.db.models.signals import post_save | |
6 from django.contrib.auth.models import User | |
7 import django.dispatch | |
8 | |
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 from potd.models import Photo | |
14 | |
15 | |
16 def on_user_save(sender, **kwargs): | |
17 """ | |
18 This signal handler ensures that every User has a corresonding | |
19 UserProfile. It is called after User instance is saved. It creates | |
20 a UserProfile for the User if the created argument is True. | |
21 | |
22 """ | |
23 created = kwargs['created'] | |
24 if created: | |
25 user = kwargs['instance'] | |
26 profile = UserProfile() | |
27 profile.user = user | |
28 profile.save() | |
29 | |
30 | |
31 def on_donation_save(sender, **kwargs): | |
32 """ | |
33 This function is called after a Donation is saved. | |
34 If the Donation was newly created and not anonymous, | |
35 award the user a contributor pin. | |
36 | |
37 """ | |
38 if kwargs['created']: | |
39 donation = kwargs['instance'] | |
40 if not donation.is_anonymous and donation.user: | |
41 bio.badges.award_badge(bio.badges.CONTRIBUTOR_PIN, donation.user) | |
42 | |
43 | |
44 def on_link_save(sender, **kwargs): | |
45 """ | |
46 This function is called after a Link is saved. If the Link was newly | |
47 created, award the user a link pin. | |
48 | |
49 """ | |
50 if kwargs['created']: | |
51 link = kwargs['instance'] | |
52 bio.badges.award_badge(bio.badges.LINK_PIN, link.user) | |
53 | |
54 | |
55 def on_download_save(sender, **kwargs): | |
56 """ | |
57 This function is called after a Download is saved. If the Download was | |
58 newly created, award the user a download pin. | |
59 | |
60 """ | |
61 if kwargs['created']: | |
62 download = kwargs['instance'] | |
63 bio.badges.award_badge(bio.badges.DOWNLOAD_PIN, download.user) | |
64 | |
65 | |
66 def on_story_save(sender, **kwargs): | |
67 """ | |
68 This function is called after a Story is saved. If the Story was | |
69 newly created, award the user a news pin. | |
70 | |
71 """ | |
72 if kwargs['created']: | |
73 story = kwargs['instance'] | |
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) | |
86 | |
87 | |
88 post_save.connect(on_user_save, sender=User, dispatch_uid='bio.signals') | |
89 post_save.connect(on_donation_save, sender=Donation, dispatch_uid='bio.signals') | |
90 post_save.connect(on_link_save, sender=Link, dispatch_uid='bio.signals') | |
91 post_save.connect(on_download_save, sender=Download, 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') | |
94 | |
95 # Signals for the bio application | |
96 # | |
97 # This signal is sent whenever a profile has had its textual content updated. | |
98 # The provided arguments to the receiver function are: | |
99 # - sender - the profile model instance | |
100 | |
101 profile_content_update = django.dispatch.Signal(providing_args=[]) | |
102 | |
103 | |
104 def notify_profile_content_update(profile): | |
105 """ | |
106 Convenience function to send the profile content update signal. | |
107 | |
108 """ | |
109 profile_content_update.send_robust(profile) | |
110 | |
111 | |
112 # To avoid circular imports | |
113 import bio.badges | |
114 from bio.models import UserProfile |