bgneal@204: """This module contains user profile badge-related functionality.""" bgneal@316: import logging bgneal@204: bgneal@204: from bio.models import Badge bgneal@204: from bio.models import BadgeOwnership bgneal@204: bgneal@204: bgneal@204: # Numeric ID's for badges that are awarded for user actions: bgneal@204: (CONTRIBUTOR_PIN, CALENDAR_PIN, NEWS_PIN, LINK_PIN, DOWNLOAD_PIN, bgneal@204: SECURITY_PIN) = range(6) bgneal@204: bgneal@204: bgneal@204: def award_badge(badge_id, user): bgneal@204: """This function awards the badge specified by badge_id bgneal@204: to the given user. If the user already has the badge, bgneal@204: the badge count is incremented by one. bgneal@204: """ bgneal@204: try: bgneal@204: badge = Badge.objects.get(numeric_id=badge_id) bgneal@204: except Badge.DoesNotExist: bgneal@316: logging.error("Can't award badge with numeric_id = %d", badge_id) bgneal@204: return bgneal@204: bgneal@204: profile = user.get_profile() bgneal@204: bgneal@204: # Does the user already have badges of this type? bgneal@204: try: bgneal@204: bo = BadgeOwnership.objects.get(profile=profile, badge=badge) bgneal@204: except BadgeOwnership.DoesNotExist: bgneal@204: # No badge of this type, yet bgneal@204: bo = BadgeOwnership(profile=profile, badge=badge, count=1) bgneal@204: else: bgneal@204: # Already have this badge bgneal@204: bo.count += 1 bgneal@204: bo.save()