]> git.openstreetmap.org Git - osqa.git/blob - forum/models/question.py
Simplify registry logic
[osqa.git] / forum / models / question.py
1 from base import *
2 from tag import Tag
3 from django.utils.translation import ugettext as _
4
5 class QuestionManager(NodeManager):
6     def search(self, keywords, **kwargs):
7         return False, self.filter(models.Q(title__icontains=keywords) | models.Q(body__icontains=keywords))
8
9 class Question(Node):
10     class Meta(Node.Meta):
11         proxy = True
12
13     answer_count = DenormalizedField("children", ~models.Q(state_string__contains="(deleted)"), node_type="answer")
14     accepted_count = DenormalizedField("children", ~models.Q(state_string__contains="(deleted)"), node_type="answer", marked=True)
15     favorite_count = DenormalizedField("actions", action_type="favorite", canceled=False)
16
17     friendly_name = _("question")
18     objects = QuestionManager()
19
20     @property
21     def closed(self):
22         return self.nis.closed
23
24     @property    
25     def view_count(self):
26         return self.extra_count
27
28     @property
29     def headline(self):
30         return self._headline()
31
32     def _headline(self):
33         if self.nis.deleted:
34             return _('[deleted] ') + self.title
35
36         if self.nis.closed:
37             return _('[closed] ') + self.title
38
39         return self.title
40
41     @property
42     def accepted_answers(self):
43         return self.answers.filter(~models.Q(state_string__contains="(deleted)"), marked=True)
44
45     @models.permalink    
46     def get_absolute_url(self):
47         return ('question', (), {'id': self.id, 'slug': django_urlquote(slugify(self.title))})
48         
49     def meta_description(self):
50         return self.summary
51
52     def get_revision_url(self):
53         return reverse('question_revisions', args=[self.id])
54
55     def get_related_questions(self, count=10):
56         cache_key = '%s.related_questions:%d:%d' % (settings.APP_URL, count, self.id)
57         related_list = cache.get(cache_key)
58
59         if related_list is None:
60             related_list = Question.objects.filter_state(deleted=False).values('id').filter(tags__id__in=[t.id for t in self.tags.all()]
61             ).exclude(id=self.id).annotate(frequency=models.Count('id')).order_by('-frequency')[:count]
62             cache.set(cache_key, related_list, 60 * 60)
63
64         return [Question.objects.get(id=r['id']) for r in related_list]
65     
66
67
68
69 class QuestionSubscription(models.Model):
70     user = models.ForeignKey(User)
71     question = models.ForeignKey(Node)
72     auto_subscription = models.BooleanField(default=True)
73     last_view = models.DateTimeField(default=datetime.datetime.now())
74
75     class Meta:
76         app_label = 'forum'
77
78
79 class QuestionRevision(NodeRevision):
80     class Meta:
81         proxy = True
82