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 import bio.badges
|
bgneal@44
|
10 from bio.models import UserProfile
|
bgneal@204
|
11 from donations.models import Donation
|
bgneal@204
|
12 from weblinks.models import Link
|
bgneal@204
|
13 from downloads.models import Download
|
bgneal@204
|
14 from news.models import Story
|
bgneal@400
|
15 from potd.models import Photo
|
bgneal@204
|
16
|
bgneal@44
|
17
|
bgneal@44
|
18 def on_user_save(sender, **kwargs):
|
bgneal@44
|
19 """
|
bgneal@44
|
20 This signal handler ensures that every User has a corresonding
|
bgneal@44
|
21 UserProfile. It is called after User instance is saved. It creates
|
bgneal@44
|
22 a UserProfile for the User if the created argument is True.
|
bgneal@400
|
23
|
bgneal@44
|
24 """
|
bgneal@44
|
25 created = kwargs['created']
|
bgneal@44
|
26 if created:
|
bgneal@44
|
27 user = kwargs['instance']
|
bgneal@204
|
28 profile = bio.models.UserProfile()
|
bgneal@44
|
29 profile.user = user
|
bgneal@44
|
30 profile.save()
|
bgneal@44
|
31
|
bgneal@44
|
32
|
bgneal@204
|
33 def on_donation_save(sender, **kwargs):
|
bgneal@400
|
34 """
|
bgneal@400
|
35 This function is called after a Donation is saved.
|
bgneal@204
|
36 If the Donation was newly created and not anonymous,
|
bgneal@204
|
37 award the user a contributor pin.
|
bgneal@400
|
38
|
bgneal@204
|
39 """
|
bgneal@204
|
40 if kwargs['created']:
|
bgneal@204
|
41 donation = kwargs['instance']
|
bgneal@204
|
42 if not donation.is_anonymous and donation.user:
|
bgneal@204
|
43 bio.badges.award_badge(bio.badges.CONTRIBUTOR_PIN, donation.user)
|
bgneal@204
|
44
|
bgneal@204
|
45
|
bgneal@204
|
46 def on_link_save(sender, **kwargs):
|
bgneal@400
|
47 """
|
bgneal@400
|
48 This function is called after a Link is saved. If the Link was newly
|
bgneal@204
|
49 created, award the user a link pin.
|
bgneal@400
|
50
|
bgneal@204
|
51 """
|
bgneal@204
|
52 if kwargs['created']:
|
bgneal@204
|
53 link = kwargs['instance']
|
bgneal@204
|
54 bio.badges.award_badge(bio.badges.LINK_PIN, link.user)
|
bgneal@204
|
55
|
bgneal@204
|
56
|
bgneal@204
|
57 def on_download_save(sender, **kwargs):
|
bgneal@400
|
58 """
|
bgneal@400
|
59 This function is called after a Download is saved. If the Download was
|
bgneal@204
|
60 newly created, award the user a download pin.
|
bgneal@400
|
61
|
bgneal@204
|
62 """
|
bgneal@204
|
63 if kwargs['created']:
|
bgneal@204
|
64 download = kwargs['instance']
|
bgneal@204
|
65 bio.badges.award_badge(bio.badges.DOWNLOAD_PIN, download.user)
|
bgneal@204
|
66
|
bgneal@204
|
67
|
bgneal@204
|
68 def on_story_save(sender, **kwargs):
|
bgneal@400
|
69 """
|
bgneal@400
|
70 This function is called after a Story is saved. If the Story was
|
bgneal@204
|
71 newly created, award the user a news pin.
|
bgneal@400
|
72
|
bgneal@204
|
73 """
|
bgneal@204
|
74 if kwargs['created']:
|
bgneal@204
|
75 story = kwargs['instance']
|
bgneal@204
|
76 bio.badges.award_badge(bio.badges.NEWS_PIN, story.submitter)
|
bgneal@204
|
77
|
bgneal@204
|
78
|
bgneal@400
|
79 def on_photo_save(sender, **kwargs):
|
bgneal@400
|
80 """
|
bgneal@400
|
81 This function is called after a Photo is saved. If the Photo was
|
bgneal@400
|
82 newly created, award the user a POTD pin.
|
bgneal@400
|
83
|
bgneal@400
|
84 """
|
bgneal@400
|
85 if kwargs['created']:
|
bgneal@400
|
86 photo = kwargs['instance']
|
bgneal@400
|
87 bio.badges.award_badge(bio.badges.POTD_PIN, photo.user)
|
bgneal@400
|
88
|
bgneal@400
|
89
|
bgneal@260
|
90 post_save.connect(on_user_save, sender=User, dispatch_uid='bio.signals')
|
bgneal@260
|
91 post_save.connect(on_donation_save, sender=Donation, dispatch_uid='bio.signals')
|
bgneal@260
|
92 post_save.connect(on_link_save, sender=Link, dispatch_uid='bio.signals')
|
bgneal@260
|
93 post_save.connect(on_download_save, sender=Download, dispatch_uid='bio.signals')
|
bgneal@260
|
94 post_save.connect(on_story_save, sender=Story, dispatch_uid='bio.signals')
|
bgneal@400
|
95 post_save.connect(on_photo_save, sender=Photo, dispatch_uid='bio.signals')
|
bgneal@471
|
96
|
bgneal@471
|
97 # Signals for the bio application
|
bgneal@471
|
98 #
|
bgneal@471
|
99 # This signal is sent whenever a profile has had its textual content updated.
|
bgneal@471
|
100 # The provided arguments to the receiver function are:
|
bgneal@471
|
101 # - sender - the profile model instance
|
bgneal@471
|
102
|
bgneal@471
|
103 profile_content_update = django.dispatch.Signal(providing_args=[])
|
bgneal@471
|
104
|
bgneal@471
|
105
|
bgneal@471
|
106 def notify_profile_content_update(profile):
|
bgneal@471
|
107 """
|
bgneal@471
|
108 Convenience function to send the profile content update signal.
|
bgneal@471
|
109
|
bgneal@471
|
110 """
|
bgneal@471
|
111 profile_content_update.send_robust(profile)
|