]> git.openstreetmap.org Git - osqa.git/blob - forum/models/tag.py
OSQA-436, now we support hyphens in the usernames too. Changing the username regex...
[osqa.git] / forum / models / tag.py
1 import datetime
2 from base import *
3
4 from django.utils.translation import ugettext as _
5 from django.utils.encoding import smart_unicode, force_unicode
6
7 from forum import modules
8
9 class ActiveTagManager(CachedManager):
10     use_for_related_fields = True
11
12     def get_query_set(self):
13         return super(ActiveTagManager, self).get_query_set().exclude(used_count__lt=1)
14
15 class Tag(BaseModel):
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")
20     # Denormalised data
21     used_count = models.PositiveIntegerField(default=0)
22
23     active = ActiveTagManager()
24
25     class Meta:
26         ordering = ('-used_count', 'name')
27         app_label = 'forum'
28
29     def __unicode__(self):
30         return force_unicode(self.name)
31
32     def add_to_usage_count(self, value):
33         if self.used_count + value < 0:
34             self.used_count = 0
35         else:
36             self.used_count = models.F('used_count') + value
37
38     def cache_key(self):
39         return self._generate_cache_key(Tag.safe_cache_name(self.name))
40
41     @classmethod
42     def safe_cache_name(cls, name):
43         return "".join([str(ord(c)) for c in name])
44
45     @classmethod
46     def infer_cache_key(cls, querydict):
47         if 'name' in querydict:
48             return cls._generate_cache_key(cls.safe_cache_name(querydict['name']))
49
50         return None
51
52     @classmethod
53     def value_to_list_on_cache_query(cls):
54         return 'name'
55
56     @models.permalink
57     def get_absolute_url(self):
58         return ('tag_questions', (), {'tag': self.name})
59
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)
65
66     class Meta:
67         app_label = 'forum'
68