]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/actions/meta.py
Several improvements in full text search operations.
[osqa.git] / forum / actions / meta.py
index 2117fb7220e47d4cc7a508e95541196a438c52d3..a964ef1db289ca0ec9171441046434c2d8f75c98 100644 (file)
@@ -2,7 +2,7 @@ from django.utils.translation import ugettext as _
 from django.db.models import F\r
 from forum.models.action import ActionProxy, DummyActionProxy\r
 from forum.models import Vote, Flag\r
-import settings\r
+from forum import settings\r
 \r
 class VoteAction(ActionProxy):\r
     def update_node_score(self, inc):\r
@@ -15,7 +15,7 @@ class VoteAction(ActionProxy):
         vote.save()\r
 \r
     def cancel_action(self):\r
-        vote = self.vote.all()[0]\r
+        vote = self.vote\r
         self.update_node_score(-vote.value)\r
         vote.delete()\r
 \r
@@ -27,6 +27,14 @@ class VoteAction(ActionProxy):
         except:\r
             return None\r
 \r
+    @classmethod\r
+    def get_action_for(cls, user, node):\r
+        try:\r
+            vote = Vote.objects.get(user=user, node=node)\r
+            return vote.action\r
+        except:\r
+            return None\r
+\r
     def describe_vote(self, vote_desc, viewer=None):\r
         return _("%(user)s %(vote_desc)s %(post_desc)s") % {\r
             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),\r
@@ -89,16 +97,16 @@ class FlagAction(ActionProxy):
         flag.save()\r
         self.node.reset_flag_count_cache()\r
 \r
-        if self.node.flag_count == int(settings.FLAG_COUNT_TO_HIDE_POST):\r
+        if self.node.flag_count >= int(settings.FLAG_COUNT_TO_HIDE_POST):\r
             self.repute(self.node.author, -int(settings.REP_LOST_BY_FLAGGED_3_TIMES))\r
 \r
-        if self.node.flag_count == int(settings.FLAG_COUNT_TO_DELETE_POST):\r
+        if self.node.flag_count >= int(settings.FLAG_COUNT_TO_DELETE_POST):\r
             self.repute(self.node.author, -int(settings.REP_LOST_BY_FLAGGED_5_TIMES))\r
-            if not self.node.deleted:\r
+            if not self.node.nis.deleted:\r
                 DeleteAction(node=self.node, user=self.user, extra="BYFLAGGED").save()\r
 \r
     def cancel_action(self):\r
-        self.flag.all()[0].delete()\r
+        self.flag.delete()\r
         self.node.reset_flag_count_cache()\r
 \r
     @classmethod\r
@@ -125,18 +133,16 @@ class AcceptAnswerAction(ActionProxy):
             self.repute(self.node.author, int(settings.REP_GAIN_BY_ACCEPTED))\r
 \r
     def process_action(self):\r
-        self.node.parent.extra_ref = self.node\r
-        self.node.parent.save()\r
         self.node.marked = True\r
-        self.node.extra_action = self\r
+        self.node.nstate.accepted = self\r
         self.node.save()\r
+        self.node.question.reset_accepted_count_cache()\r
 \r
     def cancel_action(self):\r
-        self.node.parent.extra_ref = None\r
-        self.node.parent.save()\r
         self.node.marked = False\r
-        self.node.extra_action = None\r
+        self.node.nstate.accepted = None\r
         self.node.save()\r
+        self.node.question.reset_accepted_count_cache()\r
 \r
     def describe(self, viewer=None):\r
         answer = self.node\r
@@ -149,7 +155,7 @@ class AcceptAnswerAction(ActionProxy):
 \r
         return _("%(user)s accepted %(answerer)s answer on %(asker)s question %(question)s") % {\r
             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),\r
-            'answerer': self.hyperlink(answer.author.get_profile_url(), self.friendly_username(viewer, answer.author)),\r
+            'answerer': self.hyperlink(answer.author.get_profile_url(), self.friendly_ownername(viewer, answer.author)),\r
             'asker': asker,\r
             'question': self.hyperlink(question.get_absolute_url(), question.title)\r
         }\r
@@ -171,23 +177,21 @@ class FavoriteAction(ActionProxy):
 \r
 class DeleteAction(ActionProxy):\r
     def process_action(self):\r
-        self.node.deleted = self\r
-        self.node.save()\r
+        self.node.mark_deleted(self)\r
         \r
         if self.node.node_type == "answer":\r
             self.node.question.reset_answer_count_cache()\r
 \r
     def cancel_action(self):\r
-        self.node.deleted = None\r
-        self.node.save()\r
+        self.node.mark_deleted(None)\r
 \r
         if self.node.node_type == "answer":\r
             self.node.question.reset_answer_count_cache()\r
 \r
     def describe(self, viewer=None):\r
-        return _("%(user)s deleted %(post_desc)s: %(reason)s") % {\r
+        return _("%(user)s deleted %(post_desc)s") % {\r
             'user': self.hyperlink(self.user.get_profile_url(), self.friendly_username(viewer, self.user)),\r
-            'post_desc': self.describe_node(viewer, self.node), 'reason': self.reason(),\r
+            'post_desc': self.describe_node(viewer, self.node)\r
         }\r
 \r
     def reason(self):\r
@@ -196,11 +200,15 @@ class DeleteAction(ActionProxy):
         else:\r
             return _("flagged by multiple users: ") + "; ".join([f.extra for f in FlagAction.objects.filter(node=self.node)])\r
 \r
+class UnknownAction(ActionProxy):\r
+    pass\r
+\r
 \r
 class QuestionViewAction(DummyActionProxy):\r
-    def __init__(self, node, user):\r
+    def __init__(self, node, user, ip=None):\r
         self.viewuser = user\r
         self.node = node\r
+        super(QuestionViewAction, self).__init__(ip)\r
 \r
     def process_action(self):\r
         self.node.extra_count = F('extra_count') + 1\r