diff options
author | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2012-07-31 21:32:03 -0400 |
---|---|---|
committer | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2012-07-31 21:32:03 -0400 |
commit | c9bc80fdb6316cfe377790c3a038ba017c89d246 (patch) | |
tree | 9c2928be123f1cb36da121a10c1f0cea94d0917a | |
parent | 2aac3317ab03ebe1f27796b647ce87679d202c46 (diff) | |
download | askbot-c9bc80fdb6316cfe377790c3a038ba017c89d246.tar.gz askbot-c9bc80fdb6316cfe377790c3a038ba017c89d246.tar.bz2 askbot-c9bc80fdb6316cfe377790c3a038ba017c89d246.zip |
small change in the code calculating similar questions
-rw-r--r-- | askbot/models/question.py | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/askbot/models/question.py b/askbot/models/question.py index 0fd02bf0..2bb9e80a 100644 --- a/askbot/models/question.py +++ b/askbot/models/question.py @@ -641,6 +641,8 @@ class Thread(models.Model): """ def get_data(): + # todo: code in this function would be simpler if + # we had question post id denormalized on the thread tags_list = self.get_tag_names() similar_threads = Thread.objects.filter( tags__name__in=tags_list @@ -659,20 +661,28 @@ class Thread(models.Model): similar_threads = similar_threads[:10] # Denormalize questions to speed up template rendering + # todo: just denormalize question_post_id on the thread! thread_map = dict([(thread.id, thread) for thread in similar_threads]) questions = Post.objects.get_questions() questions = questions.select_related('thread').filter(thread__in=similar_threads) for q in questions: thread_map[q.thread_id].question_denorm = q - # Postprocess data - similar_threads = [ - { - 'url': thread.question_denorm.get_absolute_url(), - 'title': thread.get_title(thread.question_denorm) - } for thread in similar_threads - ] - return similar_threads + # Postprocess data for the final output + result = list() + for thread in similar_threads: + question_post = getattr(thread, 'question_denorm', None) + # unfortunately the if statement below is necessary due to + # a possible bug + # all this proves that it's wrong to reference threads by + # the question post id in the question page urls!!! + # this is a "legacy" problem inherited from the old models + if question_post: + url = question_post.get_absolute_url() + title = thread.get_title(question_post) + result.append({'url': url, 'title': title}) + + return result def get_cached_data(): """similar thread data will expire |