X-Git-Url: https://git.openstreetmap.org./osqa.git/blobdiff_plain/410bfa05ee36ed1d99356c443a5f3f6aa3ee9578..22236fb67da2f27ceb35ad138f4d534a4fd08251:/forum/badges/base.py diff --git a/forum/badges/base.py b/forum/badges/base.py index fce6b6e..c78a925 100644 --- a/forum/badges/base.py +++ b/forum/badges/base.py @@ -1,10 +1,10 @@ import re from string import lower -from django.contrib.contenttypes.models import ContentType +from django.core.exceptions import MultipleObjectsReturned from django.db.models.signals import post_save -from forum.models import Badge, Node +from forum.models import Badge, Node, Action from forum.actions import AwardAction import logging @@ -20,10 +20,12 @@ class BadgesMeta(type): if not dic.get('abstract', False): if not name in installed: - badge.ondb = Badge(cls=name, type=dic.get('type', Badge.BRONZE)) - badge.ondb.save() + ondb = Badge(cls=name, type=dic.get('type', Badge.BRONZE)) + ondb.save() else: - badge.ondb = installed[name] + ondb = installed[name] + + badge.ondb = ondb.id inst = badge() @@ -36,9 +38,8 @@ class BadgesMeta(type): for action in badge.listen_to: action.hook(hook) - BadgesMeta.by_class[name] = badge - badge.ondb.__dict__['_class'] = inst - BadgesMeta.by_id[badge.ondb.id] = badge + BadgesMeta.by_class[name] = inst + BadgesMeta.by_id[ondb.id] = inst return badge @@ -58,12 +59,21 @@ class AbstractBadge(object): @classmethod def award(cls, user, action, once=False): - if once: - node = None - else: - node = action.node + db_object = Badge.objects.get(id=cls.ondb) + try: + if once: + node = None + awarded = AwardAction.get_for(user, db_object) + else: + node = action.node + awarded = AwardAction.get_for(user, db_object, node) - awarded = AwardAction.get_for(user, node, cls.ondb) + trigger = isinstance(action, Action) and action or None - if not awarded: - AwardAction(user=user, node=node, ip=action.ip).save(data=dict(badge=cls.ondb, trigger=action)) \ No newline at end of file + if not awarded: + AwardAction(user=user, node=node).save(data=dict(badge=db_object, trigger=trigger)) + except MultipleObjectsReturned: + if node: + logging.error('Found multiple %s badges awarded for user %s (%s)' % (self.name, user.username, user.id)) + else: + logging.error('Found multiple %s badges awarded for user %s (%s) and node %s' % (self.name, user.username, user.id, node.id)) \ No newline at end of file