From b37cfb6a48934b52ca02797861d3817ad34a65ea Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Wed, 7 Oct 2026 01:12:58 +0200 Subject: [PATCH] [ADD] website_mass_mailing_no_duplicate --- website_mass_mailing_no_duplicate/README.rst | 103 ++++ website_mass_mailing_no_duplicate/__init__.py | 2 + .../__manifest__.py | 14 + .../controllers/__init__.py | 1 + .../controllers/form.py | 12 + website_mass_mailing_no_duplicate/i18n/fr.po | 0 .../models/__init__.py | 1 + .../models/mailing_contact.py | 49 ++ .../pyproject.toml | 3 + .../readme/CONTRIBUTORS.md | 1 + .../readme/DESCRIPTION.md | 9 + .../readme/ROADMAP.md | 5 + .../static/description/index.html | 440 ++++++++++++++++++ .../tests/__init__.py | 1 + .../test_website_mass_mailing_no_duplicate.py | 66 +++ 15 files changed, 707 insertions(+) create mode 100644 website_mass_mailing_no_duplicate/README.rst create mode 100644 website_mass_mailing_no_duplicate/__init__.py create mode 100644 website_mass_mailing_no_duplicate/__manifest__.py create mode 100644 website_mass_mailing_no_duplicate/controllers/__init__.py create mode 100644 website_mass_mailing_no_duplicate/controllers/form.py create mode 100644 website_mass_mailing_no_duplicate/i18n/fr.po create mode 100644 website_mass_mailing_no_duplicate/models/__init__.py create mode 100644 website_mass_mailing_no_duplicate/models/mailing_contact.py create mode 100644 website_mass_mailing_no_duplicate/pyproject.toml create mode 100644 website_mass_mailing_no_duplicate/readme/CONTRIBUTORS.md create mode 100644 website_mass_mailing_no_duplicate/readme/DESCRIPTION.md create mode 100644 website_mass_mailing_no_duplicate/readme/ROADMAP.md create mode 100644 website_mass_mailing_no_duplicate/static/description/index.html create mode 100644 website_mass_mailing_no_duplicate/tests/__init__.py create mode 100644 website_mass_mailing_no_duplicate/tests/test_website_mass_mailing_no_duplicate.py diff --git a/website_mass_mailing_no_duplicate/README.rst b/website_mass_mailing_no_duplicate/README.rst new file mode 100644 index 0000000000..948f0e9691 --- /dev/null +++ b/website_mass_mailing_no_duplicate/README.rst @@ -0,0 +1,103 @@ +====================================== +Website - Mass Mailing - No duplicates +====================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:158e89f61f2770f93ad3c67969c8e4726a8e73cc45e4225c218e405d0ca602a4 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fwebsite-lightgray.png?logo=github + :target: https://github.com/OCA/website/tree/18.0/website_mass_mailing_no_duplicate + :alt: OCA/website +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/website-18-0/website-18-0-website_mass_mailing_no_duplicate + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/website&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module extends the ``website`` and ``mass_mailing`` Odoo modules to +prevent duplicates entries in ``mailing.contact`` tables. + +**Rational** + +In Odoo website, if the page contains Customer Form type = 'Subscribe to +one (or many) newsletters', the form creates a new mailing.contact. That +can generate annoying duplicates. + +This module prevent such duplicated entries, updating existing +mailing.contact, if exists. + +**Table of contents** + +.. contents:: + :local: + +Known issues / Roadmap +====================== + +This module is a bugfix. When we use the other widget "Newsletters" +(that is more simple), the widget doesn't create duplicates. + +To Do: check if this module is still necessary in more recent version of +Odoo. + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* GRAP + +Contributors +------------ + +- Sylvain LE GAL + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +.. |maintainer-legalsylvain| image:: https://github.com/legalsylvain.png?size=40px + :target: https://github.com/legalsylvain + :alt: legalsylvain + +Current `maintainer `__: + +|maintainer-legalsylvain| + +This module is part of the `OCA/website `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/website_mass_mailing_no_duplicate/__init__.py b/website_mass_mailing_no_duplicate/__init__.py new file mode 100644 index 0000000000..f7209b1710 --- /dev/null +++ b/website_mass_mailing_no_duplicate/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import controllers diff --git a/website_mass_mailing_no_duplicate/__manifest__.py b/website_mass_mailing_no_duplicate/__manifest__.py new file mode 100644 index 0000000000..40370acc76 --- /dev/null +++ b/website_mass_mailing_no_duplicate/__manifest__.py @@ -0,0 +1,14 @@ +# Copyright 2026 - Today: GRAP https://www.grap.coop +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). +{ + "name": "Website - Mass Mailing - No duplicates", + "summary": "Prevent duplicate of mailing contacts" + " when contact subscribes on the website", + "version": "18.0.1.0.0", + "author": "GRAP, Odoo Community Association (OCA)", + "maintainers": ["legalsylvain"], + "website": "https://github.com/OCA/website", + "license": "AGPL-3", + "category": "Website", + "depends": ["website_mass_mailing"], +} diff --git a/website_mass_mailing_no_duplicate/controllers/__init__.py b/website_mass_mailing_no_duplicate/controllers/__init__.py new file mode 100644 index 0000000000..e555ee7d03 --- /dev/null +++ b/website_mass_mailing_no_duplicate/controllers/__init__.py @@ -0,0 +1 @@ +from . import form diff --git a/website_mass_mailing_no_duplicate/controllers/form.py b/website_mass_mailing_no_duplicate/controllers/form.py new file mode 100644 index 0000000000..b4747088bb --- /dev/null +++ b/website_mass_mailing_no_duplicate/controllers/form.py @@ -0,0 +1,12 @@ +from odoo.tools.misc import clean_context + +from odoo.addons.website.controllers import form + + +class WebsiteForm(form.WebsiteForm): + def insert_record(self, request, model, values, custom, meta=None): + if model.model == "mailing.contact": + request.update_context( + **clean_context({"mailing_contact_from_website": True}) + ) + return super().insert_record(request, model, values, custom, meta=meta) diff --git a/website_mass_mailing_no_duplicate/i18n/fr.po b/website_mass_mailing_no_duplicate/i18n/fr.po new file mode 100644 index 0000000000..e69de29bb2 diff --git a/website_mass_mailing_no_duplicate/models/__init__.py b/website_mass_mailing_no_duplicate/models/__init__.py new file mode 100644 index 0000000000..0794ea1251 --- /dev/null +++ b/website_mass_mailing_no_duplicate/models/__init__.py @@ -0,0 +1 @@ +from . import mailing_contact diff --git a/website_mass_mailing_no_duplicate/models/mailing_contact.py b/website_mass_mailing_no_duplicate/models/mailing_contact.py new file mode 100644 index 0000000000..7ab1e5be9b --- /dev/null +++ b/website_mass_mailing_no_duplicate/models/mailing_contact.py @@ -0,0 +1,49 @@ +# Copyright 2026 - Today: GRAP https://www.grap.coop +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). +import logging + +from odoo import api, models, tools +from odoo.fields import Command + +_logger = logging.getLogger(__name__) + + +class MailingContact(models.Model): + _inherit = "mailing.contact" + + @api.model_create_multi + def create(self, vals_list): + if not self.env.context.get("mailing_contact_from_website", False): + return super().create(vals_list) + new_vals_list = [] + all_existing_contacts = self + for vals in vals_list: + existing_contacts = self.search( + [("email_normalized", "=", tools.email_normalize(vals.get("email")))] + ) + if existing_contacts: + new_list_ids = vals.get("list_ids")[0][2] + existing_list_ids = existing_contacts.mapped("list_ids").ids + to_add_list_ids = list(set(new_list_ids) - set(existing_list_ids)) + all_existing_contacts |= existing_contacts[0] + if to_add_list_ids: + _logger.info( + f"mailing.contact #{existing_contacts[0].id}:" + f" Adding new mailing list {to_add_list_ids}." + ) + existing_contacts[0].write( + { + "list_ids": [ + (Command.link(list_id)) for list_id in to_add_list_ids + ] + } + ) + else: + _logger.info( + f"mailing.contact #{existing_contacts[0].id}:" + " No mailing list to change." + ) + else: + new_vals_list.append(vals) + + return super().create(new_vals_list) | all_existing_contacts diff --git a/website_mass_mailing_no_duplicate/pyproject.toml b/website_mass_mailing_no_duplicate/pyproject.toml new file mode 100644 index 0000000000..4231d0cccb --- /dev/null +++ b/website_mass_mailing_no_duplicate/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/website_mass_mailing_no_duplicate/readme/CONTRIBUTORS.md b/website_mass_mailing_no_duplicate/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..fea5f58349 --- /dev/null +++ b/website_mass_mailing_no_duplicate/readme/CONTRIBUTORS.md @@ -0,0 +1 @@ +- Sylvain LE GAL \ No newline at end of file diff --git a/website_mass_mailing_no_duplicate/readme/DESCRIPTION.md b/website_mass_mailing_no_duplicate/readme/DESCRIPTION.md new file mode 100644 index 0000000000..ccbb8b47a6 --- /dev/null +++ b/website_mass_mailing_no_duplicate/readme/DESCRIPTION.md @@ -0,0 +1,9 @@ +This module extends the ``website`` and ``mass_mailing`` Odoo modules to prevent +duplicates entries in ``mailing.contact`` tables. + +**Rational** + +In Odoo website, if the page contains Customer Form type = 'Subscribe to one (or many) newsletters', the form creates a new mailing.contact. +That can generate annoying duplicates. + +This module prevent such duplicated entries, updating existing mailing.contact, if exists. diff --git a/website_mass_mailing_no_duplicate/readme/ROADMAP.md b/website_mass_mailing_no_duplicate/readme/ROADMAP.md new file mode 100644 index 0000000000..460fac673e --- /dev/null +++ b/website_mass_mailing_no_duplicate/readme/ROADMAP.md @@ -0,0 +1,5 @@ +This module is a bugfix. When we use the other widget "Newsletters" +(that is more simple), the widget doesn't create duplicates. + +To Do: check if this module is still necessary in more recent +version of Odoo. diff --git a/website_mass_mailing_no_duplicate/static/description/index.html b/website_mass_mailing_no_duplicate/static/description/index.html new file mode 100644 index 0000000000..8e9ca2098e --- /dev/null +++ b/website_mass_mailing_no_duplicate/static/description/index.html @@ -0,0 +1,440 @@ + + + + + +Website - Mass Mailing - No duplicates + + + +
+

Website - Mass Mailing - No duplicates

+ + +

Beta License: AGPL-3 OCA/website Translate me on Weblate Try me on Runboat

+

This module extends the website and mass_mailing Odoo modules to +prevent duplicates entries in mailing.contact tables.

+

Rational

+

In Odoo website, if the page contains Customer Form type = ‘Subscribe to +one (or many) newsletters’, the form creates a new mailing.contact. That +can generate annoying duplicates.

+

This module prevent such duplicated entries, updating existing +mailing.contact, if exists.

+

Table of contents

+ +
+

Known issues / Roadmap

+

This module is a bugfix. When we use the other widget “Newsletters” +(that is more simple), the widget doesn’t create duplicates.

+

To Do: check if this module is still necessary in more recent version of +Odoo.

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • GRAP
  • +
+
+
+

Contributors

+
    +
  • Sylvain LE GAL
  • +
+
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

Current maintainer:

+

legalsylvain

+

This module is part of the OCA/website project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/website_mass_mailing_no_duplicate/tests/__init__.py b/website_mass_mailing_no_duplicate/tests/__init__.py new file mode 100644 index 0000000000..6f678aa42e --- /dev/null +++ b/website_mass_mailing_no_duplicate/tests/__init__.py @@ -0,0 +1 @@ +from . import test_website_mass_mailing_no_duplicate diff --git a/website_mass_mailing_no_duplicate/tests/test_website_mass_mailing_no_duplicate.py b/website_mass_mailing_no_duplicate/tests/test_website_mass_mailing_no_duplicate.py new file mode 100644 index 0000000000..04e8b342ea --- /dev/null +++ b/website_mass_mailing_no_duplicate/tests/test_website_mass_mailing_no_duplicate.py @@ -0,0 +1,66 @@ +# Copyright 2026 - Today: GRAP https://www.grap.coop +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). + +from odoo.http import request +from odoo.tests.common import TransactionCase, tagged + +from odoo.addons.website.tools import MockRequest +from odoo.addons.website_mass_mailing_no_duplicate.controllers.form import WebsiteForm + + +@tagged("post_install", "-at_install") +class TestWebsiteForm(TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.website = cls.env.ref("website.default_website") + cls.mailing_list_1 = cls.env.ref("mass_mailing.mailing_list_1") + cls.mailing_list_2 = cls.env.ref("mass_mailing.mailing_list_data") + cls.mailingContact = cls.env["mailing.contact"] + + def _get_mailing_contact_qty(self): + return len(self.mailingContact.with_context(active_test=False).search([])) + + def test_website_form_html_escaping(self): + model = self.env["ir.model"].search([("model", "=", "mailing.contact")]) + + original_qty = self._get_mailing_contact_qty() + WebsiteFormController = WebsiteForm() + + vals = { + "name": "/", + "email": "BOB@BOB.com", + "list_ids": [(6, 0, [self.mailing_list_1.id])], + } + + with MockRequest(self.env, website=self.website): + WebsiteFormController.insert_record(request, model, vals, "") + self.assertEqual( + self._get_mailing_contact_qty(), + original_qty + 1, + "subscribe should create a new mailing contact", + ) + + new_mailing_contact = self.mailingContact.search( + [("email", "=", vals["email"])] + ) + self.assertEqual( + len(new_mailing_contact.list_ids), + 1, + "subscribe to a mailing list should create an entry in list_ids", + ) + + vals["list_ids"] = [(6, 0, [self.mailing_list_2.id])] + WebsiteFormController.insert_record(request, model, vals, "") + self.assertEqual( + self._get_mailing_contact_qty(), + original_qty + 1, + "subscribe to another mailing list should" + " not create a new mailing contact", + ) + self.assertEqual( + len(new_mailing_contact.list_ids), + 2, + "subscribe to another mailing list should" + " create another entry in list_ids", + )