4 from django.utils.translation import ugettext as _
5 from django.utils.encoding import smart_unicode, force_unicode
7 from forum import modules
9 class ActiveTagManager(CachedManager):
10 use_for_related_fields = True
12 def get_query_set(self):
13 return super(ActiveTagManager, self).get_query_set().exclude(used_count__lt=1)
16 name = models.CharField(max_length=255, unique=True)
17 created_by = models.ForeignKey(User, related_name='created_tags')
18 created_at = models.DateTimeField(default=datetime.datetime.now, blank=True, null=True)
19 marked_by = models.ManyToManyField(User, related_name="marked_tags", through="MarkedTag")
21 used_count = models.PositiveIntegerField(default=0)
23 active = ActiveTagManager()
26 ordering = ('-used_count', 'name')
29 def __unicode__(self):
30 return force_unicode(self.name)
32 def add_to_usage_count(self, value):
33 if self.used_count + value < 0:
36 self.used_count = models.F('used_count') + value
39 return self._generate_cache_key(Tag.safe_cache_name(self.name))
42 def safe_cache_name(cls, name):
43 return "".join([str(ord(c)) for c in name])
46 def infer_cache_key(cls, querydict):
47 if 'name' in querydict:
48 return cls._generate_cache_key(cls.safe_cache_name(querydict['name']))
53 def value_to_list_on_cache_query(cls):
57 def get_absolute_url(self):
58 return ('tag_questions', (), {'tag': self.name})
60 class MarkedTag(models.Model):
61 TAG_MARK_REASONS = (('good', _('interesting')), ('bad', _('ignored')))
62 tag = models.ForeignKey(Tag, related_name='user_selections')
63 user = models.ForeignKey(User, related_name='tag_selections')
64 reason = models.CharField(max_length=16, choices=TAG_MARK_REASONS)