view bio/badges.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 5892c05886a9
children
line wrap: on
line source
"""This module contains user profile badge-related functionality."""
import logging

from bio.models import Badge
from bio.models import BadgeOwnership


# Numeric ID's for badges that are awarded for user actions:
(CONTRIBUTOR_PIN, CALENDAR_PIN, NEWS_PIN, LINK_PIN, DOWNLOAD_PIN,
        SECURITY_PIN, POTD_PIN, MAP_PIN) = range(8)


def award_badge(badge_id, user):
    """This function awards the badge specified by badge_id
    to the given user. If the user already has the badge,
    the badge count is incremented by one.
    """
    try:
        badge = Badge.objects.get(numeric_id=badge_id)
    except Badge.DoesNotExist:
        logging.error("Can't award badge with numeric_id = %d", badge_id)
        return

    profile = user.profile

    # Does the user already have badges of this type?
    try:
        bo = BadgeOwnership.objects.get(profile=profile, badge=badge)
    except BadgeOwnership.DoesNotExist:
        # No badge of this type, yet
        bo = BadgeOwnership(profile=profile, badge=badge, count=1)
    else:
        # Already have this badge
        bo.count += 1
    bo.save()

    logging.info('Awarded %s with the badge: %s', user.username, badge.name)