]> git.openstreetmap.org Git - osqa.git/blob - forum/models/tag.py
Several fixes in the OSQA exporter.
[osqa.git] / forum / models / tag.py
1 from base import *
2
3 from django.utils.translation import ugettext as _
4 import django.dispatch
5
6 class ActiveTagManager(models.Manager):
7     def get_query_set(self):
8         return super(ActiveTagManager, self).get_query_set().exclude(used_count__lt=1)
9
10
11 class Tag(BaseModel):
12     name            = models.CharField(max_length=255, unique=True)
13     created_by      = models.ForeignKey(User, related_name='created_tags')
14     marked_by       = models.ManyToManyField(User, related_name="marked_tags", through="MarkedTag")
15     # Denormalised data
16     used_count = models.PositiveIntegerField(default=0)
17
18     active = ActiveTagManager()
19
20     class Meta:
21         ordering = ('-used_count', 'name')
22         app_label = 'forum'
23
24     def __unicode__(self):
25         return self.name
26
27     def add_to_usage_count(self, value):
28         if self.used_count + value < 0:
29             self.used_count = 0
30         else:
31             self.used_count = models.F('used_count') + value
32
33     @models.permalink
34     def get_absolute_url(self):
35         return ('tag_questions', (), {'tag': self.name})
36
37 class MarkedTag(models.Model):
38     TAG_MARK_REASONS = (('good', _('interesting')), ('bad', _('ignored')))
39     tag = models.ForeignKey(Tag, related_name='user_selections')
40     user = models.ForeignKey(User, related_name='tag_selections')
41     reason = models.CharField(max_length=16, choices=TAG_MARK_REASONS)
42
43     class Meta:
44         app_label = 'forum'
45