Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions mass_mailing_partner/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,36 @@

def post_init_hook(env):
# ACTION 1: Match existing contacts
contact_model = env["mailing.contact"]
partner_model = env["res.partner"]
contacts = contact_model.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})
_logger.info("Trying to match contacts to partner by email")

matching_query = """
UPDATE mailing_contact mc
SET partner_id = rp.id
FROM res_partner rp
WHERE mc.email IS NOT NULL
AND rp.email IS NOT NULL
AND LOWER(TRIM(mc.email)) = LOWER(TRIM(rp.email))
"""
delete_older_tag_ids_query = """
DELETE FROM mailing_contact_res_partner_category_rel
WHERE mailing_contact_id IN (SELECT id FROM mailing_contact WHERE partner_id IS NOT NULL)
"""
insert_tag_ids_query = """
INSERT INTO mailing_contact_res_partner_category_rel (mailing_contact_id, res_partner_category_id)
SELECT
mc.id,
rprpcr.category_id
FROM mailing_contact mc
JOIN res_partner_res_partner_category_rel rprpcr ON mc.partner_id = rprpcr.partner_id
WHERE mc.partner_id IS NOT NULL
"""

env.cr.execute(matching_query)
_logger.info("Mailing contacts updated: %d rows affected", env.cr.rowcount)

env.cr.execute(delete_older_tag_ids_query)
env.cr.execute(insert_tag_ids_query)

# ACTION 2: Match existing statistics
stat_model = env["mailing.trace"]
stats = stat_model.search([("model", "!=", False), ("res_id", "!=", False)])
Expand Down
Loading