annotate bio/badges.py @ 629:f4c043cf55ac
Wiki integration. Requests don't always have sessions.
In particular this occurs when a request is made without a trailing slash.
The Common middleware redirects when this happens, and the middleware
process_request() processing stops before a session can get added.
So just set an attribute on the request object for each operation.
This seemed weird to me at first, but there are plenty of examples of this
in the Django code base already.
author |
Brian Neal <bgneal@gmail.com> |
date |
Tue, 13 Nov 2012 13:50:06 -0600 |
parents |
ee87ea74d46b |
children |
9e803323a0d0 |
rev |
line source |
bgneal@204
|
1 """This module contains user profile badge-related functionality."""
|
bgneal@316
|
2 import logging
|
bgneal@204
|
3
|
bgneal@204
|
4 from bio.models import Badge
|
bgneal@204
|
5 from bio.models import BadgeOwnership
|
bgneal@204
|
6
|
bgneal@204
|
7
|
bgneal@204
|
8 # Numeric ID's for badges that are awarded for user actions:
|
bgneal@204
|
9 (CONTRIBUTOR_PIN, CALENDAR_PIN, NEWS_PIN, LINK_PIN, DOWNLOAD_PIN,
|
bgneal@400
|
10 SECURITY_PIN, POTD_PIN) = range(7)
|
bgneal@204
|
11
|
bgneal@204
|
12
|
bgneal@204
|
13 def award_badge(badge_id, user):
|
bgneal@204
|
14 """This function awards the badge specified by badge_id
|
bgneal@204
|
15 to the given user. If the user already has the badge,
|
bgneal@204
|
16 the badge count is incremented by one.
|
bgneal@204
|
17 """
|
bgneal@204
|
18 try:
|
bgneal@204
|
19 badge = Badge.objects.get(numeric_id=badge_id)
|
bgneal@204
|
20 except Badge.DoesNotExist:
|
bgneal@316
|
21 logging.error("Can't award badge with numeric_id = %d", badge_id)
|
bgneal@204
|
22 return
|
bgneal@204
|
23
|
bgneal@204
|
24 profile = user.get_profile()
|
bgneal@204
|
25
|
bgneal@204
|
26 # Does the user already have badges of this type?
|
bgneal@204
|
27 try:
|
bgneal@204
|
28 bo = BadgeOwnership.objects.get(profile=profile, badge=badge)
|
bgneal@204
|
29 except BadgeOwnership.DoesNotExist:
|
bgneal@204
|
30 # No badge of this type, yet
|
bgneal@204
|
31 bo = BadgeOwnership(profile=profile, badge=badge, count=1)
|
bgneal@204
|
32 else:
|
bgneal@204
|
33 # Already have this badge
|
bgneal@204
|
34 bo.count += 1
|
bgneal@204
|
35 bo.save()
|
bgneal@400
|
36
|
bgneal@400
|
37 logging.info('Awarded %s with the badge: %s', user.username, badge.name)
|