3 from django.utils.translation import ugettext as _
5 question_view = django.dispatch.Signal(providing_args=['instance', 'user'])
11 answer_count = DenormalizedField("children", node_type="answer", deleted=None)
12 favorite_count = DenormalizedField("actions", action_type="favorite", canceled=False)
14 friendly_name = _("question")
18 return self.extra_action
22 return self.extra_count
27 return _('[closed] ') + self.title
30 return _('[deleted] ') + self.title
35 def answer_accepted(self):
36 return self.extra_ref is not None
39 def accepted_answer(self):
43 def get_absolute_url(self):
44 return ('question', (), {'id': self.id, 'slug': django_urlquote(slugify(self.title))})
46 def get_revision_url(self):
47 return reverse('question_revisions', args=[self.id])
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)
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)
58 return [Question.objects.get(id=r['id']) for r in related_list]
61 def question_viewed(instance, **kwargs):
62 instance.extra_count += 1
65 question_view.connect(question_viewed)
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())
78 class QuestionRevision(NodeRevision):