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