Skip to content
Browse files

Badge.award_to option to raise BadgeAlreadyAwardedException when needed

  • Loading branch information...
1 parent 9a8a9ef commit f55d06e4eaff9ce0063462e6a8663b0f6e02ba76 @lmorchard committed Mar 4, 2013
Showing with 14 additions and 4 deletions.
  1. +6 −3 badger/models.py
  2. +8 −1 badger/tests/test_models.py
View
9 badger/models.py
@@ -522,7 +522,7 @@ def claim_groups(self):
return DeferredAward.objects.get_claim_groups(badge=self)
def award_to(self, awardee=None, email=None, awarder=None,
- description=''):
+ description='', raise_already_awarded=False):
"""Award this badge to the awardee on the awarder's behalf"""
# If no awarder given, assume this is on the badge creator's behalf.
if not awarder:
@@ -544,9 +544,12 @@ def award_to(self, awardee=None, email=None, awarder=None,
# Otherwise, we'll use the most recently created user
awardee = qs.latest('date_joined')
- # If unique and already awarded, just return the existing award.
if self.unique and self.is_awarded_to(awardee):
- return Award.objects.filter(user=awardee, badge=self)[0]
+ if raise_already_awarded:
+ raise BadgeAlreadyAwardedException()
+ else:
+ # If unique and already awarded, just return the existing
+ return Award.objects.filter(user=awardee, badge=self)[0]
award = Award.objects.create(user=awardee, badge=self,
creator=awarder,
View
9 badger/tests/test_models.py
@@ -94,10 +94,17 @@ def test_award_unique_duplication(self):
unique=True, creator=user)
a = Award.objects.create(badge=b, user=user)
- # award_to should not trigger the exception
+ # award_to should not trigger the exception by default
b.award_to(awardee=user)
try:
+ b.award_to(awardee=user, raise_already_awarded=True)
+ ok_(False, 'BadgeAlreadyAwardedException should have been raised')
+ except BadgeAlreadyAwardedException, e:
+ # The raise_already_awarded flag should raise the exception
+ pass
+
+ try:
a = Award.objects.create(badge=b, user=user)
ok_(False, 'BadgeAlreadyAwardedException should have been raised')
except BadgeAlreadyAwardedException, e:

0 comments on commit f55d06e

Please sign in to comment.
Something went wrong with that request. Please try again.