Moving / renaming some utils from __init__.py to utils.py
This commit is contained in:
Родитель
5ff33b1b4a
Коммит
5632a117b3
|
@ -15,24 +15,3 @@ def autodiscover():
|
|||
except ImportError:
|
||||
continue
|
||||
badges_mod.register_signals()
|
||||
|
||||
|
||||
def badge(slug_or_badge):
|
||||
"""Find a badge by slug or by instance"""
|
||||
if isinstance(slug_or_badge, Badge):
|
||||
b = slug_or_badge
|
||||
else:
|
||||
b = Badge.objects.get(slug=slug_or_badge)
|
||||
return b
|
||||
|
||||
|
||||
def award(slug_or_badge, awardee, awarder=None):
|
||||
"""Award a badge to an awardee, with optional awarder"""
|
||||
b = badge(slug_or_badge)
|
||||
return b.award_to(awardee, awarder)
|
||||
|
||||
|
||||
def progress(slug_or_badge, user):
|
||||
"""Get a progress record for a badge and awardee"""
|
||||
b = badge(slug_or_badge)
|
||||
return b.progress_for(user)
|
||||
|
|
|
@ -11,6 +11,7 @@ from nose.plugins.attrib import attr
|
|||
from . import BadgerTestCase
|
||||
|
||||
import badger
|
||||
from badger.utils import get_badge, award_badge
|
||||
import badger_test
|
||||
import badger_test.badges
|
||||
|
||||
|
@ -35,22 +36,22 @@ class BadgesPyTest(BadgerTestCase):
|
|||
|
||||
def test_badges_from_fixture(self):
|
||||
"""Badges can be created via fixture"""
|
||||
b = Badge.objects.get(slug="test-1")
|
||||
b = get_badge("test-1")
|
||||
eq_("Test #1", b.title)
|
||||
b = Badge.objects.get(slug="button-clicker")
|
||||
b = get_badge("button-clicker")
|
||||
eq_("Button Clicker", b.title)
|
||||
b = Badge.objects.get(slug="first-post")
|
||||
b = get_badge("first-post")
|
||||
eq_("First post!", b.title)
|
||||
|
||||
def test_badges_from_code(self):
|
||||
"""Badges can be created in code"""
|
||||
b = Badge.objects.get(slug="test-2")
|
||||
b = get_badge("test-2")
|
||||
eq_("Test #2", b.title)
|
||||
b = Badge.objects.get(slug="awesomeness")
|
||||
b = get_badge("awesomeness")
|
||||
eq_("Awesomeness (you have it)", b.title)
|
||||
b = Badge.objects.get(slug="250-words")
|
||||
b = get_badge("250-words")
|
||||
eq_("250 Words", b.title)
|
||||
b = Badge.objects.get(slug="master-badger")
|
||||
b = get_badge("master-badger")
|
||||
eq_("Master Badger", b.title)
|
||||
|
||||
def test_badge_awarded_on_model_create(self):
|
||||
|
@ -58,7 +59,7 @@ class BadgesPyTest(BadgerTestCase):
|
|||
user = self._get_user()
|
||||
post = GuestbookEntry(message="This is my first post", creator=user)
|
||||
post.save()
|
||||
b = Badge.objects.get(slug='first-post')
|
||||
b = get_badge('first-post')
|
||||
ok_(b.is_awarded_to(user))
|
||||
|
||||
# "first-post" badge should be unique
|
||||
|
@ -71,7 +72,7 @@ class BadgesPyTest(BadgerTestCase):
|
|||
created"""
|
||||
user = self._get_user()
|
||||
|
||||
b = badger.badge('250-words')
|
||||
b = get_badge('250-words')
|
||||
|
||||
# Post 5 words in progress...
|
||||
GuestbookEntry.objects.create(creator=user,
|
||||
|
@ -108,7 +109,7 @@ class BadgesPyTest(BadgerTestCase):
|
|||
created, but the tracking is done via percentage"""
|
||||
user = self._get_user()
|
||||
|
||||
b = badger.badge('250-words-by-percent')
|
||||
b = get_badge('250-words-by-percent')
|
||||
|
||||
# Post 5 words in progress...
|
||||
GuestbookEntry.objects.create(creator=user,
|
||||
|
@ -142,11 +143,11 @@ class BadgesPyTest(BadgerTestCase):
|
|||
def test_metabadge_awarded(self):
|
||||
"""Upon completing collection of badges, award a meta-badge"""
|
||||
user = self._get_user()
|
||||
badger.award('test-1', user)
|
||||
badger.award('test-2', user)
|
||||
badger.award('awesomeness', user)
|
||||
badger.award('button-clicker', user)
|
||||
ok_(badger.badge('master-badger').is_awarded_to(user))
|
||||
award_badge('test-1', user)
|
||||
award_badge('test-2', user)
|
||||
award_badge('awesomeness', user)
|
||||
award_badge('button-clicker', user)
|
||||
ok_(get_badge('master-badger').is_awarded_to(user))
|
||||
|
||||
def _get_user(self, username="tester", email="tester@example.com",
|
||||
password="trustno1"):
|
||||
|
|
|
@ -9,14 +9,18 @@ def update_badges(badge_data, overwrite=False):
|
|||
"""Update badges from array of dicts, with option to overwrite existing"""
|
||||
badges = []
|
||||
for data in badge_data:
|
||||
badges.append(update_badge(data))
|
||||
return badges
|
||||
|
||||
|
||||
def update_badge(data, overwrite=False):
|
||||
# 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 = [badger.badge(id)
|
||||
for id in data.get('prerequisites', [])]
|
||||
prerequisites = [get_badge(n)
|
||||
for n in data.get('prerequisites', [])]
|
||||
del data['prerequisites']
|
||||
|
||||
badge, created = Badge.objects.get_or_create(title=data['title'],
|
||||
|
@ -31,6 +35,25 @@ def update_badges(badge_data, overwrite=False):
|
|||
badge.prerequisites.clear()
|
||||
badge.prerequisites.add(*prerequisites)
|
||||
|
||||
badges.append(badge)
|
||||
return badge
|
||||
|
||||
return badges
|
||||
|
||||
def get_badge(slug_or_badge):
|
||||
"""Find a badge by slug or by instance"""
|
||||
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):
|
||||
"""Award a badge to an awardee, with optional awarder"""
|
||||
b = get_badge(slug_or_badge)
|
||||
return b.award_to(awardee, awarder)
|
||||
|
||||
|
||||
def get_progress(slug_or_badge, user):
|
||||
"""Get a progress record for a badge and awardee"""
|
||||
b = get_badge(slug_or_badge)
|
||||
return b.progress_for(user)
|
||||
|
|
|
@ -5,7 +5,8 @@ from django.db.models.signals import post_save
|
|||
from .models import GuestbookEntry
|
||||
|
||||
import badger
|
||||
from badger import utils
|
||||
import badger.utils
|
||||
from badger.utils import get_badge, award_badge, get_progress
|
||||
from badger.models import Badge, Nomination, Award, Progress
|
||||
from badger.signals import badge_was_awarded
|
||||
|
||||
|
@ -36,7 +37,7 @@ def update_badges(overwrite=False):
|
|||
'button-clicker')),
|
||||
|
||||
]
|
||||
return utils.update_badges(badge_data, overwrite)
|
||||
return badger.utils.update_badges(badge_data, overwrite)
|
||||
|
||||
|
||||
def on_guestbook_post(sender, **kwargs):
|
||||
|
@ -44,10 +45,10 @@ def on_guestbook_post(sender, **kwargs):
|
|||
created = kwargs['created']
|
||||
|
||||
if created:
|
||||
badger.award('first-post', o.creator)
|
||||
award_badge('first-post', o.creator)
|
||||
|
||||
# Increment progress counter and track the completion condition ourselves.
|
||||
b = badger.badge('250-words')
|
||||
b = get_badge('250-words')
|
||||
p = b.progress_for(o.creator).increment_by(o.word_count)
|
||||
if p.counter >= 250:
|
||||
b.award_to(o.creator)
|
||||
|
@ -55,7 +56,7 @@ def on_guestbook_post(sender, **kwargs):
|
|||
# Update percentage from total word count, and Progress will award on 100%
|
||||
total_word_count = (GuestbookEntry.objects.filter(creator=o.creator)
|
||||
.aggregate(s=Sum('word_count'))['s'])
|
||||
(badger.progress("250-words-by-percent", o.creator)
|
||||
(get_progress("250-words-by-percent", o.creator)
|
||||
.update_percent(total_word_count, 250))
|
||||
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче