]> git.openstreetmap.org Git - osqa.git/blob - forum/models/question.py
54840166d1d9c51534de77884296f670622bb0cf
[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 question_view = django.dispatch.Signal(providing_args=['instance', 'user'])
6
7 class Question(Node):
8     class Meta(Node.Meta):
9         proxy = True
10
11     answer_count = DenormalizedField("children", node_type="answer", deleted=None)
12     favorite_count = DenormalizedField("actions", action_type="favorite", canceled=False)
13
14     friendly_name = _("question")
15
16     @property   
17     def closed(self):
18         return self.extra_action
19
20     @property    
21     def view_count(self):
22         return self.extra_count
23
24     @property
25     def headline(self):
26         if self.marked:
27             return _('[closed] ') + self.title
28
29         if self.deleted:
30             return _('[deleted] ') + self.title
31
32         return self.title
33
34     @property
35     def answer_accepted(self):
36         return self.extra_ref is not None
37
38     @property
39     def accepted_answer(self):
40         return self.extra_ref
41
42     @models.permalink    
43     def get_absolute_url(self):
44         return ('question', (), {'id': self.id, 'slug': django_urlquote(slugify(self.title))})
45
46     def get_revision_url(self):
47         return reverse('question_revisions', args=[self.id])
48
49     def get_related_questions(self, count=10):
50         cache_key = '%s.related_questions:%d:%d' % (settings.APP_URL, count, self.id)
51         related_list = cache.get(cache_key)
52
53         if related_list is None:
54             related_list = Question.objects.values('id').filter(tags__id__in=[t.id for t in self.tags.all()]
55             ).exclude(id=self.id).filter(deleted=None).annotate(frequency=models.Count('id')).order_by('-frequency')[:count]
56             cache.set(cache_key, related_list, 60 * 60)
57
58         return [Question.objects.get(id=r['id']) for r in related_list]
59
60
61 def question_viewed(instance, **kwargs):
62     instance.extra_count += 1
63     instance.save()
64
65 question_view.connect(question_viewed)
66
67
68 class QuestionSubscription(models.Model):
69     user = models.ForeignKey(User)
70     question = models.ForeignKey(Node)
71     auto_subscription = models.BooleanField(default=True)
72     last_view = models.DateTimeField(default=datetime.datetime.now())
73
74     class Meta:
75         app_label = 'forum'
76
77
78 class QuestionRevision(NodeRevision):
79     class Meta:
80         proxy = True
81