view bio/receivers.py @ 943:cf9918328c64

Haystack tweaks for Django 1.7.7. I had to upgrade to Haystack 2.3.1 to get it to work with Django 1.7.7. I also had to update the Xapian backend. But I ran into problems. On my laptop anyway (Ubuntu 14.0.4), xapian gets mad when search terms are greater than 245 chars (or something) when indexing. So I created a custom field that would simply omit terms greater than 64 chars and used this field everywhere I previously used a CharField. Secondly, the custom search form was broken now. Something changed in the Xapian backend and exact searches stopped working. Fortunately the auto_query (which I was using originally and broke during an upgrade) started working again. So I cut the search form back over to doing an auto_query. I kept the form the same (3 fields) because I didn't want to change the form and I think it's better that way.
author Brian Neal <bgneal@gmail.com>
date Wed, 13 May 2015 20:25:07 -0500
parents 0b6bf9c5a982
children
line wrap: on
line source
"""
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')