From 21135f96641449059fa9868f5e6972cf33f368a1 Mon Sep 17 00:00:00 2001 From: Tomasz Zielinski Date: Thu, 1 Dec 2011 22:59:54 +0100 Subject: Tickets 104, 107: transplant of get_similar_questions method --- askbot/models/question.py | 72 ++++++++++++++++++++++------------------------- askbot/views/readers.py | 2 +- 2 files changed, 34 insertions(+), 40 deletions(-) diff --git a/askbot/models/question.py b/askbot/models/question.py index dd6635f4..ad381224 100644 --- a/askbot/models/question.py +++ b/askbot/models/question.py @@ -1,11 +1,14 @@ import logging import datetime +import operator + from django.conf import settings from django.utils.datastructures import SortedDict from django.db import models from django.contrib.auth.models import User from django.contrib.sitemaps import ping_google from django.utils.translation import ugettext as _ + import askbot import askbot.conf from askbot.models.tag import Tag @@ -483,6 +486,36 @@ class Thread(models.Model): self.save() + def get_similar_questions(self): + """ + Get 10 similar questions for given one. + Questions with the individual tags will be added to list if above questions are not full. + + This function has a limitation that it will + retrieve only 100 records then select 10 most similar + from that list as querying entire database may + be very expensive - this function will benefit from + some sort of optimization + """ + + def get_data(): + thread_question = self._question() + + tags_list = thread_question.tags.all() + similar_questions = Question.objects.filter(tags__in=tags_list).\ + exclude(id = self.id).exclude(deleted = True).distinct()[:100] + + similar_questions = list(similar_questions) + for question in similar_questions: + question.similarity = thread_question.get_similarity(other_question=question) + + similar_questions.sort(key=operator.attrgetter('similarity'), reverse=True) + return similar_questions[:10] + + return LazyList(get_data) + + + class Question(content.Content): # TODO: Eventually move this into Content/Post model thread = models.ForeignKey('Thread', unique=True, related_name='questions') @@ -539,45 +572,6 @@ class Question(content.Content): Question.objects.filter(id = self.id).update(is_anonymous = False) self.revisions.all().update(is_anonymous = False) - def get_similar_questions(self): - """ - Get 10 similar questions for given one. - Questions with the individual tags will be added to list if above questions are not full. - - This function has a limitation that it will - retrieve only 100 records then select 10 most similar - from that list as querying entire database may - be very expensive - this function will benefit from - some sort of optimization - """ - #todo: goes to thread - #print datetime.datetime.now() - - def get_data(): - - tags_list = self.tags.all() - similar_questions = self.__class__.objects.filter( - tags__in = self.tags.all() - ).exclude( - id = self.id, - ).exclude( - deleted = True - ).distinct()[:100] - similar_questions = list(similar_questions) - output = list() - for question in similar_questions: - question.similarity = self.get_similarity( - other_question = question - ) - #sort in reverse order - x and y are interchanged in cmp() call - similar_questions.sort(lambda x,y: cmp(y.similarity, x.similarity)) - if len(similar_questions) > 10: - return similar_questions[:10] - else: - return similar_questions - - return LazyList(get_data) - def get_similarity(self, other_question = None): """return number of tags in the other question that overlap with the current question (self) diff --git a/askbot/views/readers.py b/askbot/views/readers.py index 2c637f36..5b4a2bc8 100644 --- a/askbot/views/readers.py +++ b/askbot/views/readers.py @@ -569,7 +569,7 @@ def question(request, id):#refactor - long subroutine. display question body, an 'tags' : question.tags.all(), 'tab_id' : answer_sort_method, 'favorited' : favorited, - 'similar_questions' : question.get_similar_questions(), + 'similar_questions' : question.thread.get_similar_questions(), 'language_code': translation.get_language(), 'paginator_context' : paginator_context, 'show_post': show_post, -- cgit v1.2.3-1-g7c22