]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/badges/base.py
#OSQA-603, fixing the badges count calculation error. After the update of the count...
[osqa.git] / forum / badges / base.py
index fce6b6e30301c542a585f76ba8e2bd799eabb9fc..79fcb523c73d5b1d1ab7c9a25becedc5cac40ca0 100644 (file)
@@ -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
@@ -58,12 +58,20 @@ class AbstractBadge(object):
 
     @classmethod
     def award(cls, user, action, once=False):
-        if once:
-            node = None
-        else:
-            node = action.node
+        try:
+            if once:
+                node = None
+                awarded = AwardAction.get_for(user, cls.ondb)
+            else:
+                node = action.node
+                awarded = AwardAction.get_for(user, cls.ondb, 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=cls.ondb, 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