comparison bio/badges.py @ 581:ee87ea74d46b

For Django 1.4, rearranged project structure for new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 May 2012 17:10:48 -0500
parents gpp/bio/badges.py@47f4259ce511
children 9e803323a0d0
comparison
equal deleted inserted replaced
580:c525f3e0b5d0 581:ee87ea74d46b
1 """This module contains user profile badge-related functionality."""
2 import logging
3
4 from bio.models import Badge
5 from bio.models import BadgeOwnership
6
7
8 # Numeric ID's for badges that are awarded for user actions:
9 (CONTRIBUTOR_PIN, CALENDAR_PIN, NEWS_PIN, LINK_PIN, DOWNLOAD_PIN,
10 SECURITY_PIN, POTD_PIN) = range(7)
11
12
13 def award_badge(badge_id, user):
14 """This function awards the badge specified by badge_id
15 to the given user. If the user already has the badge,
16 the badge count is incremented by one.
17 """
18 try:
19 badge = Badge.objects.get(numeric_id=badge_id)
20 except Badge.DoesNotExist:
21 logging.error("Can't award badge with numeric_id = %d", badge_id)
22 return
23
24 profile = user.get_profile()
25
26 # Does the user already have badges of this type?
27 try:
28 bo = BadgeOwnership.objects.get(profile=profile, badge=badge)
29 except BadgeOwnership.DoesNotExist:
30 # No badge of this type, yet
31 bo = BadgeOwnership(profile=profile, badge=badge, count=1)
32 else:
33 # Already have this badge
34 bo.count += 1
35 bo.save()
36
37 logging.info('Awarded %s with the badge: %s', user.username, badge.name)