comparison bio/receivers.py @ 919:0b6bf9c5a982

Bio app refactor. For Django 1.7.7 upgrade.
author Brian Neal <bgneal@gmail.com>
date Mon, 06 Apr 2015 20:33:10 -0500
parents
children
comparison
equal deleted inserted replaced
918:886234943aed 919:0b6bf9c5a982
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
8 import bio.badges
9 from bio.models import UserProfile
10
11 from donations.models import Donation
12 from weblinks.models import Link
13 from downloads.models import Download
14 from news.models import Story
15 from potd.models import Photo
16
17
18 def on_user_save(sender, **kwargs):
19 """
20 This signal handler ensures that every User has a corresonding
21 UserProfile. It is called after User instance is saved. It creates
22 a UserProfile for the User if the created argument is True.
23
24 """
25 created = kwargs['created']
26 if created:
27 user = kwargs['instance']
28 profile = UserProfile()
29 profile.user = user
30 profile.save()
31
32
33 def on_donation_save(sender, **kwargs):
34 """
35 This function is called after a Donation is saved.
36 If the Donation was newly created and not anonymous,
37 award the user a contributor pin.
38
39 """
40 if kwargs['created']:
41 donation = kwargs['instance']
42 if not donation.is_anonymous and donation.user:
43 bio.badges.award_badge(bio.badges.CONTRIBUTOR_PIN, donation.user)
44
45
46 def on_link_save(sender, **kwargs):
47 """
48 This function is called after a Link is saved. If the Link was newly
49 created, award the user a link pin.
50
51 """
52 if kwargs['created']:
53 link = kwargs['instance']
54 bio.badges.award_badge(bio.badges.LINK_PIN, link.user)
55
56
57 def on_download_save(sender, **kwargs):
58 """
59 This function is called after a Download is saved. If the Download was
60 newly created, award the user a download pin.
61
62 """
63 if kwargs['created']:
64 download = kwargs['instance']
65 bio.badges.award_badge(bio.badges.DOWNLOAD_PIN, download.user)
66
67
68 def on_story_save(sender, **kwargs):
69 """
70 This function is called after a Story is saved. If the Story was
71 newly created, award the user a news pin.
72
73 """
74 if kwargs['created']:
75 story = kwargs['instance']
76 bio.badges.award_badge(bio.badges.NEWS_PIN, story.submitter)
77
78
79 def on_photo_save(sender, **kwargs):
80 """
81 This function is called after a Photo is saved. If the Photo was
82 newly created, award the user a POTD pin.
83
84 """
85 if kwargs['created']:
86 photo = kwargs['instance']
87 bio.badges.award_badge(bio.badges.POTD_PIN, photo.user)
88
89
90 post_save.connect(on_user_save, sender=User, dispatch_uid='bio.receivers')
91 post_save.connect(on_donation_save, sender=Donation, dispatch_uid='bio.receivers')
92 post_save.connect(on_link_save, sender=Link, dispatch_uid='bio.receivers')
93 post_save.connect(on_download_save, sender=Download, dispatch_uid='bio.receivers')
94 post_save.connect(on_story_save, sender=Story, dispatch_uid='bio.receivers')
95 post_save.connect(on_photo_save, sender=Photo, dispatch_uid='bio.receivers')