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