]> git.openstreetmap.org Git - osqa.git/blob - forum/models/tag.py
show the recaptcha field to unauthenticated users without a metter what is the min...
[osqa.git] / forum / models / tag.py
1 import datetime
2 from base import *
3
4 from django.conf import settings as django_settings
5 from django.core.cache.backends.base import BaseCache
6 from django.utils.translation import ugettext as _
7 from django.utils.encoding import smart_unicode, force_unicode
8
9 from forum import modules
10
11 class ActiveTagManager(CachedManager):
12     use_for_related_fields = True
13
14     def get_query_set(self):
15         return super(ActiveTagManager, self).get_query_set().exclude(used_count__lt=1)
16
17 class Tag(BaseModel):
18     name            = models.CharField(max_length=255, unique=True)
19     created_by      = models.ForeignKey(User, related_name='created_tags')
20     created_at      = models.DateTimeField(default=datetime.datetime.now, blank=True, null=True)
21     marked_by       = models.ManyToManyField(User, related_name="marked_tags", through="MarkedTag")
22     # Denormalised data
23     used_count = models.PositiveIntegerField(default=0)
24
25     active = ActiveTagManager()
26
27     class Meta:
28         ordering = ('-used_count', 'name')
29         app_label = 'forum'
30
31     def __unicode__(self):
32         return force_unicode(self.name)
33
34     def add_to_usage_count(self, value):
35         if self.used_count + value < 0:
36             self.used_count = 0
37         else:
38             self.used_count = models.F('used_count') + value
39
40     def cache_key(self):
41         return self._generate_cache_key(Tag.safe_cache_name(self.name))
42
43     @classmethod
44     def safe_cache_name(cls, name):
45         return "".join([str(ord(c)) for c in name])
46
47     @classmethod
48     def infer_cache_key(cls, querydict):
49         if 'name' in querydict:
50             cache_key = cls._generate_cache_key(cls.safe_cache_name(querydict['name']))
51
52             if len(cache_key) > django_settings.CACHE_MAX_KEY_LENGTH:
53                 cache_key = cache_key[:django_settings.CACHE_MAX_KEY_LENGTH]
54
55             return cache_key
56
57         return None
58
59     @classmethod
60     def value_to_list_on_cache_query(cls):
61         return 'name'
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