From cdd5a2ea9ae8bd25b0acd238a8325fb14721bacd Mon Sep 17 00:00:00 2001 From: David Vidal Date: Mon, 14 May 2018 16:18:11 +0200 Subject: [PATCH] [FIX] mass_mailing_partner: compute fields performance (#271) - In DB which use large amounts of records and intesive use of mass_mailings, not optimized compute records lead to a drastical decrease of performance --- mass_mailing_partner/__manifest__.py | 5 ++--- mass_mailing_partner/models/res_partner.py | 22 +++++++++++++++++----- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/mass_mailing_partner/__manifest__.py b/mass_mailing_partner/__manifest__.py index ad7c4362..493639d4 100644 --- a/mass_mailing_partner/__manifest__.py +++ b/mass_mailing_partner/__manifest__.py @@ -1,13 +1,12 @@ # -*- coding: utf-8 -*- # Copyright 2015 Pedro M. Baeza -# Copyright 2015 Antonio Espinosa +# Copyright 2015-2016 Antonio Espinosa # Copyright 2015 Javier Iniesta -# Copyright 2016 Antonio Espinosa - # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). { "name": "Link partners with mass-mailing", - "version": "10.0.1.0.2", + "version": "10.0.1.0.3", "author": "Tecnativa, " "Odoo Community Association (OCA)", "website": "https://www.tecnativa.com", diff --git a/mass_mailing_partner/models/res_partner.py b/mass_mailing_partner/models/res_partner.py index 334d6a03..d98c9a02 100644 --- a/mass_mailing_partner/models/res_partner.py +++ b/mass_mailing_partner/models/res_partner.py @@ -36,17 +36,29 @@ class ResPartner(models.Model): _("This partner '%s' is subscribed to one or more " "mailing lists. Email must be assigned.") % partner.name) + @api.multi @api.depends('mass_mailing_contact_ids', 'mass_mailing_contact_ids.opt_out') def _compute_mass_mailing_contacts_count(self): - for reg in self: - reg.mass_mailing_contacts_count = len( - reg.mass_mailing_contact_ids) + contact_data = self.env['mail.mass_mailing.contact'].read_group( + [('partner_id', 'in', self.ids)], ['partner_id'], ['partner_id']) + mapped_data = dict( + [(contact['partner_id'][0], contact['partner_id_count']) + for contact in contact_data]) + for partner in self: + partner.mass_mailing_contacts_count = mapped_data.get(partner.id, + 0) + @api.multi @api.depends('mass_mailing_stats') def _compute_mass_mailing_stats_count(self): - for reg in self: - reg.mass_mailing_stats_count = len(reg.mass_mailing_stats) + contact_data = self.env['mail.mail.statistics'].read_group( + [('partner_id', 'in', self.ids)], ['partner_id'], ['partner_id']) + mapped_data = dict( + [(contact['partner_id'][0], contact['partner_id_count']) + for contact in contact_data]) + for partner in self: + partner.mass_mailing_stats_count = mapped_data.get(partner.id, 0) def write(self, vals): res = super(ResPartner, self).write(vals)