[IMP] mail(_*): limit usage of search

Purpose of this commit is to globally improve code performance by limiting
search impact by

  * adding limits when only first found record id used;
  * avoid unnecessary searches when record set can be filtered instead;
  * using cache when accessing ir.model;

Task-2638444
PR odoo/odoo#76005

Co-Authored-By: Thibault Delavallée <tde@odoo.com>
Co-Authored-By: Victor Feyens <vfe@odoo.com>
This commit is contained in:
Victor Feyens
2021-09-08 07:56:04 +00:00
committed by Thibault Delavallée
co-authored by Thibault Delavallée
parent 2f4d582a79
commit f85387ef33
5 changed files with 11 additions and 15 deletions
@@ -175,9 +175,10 @@ class AccountBankStatement(models.Model):
journal_type = self.env.context.get('journal_type', False)
company_id = self.env.company.id
if journal_type:
journals = self.env['account.journal'].search([('type', '=', journal_type), ('company_id', '=', company_id)])
if journals:
return journals[0]
return self.env['account.journal'].search([
('type', '=', journal_type),
('company_id', '=', company_id)
], limit=1)
return self.env['account.journal']
@api.depends('balance_start', 'previous_statement_id')
+4 -7
View File
@@ -56,11 +56,9 @@ class LivechatController(http.Controller):
if available:
# find the country from the request
country_id = False
country_code = request.session.geoip and request.session.geoip.get('country_code') or False
country_code = request.session.geoip.get('country_code') if request.session.geoip else False
if country_code:
country_ids = request.env['res.country'].sudo().search([('code', '=', country_code)])
if country_ids:
country_id = country_ids[0].id
country_id = request.env['res.country'].sudo().search([('code', '=', country_code)], limit=1).id
# extract url
url = request.httprequest.headers.get('Referer')
# find the first matching rule for the given country and url
@@ -111,14 +109,13 @@ class LivechatController(http.Controller):
'is_internal': False,
}
if not channel.rating_ids:
res_model_id = request.env['ir.model'].sudo().search([('model', '=', channel._name)], limit=1).id
values.update({
'res_id': channel.id,
'res_model_id': res_model_id,
'res_model_id': request.env['ir.model']._get_id('mail.channel'),
})
# find the partner (operator)
if channel.channel_partner_ids:
values['rated_partner_id'] = channel.channel_partner_ids[0] and channel.channel_partner_ids[0].id or False
values['rated_partner_id'] = channel.channel_partner_ids[0].id
# if logged in user, set its partner on rating
values['partner_id'] = request.env.user.partner_id.id if request.session.uid else False
# create the rating
@@ -22,7 +22,7 @@ class MailComposeMessage(models.TransientModel):
# use only for allowed models in mass mailing
if self.composition_mode == 'mass_mail' and \
(self.mass_mailing_name or self.mass_mailing_id) and \
self.env['ir.model'].sudo().search([('model', '=', self.model), ('is_mail_thread', '=', True)], limit=1):
self.env['ir.model'].sudo().search_count([('model', '=', self.model), ('is_mail_thread', '=', True)]):
mass_mailing = self.mass_mailing_id
if not mass_mailing:
mass_mailing = self.env['mailing.mailing'].create({
+1 -2
View File
@@ -16,11 +16,10 @@ class MailThread(models.AbstractModel):
# create rating.rating record linked to given rating_value. Using sudo as portal users may have
# rights to create messages and therefore ratings (security should be checked beforehand)
if rating_value:
ir_model = self.env['ir.model'].sudo().search([('model', '=', self._name)])
self.env['rating.rating'].sudo().create({
'rating': float(rating_value) if rating_value is not None else False,
'feedback': rating_feedback,
'res_model_id': ir_model.id,
'res_model_id': self.env['ir.model']._get_id(self._name),
'res_id': self.id,
'message_id': message.id,
'consumed': True,
+1 -2
View File
@@ -131,11 +131,10 @@ class RatingMixin(models.AbstractModel):
rated_partner = self.rating_get_rated_partner_id()
ratings = self.rating_ids.sudo().filtered(lambda x: x.partner_id.id == partner.id and not x.consumed)
if not ratings:
record_model_id = self.env['ir.model'].sudo().search([('model', '=', self._name)], limit=1).id
rating = self.env['rating.rating'].sudo().create({
'partner_id': partner.id,
'rated_partner_id': rated_partner.id,
'res_model_id': record_model_id,
'res_model_id': self.env['ir.model']._get_id(self._name),
'res_id': self.id,
'is_internal': False,
})