From: hernani Date: Wed, 9 Jun 2010 10:39:49 +0000 (+0000) Subject: Prevent tag usage count to go bellow 0. Fixes OSQA 334, Problems when editing a question. X-Git-Tag: live~736 X-Git-Url: https://git.openstreetmap.org./osqa.git/commitdiff_plain/3c3864586d0d186ca2408c1016052db1d3c4dc55 Prevent tag usage count to go bellow 0. Fixes OSQA 334, Problems when editing a question. git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@396 0cfe37f9-358a-4d5e-be75-b63607b5c754 --- diff --git a/forum/models/node.py b/forum/models/node.py index b0f5361..bebd07c 100644 --- a/forum/models/node.py +++ b/forum/models/node.py @@ -62,7 +62,7 @@ class NodeMetaClass(BaseMetaClass): @classmethod def setup_relations(cls): for node_cls in NodeMetaClass.types.values(): - NodeMetaClass.setup_relation(node_cls) + NodeMetaClass.setup_relation(node_cls) @classmethod def setup_relation(cls, node_cls): @@ -171,7 +171,6 @@ class NodeStateQuery(object): return "(%s)" % name in node.state_string - class Node(BaseModel, NodeContent): __metaclass__ = NodeMetaClass @@ -250,7 +249,7 @@ class Node(BaseModel, NodeContent): def deleted(self): return self.nis.deleted - @property + @property def absolute_parent(self): if not self.abs_parent_id: return self @@ -309,10 +308,10 @@ class Node(BaseModel, NodeContent): new_tags = set(name for name in self.tagnames.split(u' ') if name) return dict( - current=list(new_tags), - added=list(new_tags - old_tags), - removed=list(old_tags - new_tags) - ) + current=list(new_tags), + added=list(new_tags - old_tags), + removed=list(old_tags - new_tags) + ) def _last_active_user(self): return self.last_edited and self.last_edited.by or self.author @@ -328,14 +327,14 @@ class Node(BaseModel, NodeContent): tag = Tag.objects.create(name=name, created_by=self._last_active_user()) if not self.nis.deleted: - tag.used_count = models.F('used_count') + 1 + tag.add_to_usage_count(1) tag.save() if not self.nis.deleted: for name in tag_changes['removed']: try: tag = Tag.objects.get(name=name) - tag.used_count = models.F('used_count') - 1 + tag.add_to_usage_count(-1) tag.save() except: pass @@ -350,20 +349,21 @@ class Node(BaseModel, NodeContent): if action: for tag in self.tags.all(): - tag.used_count = models.F('used_count') - 1 + tag.tag.add_to_usage_count(-1) tag.save() else: for tag in Tag.objects.filter(name__in=self.tagname_list()): - tag.used_count = models.F('used_count') + 1 + tag.add_to_usage_count(1) tag.save() def save(self, *args, **kwargs): tags_changed = self._process_changes_in_tags() - + if not self.id: self.node_type = self.get_type() super(BaseModel, self).save(*args, **kwargs) - self.active_revision = self._create_revision(self.author, 1, title=self.title, tagnames=self.tagnames, body=self.body) + self.active_revision = self._create_revision(self.author, 1, title=self.title, tagnames=self.tagnames, + body=self.body) self.update_last_activity(self.author) if self.parent_id and not self.abs_parent_id: diff --git a/forum/models/tag.py b/forum/models/tag.py index 3d3b8e8..0bff004 100644 --- a/forum/models/tag.py +++ b/forum/models/tag.py @@ -17,7 +17,6 @@ class Tag(BaseModel): active = ActiveTagManager() - class Meta: ordering = ('-used_count', 'name') app_label = 'forum' @@ -25,12 +24,18 @@ class Tag(BaseModel): def __unicode__(self): return self.name + def add_to_usage_count(self, value): + if self.used_count + value < 0: + self.used_count = 0 + else: + self.used_count = models.F('used_count') + value + @models.permalink def get_absolute_url(self): return ('tag_questions', (), {'tag': self.name}) class MarkedTag(models.Model): - TAG_MARK_REASONS = (('good',_('interesting')),('bad',_('ignored'))) + TAG_MARK_REASONS = (('good', _('interesting')), ('bad', _('ignored'))) tag = models.ForeignKey(Tag, related_name='user_selections') user = models.ForeignKey(User, related_name='tag_selections') reason = models.CharField(max_length=16, choices=TAG_MARK_REASONS)