2 from string import lower
4 from django.contrib.contenttypes.models import ContentType
5 from django.db.models.signals import post_save
7 from forum.models import Badge, Node
8 from forum.actions import AwardAction
12 installed = dict([(b.cls, b) for b in Badge.objects.all()])
14 class BadgesMeta(type):
18 def __new__(mcs, name, bases, dic):
19 badge = type.__new__(mcs, name, bases, dic)
21 if not dic.get('abstract', False):
22 if not name in installed:
23 badge.ondb = Badge(cls=name, type=dic.get('type', Badge.BRONZE))
26 badge.ondb = installed[name]
30 def hook(action, new):
31 user = inst.award_to(action)
34 badge.award(user, action, badge.award_once)
36 for action in badge.listen_to:
39 BadgesMeta.by_class[name] = badge
40 badge.ondb.__dict__['_class'] = inst
41 BadgesMeta.by_id[badge.ondb.id] = badge
45 class AbstractBadge(object):
46 __metaclass__ = BadgesMeta
53 raise NotImplementedError
56 def description(self):
57 raise NotImplementedError
60 def award(cls, user, action, once=False):
63 awarded = AwardAction.get_for(user, cls.ondb)
66 awarded = AwardAction.get_for(user, cls.ondb, node)
69 AwardAction(user=user, node=node, ip=action.ip).save(data=dict(badge=cls.ondb, trigger=action))