]> git.openstreetmap.org Git - osqa.git/blob - forum/models/tag.py
Don't let reputation go 0, it may cause mysql to overflow.
[osqa.git] / forum / models / tag.py
1 import datetime
2 from base import *
3
4 from django.utils.translation import ugettext as _
5
6 from forum import modules
7
8 class ActiveTagManager(CachedManager):
9     use_for_related_fields = True
10
11     def get_query_set(self):
12         return super(ActiveTagManager, self).get_query_set().exclude(used_count__lt=1)
13
14 class Tag(BaseModel):
15     name            = models.CharField(max_length=255, unique=True)
16     created_by      = models.ForeignKey(User, related_name='created_tags')
17     created_at      = models.DateTimeField(default=datetime.datetime.now, blank=True, null=True)
18     marked_by       = models.ManyToManyField(User, related_name="marked_tags", through="MarkedTag")
19     # Denormalised data
20     used_count = models.PositiveIntegerField(default=0)
21
22     active = ActiveTagManager()
23
24     class Meta:
25         ordering = ('-used_count', 'name')
26         app_label = 'forum'
27
28     def __unicode__(self):
29         return self.name
30
31     def add_to_usage_count(self, value):
32         if self.used_count + value < 0:
33             self.used_count = 0
34         else:
35             self.used_count = models.F('used_count') + value
36
37     def cache_key(self):
38         return self._generate_cache_key(Tag.safe_cache_name(self.name))
39
40     @classmethod
41     def safe_cache_name(cls, name):
42         return "".join([str(ord(c)) for c in name])
43
44     @classmethod
45     def infer_cache_key(cls, querydict):
46         if 'name' in querydict:
47             return cls._generate_cache_key(cls.safe_cache_name(querydict['name']))
48
49         return None
50
51     @classmethod
52     def value_to_list_on_cache_query(cls):
53         return 'name'
54
55     @models.permalink
56     def get_absolute_url(self):
57         return ('tag_questions', (), {'tag': self.name})
58
59 class MarkedTag(models.Model):
60     TAG_MARK_REASONS = (('good', _('interesting')), ('bad', _('ignored')))
61     tag = models.ForeignKey(Tag, related_name='user_selections')
62     user = models.ForeignKey(User, related_name='tag_selections')
63     reason = models.CharField(max_length=16, choices=TAG_MARK_REASONS)
64
65     class Meta:
66         app_label = 'forum'
67