diff options
author | sailingcai <sailingcai@gmail.com> | 2009-10-14 00:27:59 +0800 |
---|---|---|
committer | sailingcai <sailingcai@gmail.com> | 2009-10-14 00:27:59 +0800 |
commit | e02cd69de6b6c534792ba12b1ac10e01e04318c2 (patch) | |
tree | 6e54c5667b0c0b3b778239e32fd2aee62f9bb73c /forum | |
parent | 0a6e82562c5c910cef4711e30a04ef866b805e32 (diff) | |
download | askbot-e02cd69de6b6c534792ba12b1ac10e01e04318c2.tar.gz askbot-e02cd69de6b6c534792ba12b1ac10e01e04318c2.tar.bz2 askbot-e02cd69de6b6c534792ba12b1ac10e01e04318c2.zip |
1. Optimize get votes in answers
Diffstat (limited to 'forum')
-rw-r--r-- | forum/models.py | 12 | ||||
-rw-r--r-- | forum/views.py | 9 |
2 files changed, 15 insertions, 6 deletions
diff --git a/forum/models.py b/forum/models.py index a6326fa9..b6722ae7 100644 --- a/forum/models.py +++ b/forum/models.py @@ -209,7 +209,17 @@ class Question(models.Model): def get_latest_revision(self): return self.revisions.all()[0] - + + def get_user_votes_in_answers(self, user): + content_type = ContentType.objects.get_for_model(Answer) + query_set = Vote.objects.extra( + tables = ['question', 'answer'], + where = ['question.id = answer.question_id AND question.id = %s AND vote.object_id = answer.id AND vote.content_type_id = %s AND vote.user_id = %s'], + params = [self.id, content_type.id, user.id] + ) + + return query_set + def get_update_summary(self,last_reported_at=None,recipient_email=''): edited = False if self.last_edited_at and self.last_edited_at > last_reported_at: diff --git a/forum/views.py b/forum/views.py index 5e53644f..cfadaa48 100644 --- a/forum/views.py +++ b/forum/views.py @@ -338,14 +338,13 @@ def question(request, id): question_vote = question_vote[0] user_answer_votes = {} - for answer in answers: - vote = answer.get_user_vote(request.user) - if vote is not None and not user_answer_votes.has_key(answer.id): + for vote in question.get_user_votes_in_answers(request.user): + if not user_answer_votes.has_key(vote.object_id): vote_value = -1 if vote.is_upvote(): vote_value = 1 - user_answer_votes[answer.id] = vote_value - + user_answer_votes[vote.object_id] = vote_value + filtered_answers = [] for answer in answers: if answer.deleted == True: |