\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
- 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
- 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
- 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
+ def _process_changes_in_tags(self):\r
+ tag_changes = self._list_changes_in_tags()\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
- 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
- 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
+ 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
\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