[FIX] forum: optimize vote metadata queries
Saves 2 requests per post, aka 20 requests on a full page (89 -> 68): * don't create a new sudoed env and recordset for each post, create a single one so prefetch can work * don't do unnecessary sudoed access (.parent_id.create_uid without reading any field) * temp variables in template seems to save a few queries, not sure why There's still a linear growth of requests wrt posts in the page (single-post page has 56 queries), but it's a bit less terrible.
This commit is contained in:
committed by
Olivier Dony
parent
bc33b8dd04
commit
aa25670c69
@@ -1,6 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from datetime import datetime
|
||||
import itertools
|
||||
import logging
|
||||
import math
|
||||
import re
|
||||
@@ -9,6 +10,7 @@ from werkzeug.exceptions import Forbidden
|
||||
|
||||
from openerp import _
|
||||
from openerp import api, fields, models
|
||||
from openerp import http
|
||||
from openerp import modules
|
||||
from openerp import tools
|
||||
from openerp import SUPERUSER_ID
|
||||
@@ -289,31 +291,32 @@ class Post(models.Model):
|
||||
can_comment_convert = fields.Boolean('Can Convert to Comment', compute='_get_post_karma_rights')
|
||||
can_view = fields.Boolean('Can View', compute='_get_post_karma_rights')
|
||||
|
||||
@api.one
|
||||
@api.multi
|
||||
def _get_post_karma_rights(self):
|
||||
user = self.env.user
|
||||
post = self.sudo()
|
||||
# sudoed recordset instead of individual posts so values can be
|
||||
# prefetched in bulk
|
||||
for post, post_sudo in itertools.izip(self, self.sudo()):
|
||||
is_creator = post.create_uid == user
|
||||
|
||||
is_creator = post.create_uid.id == self._uid
|
||||
post.karma_accept = post.forum_id.karma_answer_accept_own if post.parent_id.create_uid == user else post.forum_id.karma_answer_accept_all
|
||||
post.karma_edit = post.forum_id.karma_edit_own if is_creator else post.forum_id.karma_edit_all
|
||||
post.karma_close = post.forum_id.karma_close_own if is_creator else post.forum_id.karma_close_all
|
||||
post.karma_unlink = post.forum_id.karma_unlink_own if is_creator else post.forum_id.karma_unlink_all
|
||||
post.karma_comment = post.forum_id.karma_comment_own if is_creator else post.forum_id.karma_comment_all
|
||||
post.karma_comment_convert = post.forum_id.karma_comment_convert_own if is_creator else post.forum_id.karma_comment_convert_all
|
||||
|
||||
self.karma_accept = self.forum_id.karma_answer_accept_own if post.parent_id and post.parent_id.create_uid.id == self._uid else self.forum_id.karma_answer_accept_all
|
||||
self.karma_edit = self.forum_id.karma_edit_own if is_creator else self.forum_id.karma_edit_all
|
||||
self.karma_close = self.forum_id.karma_close_own if is_creator else self.forum_id.karma_close_all
|
||||
self.karma_unlink = self.forum_id.karma_unlink_own if is_creator else self.forum_id.karma_unlink_all
|
||||
self.karma_comment = self.forum_id.karma_comment_own if is_creator else self.forum_id.karma_comment_all
|
||||
self.karma_comment_convert = self.forum_id.karma_comment_convert_own if is_creator else self.forum_id.karma_comment_convert_all
|
||||
|
||||
self.can_ask = user.karma >= self.forum_id.karma_ask
|
||||
self.can_answer = user.karma >= self.forum_id.karma_answer
|
||||
self.can_accept = user.karma >= self.karma_accept
|
||||
self.can_edit = user.karma >= self.karma_edit
|
||||
self.can_close = user.karma >= self.karma_close
|
||||
self.can_unlink = user.karma >= self.karma_unlink
|
||||
self.can_upvote = user.karma >= self.forum_id.karma_upvote
|
||||
self.can_downvote = user.karma >= self.forum_id.karma_downvote
|
||||
self.can_comment = user.karma >= self.karma_comment
|
||||
self.can_comment_convert = user.karma >= self.karma_comment_convert
|
||||
self.can_view = user.karma >= self.karma_close or post.create_uid.karma > 0
|
||||
post.can_ask = user.karma >= post.forum_id.karma_ask
|
||||
post.can_answer = user.karma >= post.forum_id.karma_answer
|
||||
post.can_accept = user.karma >= post.karma_accept
|
||||
post.can_edit = user.karma >= post.karma_edit
|
||||
post.can_close = user.karma >= post.karma_close
|
||||
post.can_unlink = user.karma >= post.karma_unlink
|
||||
post.can_upvote = user.karma >= post.forum_id.karma_upvote
|
||||
post.can_downvote = user.karma >= post.forum_id.karma_downvote
|
||||
post.can_comment = user.karma >= post.karma_comment
|
||||
post.can_comment_convert = user.karma >= post.karma_comment_convert
|
||||
post.can_view = user.karma >= post.karma_close or post_sudo.create_uid.karma > 0
|
||||
|
||||
@api.one
|
||||
@api.constrains('post_type', 'forum_id')
|
||||
|
||||
@@ -561,14 +561,20 @@
|
||||
</template>
|
||||
|
||||
<template id="vote">
|
||||
<t t-set="own_vote" t-value="post.user_vote"/>
|
||||
<t t-set="forum" t-value="post.forum_id"/>
|
||||
<t t-set="karma_up" t-value="forum.karma_upvote"/>
|
||||
<t t-set="karma_down" t-value="forum.karma_downvote"/>
|
||||
<t t-set="can_upvote" t-value="post.can_upvote"/>
|
||||
<t t-set="can_downvote" t-value="post.can_downvote"/>
|
||||
<div class="vote text-center">
|
||||
<a t-attf-class="vote_up fa fa-caret-up no-decoration #{post.user_vote == 1 and 'text-success' or ''} #{((post.user_vote == 1 and not post.can_downvote) or not post.can_upvote) and 'karma_required' or ''}"
|
||||
t-attf-data-karma="#{post.user_vote == 1 and post.forum_id.karma_downvote or post.forum_id.karma_upvote}"
|
||||
t-attf-data-href="/forum/#{slug(post.forum_id)}/post/#{slug(post)}/upvote"/>
|
||||
<a t-attf-class="vote_up fa fa-caret-up no-decoration #{own_vote == 1 and 'text-success' or ''} #{((own_vote == 1 and not can_downvote) or not can_upvote) and 'karma_required' or ''}"
|
||||
t-attf-data-karma="#{own_vote == 1 and karma_down or karma_up}"
|
||||
t-attf-data-href="/forum/#{slug(forum)}/post/#{slug(post)}/upvote"/>
|
||||
<div class="vote_count text-muted" t-esc="post.vote_count"/>
|
||||
<a t-attf-class="vote_down fa fa-caret-down no-decoration #{post.user_vote == -1 and 'text-warning' or ''} #{((post.user_vote == -1 and not post.can_upvote) or not post.can_downvote) and 'karma_required' or ''}"
|
||||
t-attf-data-karma="#{post.user_vote == -1 and post.forum_id.karma_upvote or post.forum_id.karma_downvote}"
|
||||
t-attf-data-href="/forum/#{slug(post.forum_id)}/post/#{slug(post)}/downvote"/>
|
||||
<a t-attf-class="vote_down fa fa-caret-down no-decoration #{own_vote == -1 and 'text-warning' or ''} #{((own_vote == -1 and not can_upvote) or not can_downvote) and 'karma_required' or ''}"
|
||||
t-attf-data-karma="#{own_vote == -1 and karma_up or karma_down}"
|
||||
t-attf-data-href="/forum/#{slug(forum)}/post/#{slug(post)}/downvote"/>
|
||||
<t t-raw="0"/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user