3 from django.utils.translation import ugettext as _
5 class QuestionManager(NodeManager):
6 def search(self, keywords):
7 return False, self.filter(models.Q(title__icontains=keywords) | models.Q(body__icontains=keywords))
10 class Meta(Node.Meta):
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)
17 friendly_name = _("question")
18 objects = QuestionManager()
22 return self.nis.closed
26 return self.extra_count
31 return _('[deleted] ') + self.title
34 return _('[closed] ') + self.title
39 def accepted_answers(self):
40 return self.answers.filter(~models.Q(state_string__contains="(deleted)"), marked=True)
43 def get_absolute_url(self):
44 return ('question', (), {'id': self.id, 'slug': django_urlquote(slugify(self.title))})
46 def meta_description(self):
49 def get_revision_url(self):
50 return reverse('question_revisions', args=[self.id])
52 def get_related_questions(self, count=10):
53 cache_key = '%s.related_questions:%d:%d' % (settings.APP_URL, count, self.id)
54 related_list = cache.get(cache_key)
56 if related_list is None:
57 related_list = Question.objects.filter_state(deleted=False).values('id').filter(tags__id__in=[t.id for t in self.tags.all()]
58 ).exclude(id=self.id).annotate(frequency=models.Count('id')).order_by('-frequency')[:count]
59 cache.set(cache_key, related_list, 60 * 60)
61 return [Question.objects.get(id=r['id']) for r in related_list]
63 def get_active_users(self):
66 active_users.add(self.author)
68 for answer in self.answers:
69 active_users.add(answer.author)
71 for comment in answer.comments:
72 active_users.add(comment.author)
74 for comment in self.comments:
75 active_users.add(comment.author)
80 class QuestionSubscription(models.Model):
81 user = models.ForeignKey(User)
82 question = models.ForeignKey(Node)
83 auto_subscription = models.BooleanField(default=True)
84 last_view = models.DateTimeField(default=datetime.datetime.now())
90 class QuestionRevision(NodeRevision):