Odoo Mindscape. Understand Odoo faster. Explore modules, fields, dependencies and version differences in one place instead of digging through the source code.

Module: Email Marketing

Entity: mailing.mailing

Compute Method: _compute_statistics

Inputs Method Outputs
""" Compute statistics of the mass mailing """
for key in (
            'scheduled', 'expected', 'canceled', 'sent', 'pending', 'delivered', 'opened',
            'process', 'clicked', 'replied', 'bounced', 'failed', 'received_ratio',
            'opened_ratio', 'replied_ratio', 'bounced_ratio',
        ):
            self[key] = False
if not self.ids:
            return
self.env['mailing.trace'].flush_model()
self.env['mailing.mailing'].flush_model()
self.env.cr.execute("""
            SELECT
                m.id as mailing_id,
                COUNT(s.id) AS expected,
                COUNT(s.sent_datetime) AS sent,
                COUNT(s.trace_status) FILTER (WHERE s.trace_status = 'outgoing') AS scheduled,
                COUNT(s.trace_status) FILTER (WHERE s.trace_status = 'cancel') AS canceled,
                COUNT(s.trace_status) FILTER (WHERE s.trace_status = 'process') AS process,
                COUNT(s.trace_status) FILTER (WHERE s.trace_status = 'pending') AS pending,
                COUNT(s.trace_status) FILTER (WHERE s.trace_status in ('sent', 'open', 'reply')) AS delivered,
                COUNT(s.trace_status) FILTER (WHERE s.trace_status in ('open', 'reply')) AS opened,
                COUNT(s.links_click_datetime) AS clicked,
                COUNT(s.trace_status) FILTER (WHERE s.trace_status = 'reply') AS replied,
                COUNT(s.trace_status) FILTER (WHERE s.trace_status = 'bounce') AS bounced,
                COUNT(s.trace_status) FILTER (WHERE s.trace_status = 'error') AS failed
            FROM
                mailing_trace s
            RIGHT JOIN
                mailing_mailing m
                ON (m.id = s.mass_mailing_id)
            WHERE
                m.id IN %s
            GROUP BY
                m.id
        """, (tuple(self.ids), ))
for row in self.env.cr.dictfetchall():
            total = (row['expected'] - row['canceled']) or 1
            row['received_ratio'] = float_round(100.0 * row['delivered'] / total, precision_digits=2)
            row['opened_ratio'] = float_round(100.0 * row['opened'] / total, precision_digits=2)
            row['replied_ratio'] = float_round(100.0 * row['replied'] / total, precision_digits=2)
            row['bounced_ratio'] = float_round(100.0 * row['bounced'] / total, precision_digits=2)
            self.browse(row.pop('mailing_id')).update(row)

Back to entity