4 from django.utils.translation import ugettext as _
6 class ActiveTagManager(models.Manager):
7 def get_query_set(self):
8 qs = super(ActiveTagManager, self).get_query_set().exclude(used_count__lt=1)
10 CurrentUserHolder = None
12 # We try to import the moderation module and if the import is successful we make the filtration
14 moderation_import = 'from %s.moderation.startup import CurrentUserHolder' % MODULES_PACKAGE
15 exec moderation_import
17 moderation_enabled= True
19 moderation_enabled = False
21 # If the moderation module has been enabled we make the filtration
22 if CurrentUserHolder is not None and moderation_enabled:
23 user = CurrentUserHolder.user
26 filter_content = not user.is_staff and not user.is_superuser
31 moderation_import = 'from %s.moderation.hooks import get_tag_ids' % MODULES_PACKAGE
32 exec moderation_import
33 qs = qs.exclude(id__in=get_tag_ids('deleted')).exclude(id__in=get_tag_ids('rejected')).exclude(
34 id__in=get_tag_ids('in_moderation')
40 name = models.CharField(max_length=255, unique=True)
41 created_by = models.ForeignKey(User, related_name='created_tags')
42 created_at = models.DateTimeField(default=datetime.datetime.now, blank=True, null=True)
43 marked_by = models.ManyToManyField(User, related_name="marked_tags", through="MarkedTag")
45 used_count = models.PositiveIntegerField(default=0)
47 active = ActiveTagManager()
48 objects = ActiveTagManager()
51 ordering = ('-used_count', 'name')
54 def __unicode__(self):
55 return u'%s' % self.name
57 def add_to_usage_count(self, value):
58 if self.used_count + value < 0:
61 self.used_count = models.F('used_count') + value
64 def get_absolute_url(self):
65 return ('tag_questions', (), {'tag': self.name})
67 class MarkedTag(models.Model):
68 TAG_MARK_REASONS = (('good', _('interesting')), ('bad', _('ignored')))
69 tag = models.ForeignKey(Tag, related_name='user_selections')
70 user = models.ForeignKey(User, related_name='tag_selections')
71 reason = models.CharField(max_length=16, choices=TAG_MARK_REASONS)