3 from django.utils.translation import ugettext as _
5 question_view = django.dispatch.Signal(providing_args=['instance', 'user'])
7 class QuestionManager(NodeManager):
8 def search(self, keywords):
9 return False, self.filter(models.Q(title__icontains=keywords) | models.Q(body__icontains=keywords))
12 class Meta(Node.Meta):
15 answer_count = DenormalizedField("children", ~models.Q(state_string__contains="(deleted)"), node_type="answer")
16 accepted_count = DenormalizedField("children", ~models.Q(state_string__contains="(deleted)"), node_type="answer", marked=True)
17 favorite_count = DenormalizedField("actions", action_type="favorite", canceled=False)
19 friendly_name = _("question")
20 objects = QuestionManager()
24 return self.nis.closed
28 return self.extra_count
33 return _('[deleted] ') + self.title
36 return _('[closed] ') + self.title
41 def accepted_answers(self):
42 return self.answers.filter(~models.Q(state_string__contains="(deleted)"), marked=True)
45 def get_absolute_url(self):
46 return ('question', (), {'id': self.id, 'slug': django_urlquote(slugify(self.title))})
48 def get_revision_url(self):
49 return reverse('question_revisions', args=[self.id])
51 def get_related_questions(self, count=10):
52 cache_key = '%s.related_questions:%d:%d' % (settings.APP_URL, count, self.id)
53 related_list = cache.get(cache_key)
55 if related_list is None:
56 related_list = Question.objects.filter_state(deleted=False).values('id').filter(tags__id__in=[t.id for t in self.tags.all()]
57 ).exclude(id=self.id).annotate(frequency=models.Count('id')).order_by('-frequency')[:count]
58 cache.set(cache_key, related_list, 60 * 60)
60 return [Question.objects.get(id=r['id']) for r in related_list]
63 def question_viewed(instance, **kwargs):
64 instance.extra_count += 1
67 question_view.connect(question_viewed)
70 class QuestionSubscription(models.Model):
71 user = models.ForeignKey(User)
72 question = models.ForeignKey(Node)
73 auto_subscription = models.BooleanField(default=True)
74 last_view = models.DateTimeField(default=datetime.datetime.now())
80 class QuestionRevision(NodeRevision):