]> git.openstreetmap.org Git - osqa.git/blob - forum/models/tag.py
2a3609a9636d6f4cd6fdbe2f7461620a5162ef48
[osqa.git] / forum / models / tag.py
1 import datetime
2 from base import *
3
4 from forum.modules import MODULES_PACKAGE
5
6 from django.utils.translation import ugettext as _
7 import django.dispatch
8
9 class ActiveTagManager(models.Manager):
10     def get_query_set(self):
11         qs = super(ActiveTagManager, self).get_query_set().exclude(used_count__lt=1)
12
13         CurrentUserHolder = None
14
15         moderation_import = 'from %s.moderation.startup import CurrentUserHolder' % MODULES_PACKAGE
16         exec moderation_import
17
18         if CurrentUserHolder is not None:
19             user = CurrentUserHolder.user
20
21             try:
22                 filter_content = not user.is_staff and not user.is_superuser
23             except:
24                 filter_content = True
25
26             if filter_content:
27                 moderation_import = 'from %s.moderation.hooks import get_tag_ids' % MODULES_PACKAGE
28                 exec moderation_import
29                 qs = qs.exclude(id__in=get_tag_ids('deleted')).exclude(id__in=get_tag_ids('rejected')).exclude(
30                     id__in=get_tag_ids('in_moderation')
31                 )
32
33         return qs
34
35 class Tag(BaseModel):
36     name            = models.CharField(max_length=255, unique=True)
37     created_by      = models.ForeignKey(User, related_name='created_tags')
38     created_at      = models.DateTimeField(default=datetime.datetime.now, blank=True, null=True)
39     marked_by       = models.ManyToManyField(User, related_name="marked_tags", through="MarkedTag")
40     # Denormalised data
41     used_count = models.PositiveIntegerField(default=0)
42
43     active = ActiveTagManager()
44     objects = ActiveTagManager()
45
46     class Meta:
47         ordering = ('-used_count', 'name')
48         app_label = 'forum'
49
50     def __unicode__(self):
51         return u'%s' % self.name
52
53     def add_to_usage_count(self, value):
54         if self.used_count + value < 0:
55             self.used_count = 0
56         else:
57             self.used_count = models.F('used_count') + value
58
59     @models.permalink
60     def get_absolute_url(self):
61         return ('tag_questions', (), {'tag': self.name})
62
63 class MarkedTag(models.Model):
64     TAG_MARK_REASONS = (('good', _('interesting')), ('bad', _('ignored')))
65     tag = models.ForeignKey(Tag, related_name='user_selections')
66     user = models.ForeignKey(User, related_name='tag_selections')
67     reason = models.CharField(max_length=16, choices=TAG_MARK_REASONS)
68
69     class Meta:
70         app_label = 'forum'
71