]> git.openstreetmap.org Git - osqa.git/commitdiff
Delted questions were not updating tag count.
authorhernani <hernani@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Thu, 20 May 2010 15:57:03 +0000 (15:57 +0000)
committerhernani <hernani@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Thu, 20 May 2010 15:57:03 +0000 (15:57 +0000)
git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@309 0cfe37f9-358a-4d5e-be75-b63607b5c754

forum/actions/meta.py
forum/models/node.py

index b16f4f994124cb930051d0b005a1cc5ded8cc9e4..1862cb3113c04c2910e7bb0992958153ea5c8459 100644 (file)
@@ -171,15 +171,13 @@ class FavoriteAction(ActionProxy):
 \r
 class DeleteAction(ActionProxy):\r
     def process_action(self):\r
 \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
         \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
         if self.node.node_type == "answer":\r
             self.node.question.reset_answer_count_cache()\r
index 4f6b6d105e921fb7ce66e0e04f23aa9d2aeda976..6f4e12b0429823bb4abad4d6243e18356e882c33 100644 (file)
@@ -203,45 +203,70 @@ class Node(BaseModel, NodeContent):
 \r
         self.save()\r
 \r
 \r
         self.save()\r
 \r
-    def get_tag_list_if_changed(self):\r
+    def _list_changes_in_tags(self):\r
         dirty = self.get_dirty_fields()\r
         dirty = self.get_dirty_fields()\r
-        active_user = self.last_edited and self.last_edited.by or self.author\r
 \r
 \r
-        if 'tagnames' in dirty:\r
-            new_tags = self.tagname_list()\r
-            old_tags = dirty['tagnames']\r
-\r
-            if old_tags is None or not old_tags:\r
-                old_tags = []\r
+        if not 'tagnames' in dirty:\r
+            return None\r
+        else:\r
+            if dirty['tagnames']:\r
+                old_tags = set(name for name in dirty['tagnames'].split(u' '))\r
             else:\r
             else:\r
-                old_tags = [name for name in dirty['tagnames'].split(u' ')]\r
+                old_tags = set()\r
+            new_tags = set(name for name in self.tagnames.split(u' ') if name)\r
+\r
+            return dict(\r
+                current=list(new_tags),\r
+                added=list(new_tags - old_tags),\r
+                removed=list(old_tags - new_tags)\r
+            )\r
+\r
+    def _last_active_user(self):\r
+        return self.last_edited and self.last_edited.by or self.author\r
 \r
 \r
-            tag_list = []\r
+    def _process_changes_in_tags(self):\r
+        tag_changes = self._list_changes_in_tags()\r
 \r
 \r
-            for name in new_tags:\r
+        if tag_changes is not None:\r
+            for name in tag_changes['added']:\r
                 try:\r
                     tag = Tag.objects.get(name=name)\r
                 except:\r
                 try:\r
                     tag = Tag.objects.get(name=name)\r
                 except:\r
-                    tag = Tag.objects.create(name=name, created_by=active_user or self.author)\r
+                    tag = Tag.objects.create(name=name, created_by=self._last_active_user())\r
 \r
 \r
-                tag_list.append(tag)\r
-\r
-                if not name in old_tags:\r
-                    tag.used_count = tag.used_count + 1\r
-                    if tag.deleted:\r
-                        tag.unmark_deleted()\r
+                if not self.deleted:\r
+                    tag.used_count = models.F('used_count') + 1\r
                     tag.save()\r
 \r
                     tag.save()\r
 \r
-            for name in [n for n in old_tags if not n in new_tags]:\r
-                tag = Tag.objects.get(name=name)\r
-                tag.used_count = tag.used_count - 1\r
-                if tag.used_count == 0:\r
-                    tag.mark_deleted(active_user)\r
-                tag.save()\r
+            if not self.deleted:\r
+                for name in tag_changes['removed']:\r
+                    try:\r
+                        tag = Tag.objects.get(name=name)\r
+                        tag.used_count = models.F('used_count') - 1\r
+                        tag.save()\r
+                        if tag.used_count == 0:\r
+                            tag.mark_deleted(self._last_active_user())\r
+                    except:\r
+                        pass\r
+\r
+            return True\r
 \r
 \r
-            return tag_list\r
+        return False\r
 \r
 \r
-        return None\r
+    def mark_deleted(self, action):\r
+        self.deleted = action\r
+        self.save()\r
+\r
+        if action:\r
+            for tag in self.tags.all():\r
+                tag.used_count = models.F('used_count') - 1\r
+                tag.save()\r
+                if tag.used_count == 0:\r
+                    tag.mark_deleted(self._last_active_user())\r
+        else:\r
+            for tag in Tag.objects.filter(name__in=self.tagname_list()):\r
+                tag.used_count = models.F('used_count') + 1\r
+                tag.save()\r
 \r
     def save(self, *args, **kwargs):\r
         if not self.id:\r
 \r
     def save(self, *args, **kwargs):\r
         if not self.id:\r
@@ -252,10 +277,11 @@ class Node(BaseModel, NodeContent):
 \r
         if self.parent_id and not self.abs_parent_id:\r
             self.abs_parent = self.parent.absolute_parent\r
 \r
         if self.parent_id and not self.abs_parent_id:\r
             self.abs_parent = self.parent.absolute_parent\r
-        \r
-        tags = self.get_tag_list_if_changed()\r
+\r
+        tags_changed = self._process_changes_in_tags()\r
+\r
         super(Node, self).save(*args, **kwargs)\r
         super(Node, self).save(*args, **kwargs)\r
-        if tags is not None: self.tags = tags\r
+        if tags_changed: self.tags = list(Tag.objects.filter(name__in=self.tagname_list()))\r
 \r
     class Meta:\r
         app_label = 'forum'\r
 \r
     class Meta:\r
         app_label = 'forum'\r