view bio/badges.py @ 821:71db8076dc3d

Bandmap WIP: geocoding integrated with add form. Add form works. Before submitting the form, client side JS makes a geocode request to Google and populates hidden lat/lon fields with the result. Successfully created a model instance on the server side. Still need to update admin dashboard, admin approval, and give out badges for adding bands to the map. Once that is done, then work on displaying the map with filtering.
author Brian Neal <bgneal@gmail.com>
date Tue, 23 Sep 2014 20:40:31 -0500
parents 9e803323a0d0
children 5892c05886a9
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) = range(7)


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)