4 from forum.modules import MODULES_PACKAGE
6 from django.utils.translation import ugettext as _
9 class ActiveTagManager(models.Manager):
10 def get_query_set(self):
11 qs = super(ActiveTagManager, self).get_query_set().exclude(used_count__lt=1)
13 CurrentUserHolder = None
14 moderation_import = 'from %s.moderation.startup import CurrentUserHolder' % MODULES_PACKAGE
15 exec moderation_import
17 if CurrentUserHolder is not None:
18 user = CurrentUserHolder.user
21 filter_content = not user.is_staff and not user.is_superuser
26 moderation_import = 'from %s.moderation.hooks import get_tag_ids' % MODULES_PACKAGE
27 exec moderation_import
28 qs = qs.exclude(id__in=get_tag_ids('deleted')).exclude(id__in=get_tag_ids('rejected')).exclude(
29 id__in=get_tag_ids('in_moderation')
35 name = models.CharField(max_length=255, unique=True)
36 created_by = models.ForeignKey(User, related_name='created_tags')
37 created_at = models.DateTimeField(default=datetime.datetime.now, blank=True, null=True)
38 marked_by = models.ManyToManyField(User, related_name="marked_tags", through="MarkedTag")
40 used_count = models.PositiveIntegerField(default=0)
42 active = ActiveTagManager()
43 objects = ActiveTagManager()
46 ordering = ('-used_count', 'name')
49 def __unicode__(self):
50 return u'%s' % self.name
52 def add_to_usage_count(self, value):
53 if self.used_count + value < 0:
56 self.used_count = models.F('used_count') + value
59 def get_absolute_url(self):
60 return ('tag_questions', (), {'tag': self.name})
62 class MarkedTag(models.Model):
63 TAG_MARK_REASONS = (('good', _('interesting')), ('bad', _('ignored')))
64 tag = models.ForeignKey(Tag, related_name='user_selections')
65 user = models.ForeignKey(User, related_name='tag_selections')
66 reason = models.CharField(max_length=16, choices=TAG_MARK_REASONS)