4 from django.conf import settings as django_settings
5 from django.core.cache.backends.base import BaseCache
6 from django.utils.translation import ugettext as _
7 from django.utils.encoding import smart_unicode, force_unicode
9 from forum import modules
11 class ActiveTagManager(CachedManager):
12 use_for_related_fields = True
14 def get_query_set(self):
15 return super(ActiveTagManager, self).get_query_set().exclude(used_count__lt=1)
18 name = models.CharField(max_length=255, unique=True)
19 created_by = models.ForeignKey(User, related_name='created_tags')
20 created_at = models.DateTimeField(default=datetime.datetime.now, blank=True, null=True)
21 marked_by = models.ManyToManyField(User, related_name="marked_tags", through="MarkedTag")
23 used_count = models.PositiveIntegerField(default=0)
25 active = ActiveTagManager()
28 ordering = ('-used_count', 'name')
31 def __unicode__(self):
32 return force_unicode(self.name)
34 def add_to_usage_count(self, value):
35 if self.used_count + value < 0:
38 self.used_count = models.F('used_count') + value
41 return self._generate_cache_key(Tag.safe_cache_name(self.name))
44 def safe_cache_name(cls, name):
45 return "".join([str(ord(c)) for c in name])
48 def infer_cache_key(cls, querydict):
49 if 'name' in querydict:
50 cache_key = cls._generate_cache_key(cls.safe_cache_name(querydict['name']))
52 if len(cache_key) > django_settings.CACHE_MAX_KEY_LENGTH:
53 cache_key = cache_key[:django_settings.CACHE_MAX_KEY_LENGTH]
60 def value_to_list_on_cache_query(cls):
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)