forked from OCA/social
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhooks.py
38 lines (34 loc) · 1.45 KB
/
hooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# -*- coding: utf-8 -*-
# © 2015 Pedro M. Baeza <pedro.baeza@serviciosbaeza.com>
# © 2015 Antonio Espinosa <antonioea@antiun.com>
# © 2015 Javier Iniesta <javieria@antiun.com>
# © 2016 Antonio Espinosa - <antonio.espinosa@tecnativa.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import logging
from openerp import api, SUPERUSER_ID
_logger = logging.getLogger(__name__)
def post_init_hook(cr, registry):
with api.Environment.manage():
env = api.Environment(cr, SUPERUSER_ID, {})
# ACTION 1: Match existing contacts
contact_model = env['mail.mass_mailing.contact']
partner_model = env['res.partner']
contacts = contact_model.with_context(
active_test=False).search([('email', '!=', False)])
_logger.info('Trying to match %d contacts to partner by email',
len(contacts))
for contact in contacts:
partners = partner_model.search([
('email', '=ilike', contact.email)
], limit=1)
if partners:
contact.write({'partner_id': partners.id})
# ACTION 2: Match existing statistics
stat_model = env['mail.mail.statistics']
stats = stat_model.search([
('model', '!=', False),
('res_id', '!=', False),
])
_logger.info('Trying to link %d mass mailing statistics to partner',
len(stats))
stats.partner_link()