changeset 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 886234943aed
children 5902dc5e58a3
files bio/__init__.py bio/apps.py bio/models.py bio/receivers.py bio/signals.py
diffstat 5 files changed, 107 insertions(+), 100 deletions(-) [+]
line wrap: on
line diff
--- a/bio/__init__.py	Mon Apr 06 20:14:10 2015 -0500
+++ b/bio/__init__.py	Mon Apr 06 20:33:10 2015 -0500
@@ -1,1 +1,1 @@
-import signals
+default_app_config = 'bio.apps.BioConfig'
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bio/apps.py	Mon Apr 06 20:33:10 2015 -0500
@@ -0,0 +1,9 @@
+from django.apps import AppConfig
+
+
+class BioConfig(AppConfig):
+    name = 'bio'
+    verbose_name = 'Biography'
+
+    def ready(self):
+        import bio.receivers
--- a/bio/models.py	Mon Apr 06 20:14:10 2015 -0500
+++ b/bio/models.py	Mon Apr 06 20:33:10 2015 -0500
@@ -13,6 +13,7 @@
 
 from core.markup import SiteMarkup
 import bio.flags
+from bio.signals import notify_profile_content_update
 
 
 # These are the secondary user status enumeration values. 
@@ -221,6 +222,3 @@
         if self.count == 1:
             return u"1 %s" % self.badge.name
         return u"%d %ss" % (self.count, self.badge.name)
-
-# Put down here to avoid a circular import
-from bio.signals import notify_profile_content_update
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bio/receivers.py	Mon Apr 06 20:33:10 2015 -0500
@@ -0,0 +1,95 @@
+"""
+Signal handlers & signals for the bio application.
+
+"""
+from django.db.models.signals import post_save
+from django.contrib.auth.models import User
+
+import bio.badges
+from bio.models import UserProfile
+
+from donations.models import Donation
+from weblinks.models import Link
+from downloads.models import Download
+from news.models import Story
+from potd.models import Photo
+
+
+def on_user_save(sender, **kwargs):
+    """
+    This signal handler ensures that every User has a corresonding
+    UserProfile. It is called after User instance is saved. It creates
+    a UserProfile for the User if the created argument is True.
+
+    """
+    created = kwargs['created']
+    if created:
+        user = kwargs['instance']
+        profile = UserProfile()
+        profile.user = user
+        profile.save()
+
+
+def on_donation_save(sender, **kwargs):
+    """
+    This function is called after a Donation is saved.
+    If the Donation was newly created and not anonymous,
+    award the user a contributor pin.
+
+    """
+    if kwargs['created']:
+        donation = kwargs['instance']
+        if not donation.is_anonymous and donation.user:
+            bio.badges.award_badge(bio.badges.CONTRIBUTOR_PIN, donation.user)
+
+
+def on_link_save(sender, **kwargs):
+    """
+    This function is called after a Link is saved. If the Link was newly
+    created, award the user a link pin.
+
+    """
+    if kwargs['created']:
+        link = kwargs['instance']
+        bio.badges.award_badge(bio.badges.LINK_PIN, link.user)
+
+
+def on_download_save(sender, **kwargs):
+    """
+    This function is called after a Download is saved. If the Download was
+    newly created, award the user a download pin.
+
+    """
+    if kwargs['created']:
+        download = kwargs['instance']
+        bio.badges.award_badge(bio.badges.DOWNLOAD_PIN, download.user)
+
+
+def on_story_save(sender, **kwargs):
+    """
+    This function is called after a Story is saved. If the Story was
+    newly created, award the user a news pin.
+
+    """
+    if kwargs['created']:
+        story = kwargs['instance']
+        bio.badges.award_badge(bio.badges.NEWS_PIN, story.submitter)
+
+
+def on_photo_save(sender, **kwargs):
+    """
+    This function is called after a Photo is saved. If the Photo was
+    newly created, award the user a POTD pin.
+
+    """
+    if kwargs['created']:
+        photo = kwargs['instance']
+        bio.badges.award_badge(bio.badges.POTD_PIN, photo.user)
+
+
+post_save.connect(on_user_save, sender=User, dispatch_uid='bio.receivers')
+post_save.connect(on_donation_save, sender=Donation, dispatch_uid='bio.receivers')
+post_save.connect(on_link_save, sender=Link, dispatch_uid='bio.receivers')
+post_save.connect(on_download_save, sender=Download, dispatch_uid='bio.receivers')
+post_save.connect(on_story_save, sender=Story, dispatch_uid='bio.receivers')
+post_save.connect(on_photo_save, sender=Photo, dispatch_uid='bio.receivers')
--- a/bio/signals.py	Mon Apr 06 20:14:10 2015 -0500
+++ b/bio/signals.py	Mon Apr 06 20:33:10 2015 -0500
@@ -1,98 +1,8 @@
 """
-Signal handlers & signals for the bio application.
-
+Signals for the bio application
 """
-from django.db.models.signals import post_save
-from django.contrib.auth.models import User
 import django.dispatch
 
-from donations.models import Donation
-from weblinks.models import Link
-from downloads.models import Download
-from news.models import Story
-from potd.models import Photo
-
-
-def on_user_save(sender, **kwargs):
-    """
-    This signal handler ensures that every User has a corresonding
-    UserProfile. It is called after User instance is saved. It creates
-    a UserProfile for the User if the created argument is True.
-
-    """
-    created = kwargs['created']
-    if created:
-        user = kwargs['instance']
-        profile = UserProfile()
-        profile.user = user
-        profile.save()
-
-
-def on_donation_save(sender, **kwargs):
-    """
-    This function is called after a Donation is saved.
-    If the Donation was newly created and not anonymous,
-    award the user a contributor pin.
-
-    """
-    if kwargs['created']:
-        donation = kwargs['instance']
-        if not donation.is_anonymous and donation.user:
-            bio.badges.award_badge(bio.badges.CONTRIBUTOR_PIN, donation.user)
-
-
-def on_link_save(sender, **kwargs):
-    """
-    This function is called after a Link is saved. If the Link was newly
-    created, award the user a link pin.
-
-    """
-    if kwargs['created']:
-        link = kwargs['instance']
-        bio.badges.award_badge(bio.badges.LINK_PIN, link.user)
-
-
-def on_download_save(sender, **kwargs):
-    """
-    This function is called after a Download is saved. If the Download was
-    newly created, award the user a download pin.
-
-    """
-    if kwargs['created']:
-        download = kwargs['instance']
-        bio.badges.award_badge(bio.badges.DOWNLOAD_PIN, download.user)
-
-
-def on_story_save(sender, **kwargs):
-    """
-    This function is called after a Story is saved. If the Story was
-    newly created, award the user a news pin.
-
-    """
-    if kwargs['created']:
-        story = kwargs['instance']
-        bio.badges.award_badge(bio.badges.NEWS_PIN, story.submitter)
-
-
-def on_photo_save(sender, **kwargs):
-    """
-    This function is called after a Photo is saved. If the Photo was
-    newly created, award the user a POTD pin.
-
-    """
-    if kwargs['created']:
-        photo = kwargs['instance']
-        bio.badges.award_badge(bio.badges.POTD_PIN, photo.user)
-
-
-post_save.connect(on_user_save, sender=User, dispatch_uid='bio.signals')
-post_save.connect(on_donation_save, sender=Donation, dispatch_uid='bio.signals')
-post_save.connect(on_link_save, sender=Link, dispatch_uid='bio.signals')
-post_save.connect(on_download_save, sender=Download, dispatch_uid='bio.signals')
-post_save.connect(on_story_save, sender=Story, dispatch_uid='bio.signals')
-post_save.connect(on_photo_save, sender=Photo, dispatch_uid='bio.signals')
-
-# Signals for the bio application
 #
 # This signal is sent whenever a profile has had its textual content updated.
 # The provided arguments to the receiver function are:
@@ -107,8 +17,3 @@
 
     """
     profile_content_update.send_robust(profile)
-
-
-# To avoid circular imports
-import bio.badges
-from bio.models import UserProfile