4 from django.utils.translation import ugettext as _
6 from forum import modules
8 class ActiveTagManager(models.Manager):
9 use_for_related_fields = True
11 def get_query_set(self):
12 return super(ActiveTagManager, self).get_query_set().exclude(used_count__lt=1)
15 name = models.CharField(max_length=255, unique=True)
16 created_by = models.ForeignKey(User, related_name='created_tags')
17 created_at = models.DateTimeField(default=datetime.datetime.now, blank=True, null=True)
18 marked_by = models.ManyToManyField(User, related_name="marked_tags", through="MarkedTag")
20 used_count = models.PositiveIntegerField(default=0)
22 active = ActiveTagManager()
25 ordering = ('-used_count', 'name')
28 def __unicode__(self):
29 return u'%s' % self.name
31 def add_to_usage_count(self, value):
32 if self.used_count + value < 0:
35 self.used_count = models.F('used_count') + value
38 def get_absolute_url(self):
39 return ('tag_questions', (), {'tag': self.name})
41 class MarkedTag(models.Model):
42 TAG_MARK_REASONS = (('good', _('interesting')), ('bad', _('ignored')))
43 tag = models.ForeignKey(Tag, related_name='user_selections')
44 user = models.ForeignKey(User, related_name='tag_selections')
45 reason = models.CharField(max_length=16, choices=TAG_MARK_REASONS)