]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/reputation.py
OSQA - 19
[osqa.git] / forum / reputation.py
index fd5ce633363ffc269b0b676cb31fc7f706dcd525..71f9d65fd7a5c8abb0c99bb52eb5b553f3083ad6 100644 (file)
@@ -1,5 +1,6 @@
 from django.db.models.signals import post_save\r
-from forum.models.meta import vote_canceled\r
+from forum.models.base import mark_canceled\r
+from forum.models.answer import answer_accepted, answer_accepted_canceled\r
 \r
 from forum.models import *\r
 from forum.const import *\r
@@ -28,28 +29,36 @@ def on_flagged_item(instance, created, **kwargs):
 \r
 post_save.connect(on_flagged_item, sender=FlaggedItem)\r
 \r
+def on_answer_accepted(answer, user, **kwargs):\r
+    if user == answer.question.author and not user == answer.author:\r
+        user.reputes.create(\r
+            value=int(settings.REP_GAIN_BY_ACCEPTING), question=answer.question,\r
+            reputation_type=TYPE_REPUTATION_GAIN_BY_ACCEPTING_ANSWER)\r
 \r
-def on_answer_accepted_switch(instance, created, **kwargs):\r
-    if not created and 'accepted' in instance.get_dirty_fields() and (\r
-            not instance.accepted_by == instance.question.author):\r
-        repute_type, repute_value = instance.accepted and (\r
-            TYPE_REPUTATION_GAIN_BY_ANSWER_ACCEPTED, int(settings.REP_GAIN_BY_ACCEPTED)) or (\r
-            TYPE_REPUTATION_LOST_BY_ACCEPTED_ANSWER_CANCELED, -int(settings.REP_LOST_BY_ACCEPTED_CANCELED))\r
+    if not user == answer.author:\r
+        answer.author.reputes.create(\r
+            value=int(settings.REP_GAIN_BY_ACCEPTED), question=answer.question,\r
+            reputation_type=TYPE_REPUTATION_GAIN_BY_ANSWER_ACCEPTED)\r
 \r
-        instance.author.reputes.create(value=repute_value, question=instance.question, reputation_type=repute_type)\r
-        \r
-        if instance.accepted_by == instance.question.author:\r
-            repute_type, repute_value = instance.accepted and (\r
-            TYPE_REPUTATION_GAIN_BY_ACCEPTING_ANSWER, int(settings.REP_GAIN_BY_ACCEPTING)) or (\r
-            TYPE_REPUTATION_LOST_BY_CANCELLING_ACCEPTED_ANSWER, -int(settings.REP_LOST_BY_CANCELING_ACCEPTED))\r
+answer_accepted.connect(on_answer_accepted)\r
 \r
-            instance.question.author.reputes.create(value=repute_value, question=instance.question, reputation_type=repute_type)\r
 \r
-post_save.connect(on_answer_accepted_switch, sender=Answer)\r
+def on_answer_accepted_canceled(answer, user, **kwargs):\r
+    if user == answer.accepted_by:\r
+        user.reputes.create(\r
+            value=-int(settings.REP_LOST_BY_CANCELING_ACCEPTED), question=answer.question,\r
+            reputation_type=TYPE_REPUTATION_LOST_BY_CANCELLING_ACCEPTED_ANSWER)\r
+\r
+    if not user == answer.author:\r
+        answer.author.reputes.create(\r
+            value=-int(settings.REP_LOST_BY_ACCEPTED_CANCELED), question=answer.question,\r
+            reputation_type=TYPE_REPUTATION_LOST_BY_ACCEPTED_ANSWER_CANCELED)\r
+\r
+answer_accepted_canceled.connect(on_answer_accepted)\r
 \r
 \r
 def on_vote(instance, created, **kwargs):\r
-    if created and not instance.content_object.wiki:\r
+    if created and (instance.content_object.node_type in ("question", "answer") and not instance.content_object.wiki):\r
         post = instance.content_object.leaf\r
         question = (post.__class__ == Question) and post or post.question\r
 \r
@@ -70,7 +79,7 @@ post_save.connect(on_vote, sender=Vote)
 \r
 \r
 def on_vote_canceled(instance, **kwargs):\r
-    if not instance.content_object.wiki:\r
+    if instance.content_object.node_type in ("question", "answer") and not instance.content_object.wiki:\r
         post = instance.content_object.leaf\r
         question = (post.__class__ == Question) and post or post.question\r
 \r
@@ -84,7 +93,7 @@ def on_vote_canceled(instance, **kwargs):
 \r
         post.author.reputes.create(value=repute_value, question=question, reputation_type=repute_type)\r
 \r
-vote_canceled.connect(on_vote_canceled)\r
+mark_canceled.connect(on_vote_canceled, sender=Vote)\r
 \r
 \r
     \r