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