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