3 from django.utils.translation import ugettext as _
5 question_view = django.dispatch.Signal(providing_args=['instance', 'user'])
8 answer_accepted = models.BooleanField(default=False)
9 closed = models.BooleanField(default=False)
10 closed_by = models.ForeignKey(User, null=True, blank=True, related_name='closed_questions')
11 closed_at = models.DateTimeField(null=True, blank=True)
12 close_reason = models.SmallIntegerField(choices=CLOSE_REASONS, null=True, blank=True)
13 subscribers = models.ManyToManyField(User, related_name='subscriptions', through='QuestionSubscription')
16 answer_count = models.PositiveIntegerField(default=0)
17 view_count = models.IntegerField(default=0)
18 favourite_count = models.IntegerField(default=0)
19 last_activity_at = models.DateTimeField(default=datetime.datetime.now)
20 last_activity_by = models.ForeignKey(User, related_name='last_active_in_questions')
22 favorited_by = models.ManyToManyField(User, through='FavoriteQuestion', related_name='favorite_questions')
24 class Meta(QandA.Meta):
25 db_table = u'question'
30 return _('[closed] ') + self.title
33 return _('[deleted] ') + self.title
38 super(Question, self).delete()
42 logging.debug('problem pinging google did you register you sitemap with google?')
44 def update_last_activity(self, user):
45 self.last_activity_by = user
46 self.last_activity_at = datetime.datetime.now()
49 def activate_revision(self, user, revision):
50 super(Question, self).activate_revision(user, revision)
51 self.update_last_activity(user)
54 def get_absolute_url(self):
55 return ('question', (), {'id': self.id, 'slug': django_urlquote(slugify(self.title))})
57 def get_answer_count_by_user(self, user_id):
58 from answer import Answer
59 query_set = Answer.objects.filter(author__id=user_id)
60 return query_set.filter(question=self).count()
62 def get_question_title(self):
64 attr = CONST['closed']
66 attr = CONST['deleted']
70 return u'%s %s' % (self.title, attr)
74 def get_revision_url(self):
75 return reverse('question_revisions', args=[self.id])
77 def get_latest_revision(self):
78 return self.revisions.all()[0]
80 def get_related_questions(self, count=10):
81 cache_key = '%s.related_questions:%d:%d' % (settings.APP_URL, count, self.id)
82 related_list = cache.get(cache_key)
84 if related_list is None:
85 related_list = Question.objects.values('id').filter(tags__id__in=[t.id for t in self.tags.all()]
86 ).exclude(id=self.id).exclude(deleted=True).annotate(frequency=models.Count('id')).order_by('-frequency')[:count]
87 cache.set(cache_key, related_list, 60 * 60)
89 return [Question.objects.get(id=r['id']) for r in related_list]
91 def __unicode__(self):
94 def question_viewed(instance, **kwargs):
95 instance.view_count += 1
98 question_view.connect(question_viewed)
100 class FavoriteQuestion(models.Model):
101 question = models.ForeignKey('Question')
102 user = models.ForeignKey(User, related_name='user_favorite_questions')
103 added_at = models.DateTimeField(default=datetime.datetime.now)
106 unique_together = ('question', 'user')
108 db_table = u'favorite_question'
110 def __unicode__(self):
111 return '[%s] favorited at %s' %(self.user, self.added_at)
113 def _update_question_fav_count(self, diff):
114 self.question.favourite_count = self.question.favourite_count + diff
117 def save(self, *args, **kwargs):
118 super(FavoriteQuestion, self).save(*args, **kwargs)
120 self._update_question_fav_count(1)
123 self._update_question_fav_count(-1)
124 super(FavoriteQuestion, self).delete()
126 class QuestionSubscription(models.Model):
127 user = models.ForeignKey(User)
128 question = models.ForeignKey(Question)
129 auto_subscription = models.BooleanField(default=True)
130 last_view = models.DateTimeField(default=datetime.datetime.now())
136 class QuestionRevision(NodeRevision):