comparison gpp/bio/badges.py @ 204:b4305e18d3af

Resolve ticket #74. Add user badges. Some extra credit was done here: also refactored how pending news, links, and downloads are handled.
author Brian Neal <bgneal@gmail.com>
date Sat, 01 May 2010 21:53:59 +0000
parents
children 767cedc7d12a
comparison
equal deleted inserted replaced
203:40e5903903e1 204:b4305e18d3af
1 """This module contains user profile badge-related functionality."""
2
3 from bio.models import Badge
4 from bio.models import BadgeOwnership
5
6
7 # Numeric ID's for badges that are awarded for user actions:
8 (CONTRIBUTOR_PIN, CALENDAR_PIN, NEWS_PIN, LINK_PIN, DOWNLOAD_PIN,
9 SECURITY_PIN) = range(6)
10
11
12 def award_badge(badge_id, user):
13 """This function awards the badge specified by badge_id
14 to the given user. If the user already has the badge,
15 the badge count is incremented by one.
16 """
17 import logging
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()