Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions django_gamification/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,27 @@ def save(self, *args, **kwargs):
badge.points = self.points
badge.save()

def award(self, interface):
"""
Awards this badge to a user.
"""
badges = interface.badge_set.filter(badge_definition=self)
badge = None
# There should only be one. Otherwise something horrible has happened.
# We can handle not having one, where we just need to create one.
if len(badges) == 1:
badge = badges[0]
# Ignore previously-acquired badges.
if badge.acquired:
return
elif len(badges) == 0:
badge = Badge.objects.create_badge(self, interface)
else:
raise "Expected one badge per definition but found two."
return
badge.award()
badge.save()


class Progression(models.Model):
"""
Expand Down