2011-07-28 08:44:05 +04:00
|
|
|
from django.conf import settings
|
|
|
|
from django.db.models import signals
|
|
|
|
|
2011-07-30 05:40:49 +04:00
|
|
|
import badger
|
2011-08-02 22:41:56 +04:00
|
|
|
from badger.models import Badge, Award, Progress
|
2011-07-28 08:44:05 +04:00
|
|
|
|
|
|
|
|
|
|
|
def update_badges(badge_data, overwrite=False):
|
2013-08-20 01:46:55 +04:00
|
|
|
"""Creates or updates list of badges
|
|
|
|
|
|
|
|
:arg badge_data: list of dicts. keys in the dict correspond to the
|
|
|
|
Badge model class. Also, you can pass in ``prerequisites``.
|
|
|
|
:arg overwrite: whether or not to overwrite the existing badge
|
|
|
|
|
|
|
|
:returns: list of Badge instances---one per dict passed in
|
|
|
|
|
|
|
|
"""
|
2011-07-28 08:44:05 +04:00
|
|
|
badges = []
|
|
|
|
for data in badge_data:
|
2013-08-20 01:46:55 +04:00
|
|
|
badges.append(update_badge(data, overwrite=overwrite))
|
2011-07-31 21:29:21 +04:00
|
|
|
return badges
|
2011-07-30 05:40:49 +04:00
|
|
|
|
|
|
|
|
2013-01-16 03:25:20 +04:00
|
|
|
def update_badge(data_in, overwrite=False):
|
|
|
|
# Clone the data, because we might delete fields
|
|
|
|
data = dict(**data_in)
|
|
|
|
|
2011-07-31 21:29:21 +04:00
|
|
|
# If there are prerequisites, ensure they're real badges and remove
|
|
|
|
# from the set of data fields.
|
|
|
|
if 'prerequisites' not in data:
|
|
|
|
prerequisites = None
|
|
|
|
else:
|
|
|
|
prerequisites = [get_badge(n)
|
|
|
|
for n in data.get('prerequisites', [])]
|
|
|
|
del data['prerequisites']
|
2011-07-30 05:40:49 +04:00
|
|
|
|
2011-07-31 21:29:21 +04:00
|
|
|
badge, created = Badge.objects.get_or_create(title=data['title'],
|
|
|
|
defaults=data)
|
2011-07-30 05:40:49 +04:00
|
|
|
|
2011-07-31 21:29:21 +04:00
|
|
|
# If overwriting, and not just created, then save with current fields.
|
|
|
|
if overwrite and not created:
|
2014-02-04 01:06:03 +04:00
|
|
|
for k, v in data.items():
|
|
|
|
setattr(badge, k, v)
|
|
|
|
badge.save()
|
2011-07-30 05:40:49 +04:00
|
|
|
|
2011-07-31 21:29:21 +04:00
|
|
|
# Set prerequisites if overwriting, or badge is newly created.
|
|
|
|
if (overwrite or created) and prerequisites:
|
|
|
|
badge.prerequisites.clear()
|
|
|
|
badge.prerequisites.add(*prerequisites)
|
2011-07-30 05:40:49 +04:00
|
|
|
|
2011-07-31 21:29:21 +04:00
|
|
|
return badge
|
|
|
|
|
|
|
|
|
|
|
|
def get_badge(slug_or_badge):
|
2013-08-20 01:46:55 +04:00
|
|
|
"""Return badge specified by slug or by instance
|
|
|
|
|
|
|
|
:arg slug_or_badge: slug or Badge instance
|
|
|
|
|
|
|
|
:returns: Badge instance
|
|
|
|
|
|
|
|
"""
|
2011-07-31 21:29:21 +04:00
|
|
|
if isinstance(slug_or_badge, Badge):
|
|
|
|
b = slug_or_badge
|
|
|
|
else:
|
|
|
|
b = Badge.objects.get(slug=slug_or_badge)
|
|
|
|
return b
|
|
|
|
|
|
|
|
|
|
|
|
def award_badge(slug_or_badge, awardee, awarder=None):
|
2013-08-20 01:46:55 +04:00
|
|
|
"""Award a badge to an awardee, with optional awarder
|
|
|
|
|
|
|
|
:arg slug_or_badge: slug or Badge instance to award
|
|
|
|
:arg awardee: User this Badge is awarded to
|
|
|
|
:arg awarder: User who awarded this Badge
|
|
|
|
|
|
|
|
:returns: Award instance
|
|
|
|
|
|
|
|
:raise BadgeAwardNotAllowedexception: ?
|
|
|
|
|
|
|
|
:raise BadgeAlreadyAwardedException: if the badge is unique and
|
|
|
|
has already been awarded to this user
|
|
|
|
|
|
|
|
"""
|
2011-07-31 21:29:21 +04:00
|
|
|
b = get_badge(slug_or_badge)
|
2013-08-20 01:46:55 +04:00
|
|
|
return b.award_to(awardee=awardee, awarder=awarder)
|
2011-07-31 21:29:21 +04:00
|
|
|
|
|
|
|
|
|
|
|
def get_progress(slug_or_badge, user):
|
2013-08-20 01:46:55 +04:00
|
|
|
"""Get a progress record for a badge and awardee
|
|
|
|
|
|
|
|
:arg slug_or_badge: slug or Badge instance
|
|
|
|
:arg user: User to check progress for
|
|
|
|
|
|
|
|
:returns: Progress instance
|
|
|
|
|
|
|
|
"""
|
2011-07-31 21:29:21 +04:00
|
|
|
b = get_badge(slug_or_badge)
|
|
|
|
return b.progress_for(user)
|