diff --git a/mail_message_purge/README.rst b/mail_message_purge/README.rst new file mode 100644 index 0000000000..440c79f3b6 --- /dev/null +++ b/mail_message_purge/README.rst @@ -0,0 +1,94 @@ +================== +Mail Message Purge +================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:38d57fe022ecc270b0aba5103c3450da0e87c68cc969792d05339f782ec79a4b + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fsocial-lightgray.png?logo=github + :target: https://github.com/OCA/social/tree/14.0/mail_message_purge + :alt: OCA/social +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/social-14-0/social-14-0-mail_message_purge + :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/social&target_branch=14.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Over the years of using an Odoo instance, its database can grow to a very large size. +Message from the chatter can quickly add up to this load and some of them have no added +value after their related records has been settled. + +This module allows to configure mail message retention policies on a per model basis. +Go to Settings > Technical > Discuss > Message Purge to add some configuration. + +A cron will run daily to purge old messages following the configurations. A maximum of +1000 messages by model will be delete by day to avoid blocking normal Odoo execution. + +**Table of contents** + +.. contents:: + :local: + +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 +~~~~~~~ + +* Camptocamp + +Contributors +~~~~~~~~~~~~ + +* `Camptocamp `_ + + * Thierry Ducrest + +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-TDu| image:: https://github.com/TDu.png?size=40px + :target: https://github.com/TDu + :alt: TDu + +Current `maintainer `__: + +|maintainer-TDu| + +This module is part of the `OCA/social `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/mail_message_purge/__init__.py b/mail_message_purge/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/mail_message_purge/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/mail_message_purge/__manifest__.py b/mail_message_purge/__manifest__.py new file mode 100644 index 0000000000..b3afda2ec9 --- /dev/null +++ b/mail_message_purge/__manifest__.py @@ -0,0 +1,20 @@ +# Copyright 2024 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) + +{ + "name": "Mail Message Purge", + "summary": "Delete old mail messages based on configuration.", + "version": "14.0.1.0.0", + "category": "Mail", + "author": "Camptocamp,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/social", + "maintainers": ["TDu"], + "license": "AGPL-3", + "installable": True, + "depends": ["mail"], + "data": [ + "data/ir_cron.xml", + "security/ir.model.access.csv", + "views/mail_message_purge_views.xml", + ], +} diff --git a/mail_message_purge/data/ir_cron.xml b/mail_message_purge/data/ir_cron.xml new file mode 100644 index 0000000000..69fb48bcca --- /dev/null +++ b/mail_message_purge/data/ir_cron.xml @@ -0,0 +1,22 @@ + + + + + + Purge Mail Messages + 1 + days + -1 + + + + + code + model._cron_purge_mail_message() + + + diff --git a/mail_message_purge/models/__init__.py b/mail_message_purge/models/__init__.py new file mode 100644 index 0000000000..4a090efd42 --- /dev/null +++ b/mail_message_purge/models/__init__.py @@ -0,0 +1 @@ +from . import mail_message_purge diff --git a/mail_message_purge/models/mail_message_purge.py b/mail_message_purge/models/mail_message_purge.py new file mode 100644 index 0000000000..7e8a6f6f74 --- /dev/null +++ b/mail_message_purge/models/mail_message_purge.py @@ -0,0 +1,85 @@ +# Copyright 2024 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + +import logging + +from dateutil.relativedelta import relativedelta + +from odoo import api, fields, models +from odoo.osv.expression import AND +from odoo.tools import safe_eval + +_logger = logging.getLogger(__name__) + + +class MailMessagePurge(models.Model): + _name = "mail.message.purge" + _description = "Mail Message Purge" + + res_model = fields.Many2one( + comodel_name="ir.model", + domain=[("transient", "=", False), ("is_mail_thread", "=", True)], + string="Target model", + ondelete="cascade", + required=True, + ) + model_name = fields.Char(related="res_model.model", readonly=True) + retention_period = fields.Integer( + default=5, required=True, help="Retention period in years" + ) + domain = fields.Char(string="Filtering domain") + mail_message_subtype_ids = fields.Many2many( + comodel_name="mail.message.subtype", + domain="['|', ('res_model', '=', model_name), ('res_model', '=', False)]", + string="Subtypes", + ) + include_user_notification = fields.Boolean() + active = fields.Boolean(default=True) + + def _domain_mail_message_purge(self): + """Generate a domain to search for mail message to purge.""" + self.ensure_one() + domain = [ + ("model", "=", self.res_model.model), + ( + "create_date", + "<", + fields.Date.today() - relativedelta(years=self.retention_period), + ), + ] + if self.include_user_notification: + domain = AND( + [ + domain, + [("message_type", "in", ("notification", "user_notification"))], + ] + ) + else: + domain = AND([domain, [("message_type", "=", "notification")]]) + if self.mail_message_subtype_ids: + domain = AND( + [domain, [("subtype_id", "in", self.mail_message_subtype_ids.ids)]] + ) + if self.domain: + record_ids = ( + self.env[self.res_model.model] + .search(safe_eval.safe_eval(self.domain)) + .ids + ) + if record_ids: + domain = AND([domain, [("res_id", "in", record_ids)]]) + return domain + + def _purge(self): + domain = self._domain_mail_message_purge() + messages = self.env["mail.message"].search(domain, limit=1000) + _logger.info( + f"Purging {len(messages)} messages for {self.res_model.model}" + ) + messages.unlink() + + @api.model + def _cron_purge_mail_message(self): + records = self.env["mail.message.purge"].search([]) + for record in records: + record._purge() diff --git a/mail_message_purge/readme/CONTRIBUTORS.rst b/mail_message_purge/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000000..df12f53dff --- /dev/null +++ b/mail_message_purge/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* `Camptocamp `_ + + * Thierry Ducrest diff --git a/mail_message_purge/readme/DESCRIPTION.rst b/mail_message_purge/readme/DESCRIPTION.rst new file mode 100644 index 0000000000..9def42b23a --- /dev/null +++ b/mail_message_purge/readme/DESCRIPTION.rst @@ -0,0 +1,9 @@ +Over the years of using an Odoo instance, its database can grow to a very large size. +Message from the chatter can quickly add up to this load and some of them have no added +value after their related records has been settled. + +This module allows to configure mail message retention policies on a per model basis. +Go to Settings > Technical > Discuss > Message Purge to add some configuration. + +A cron will run daily to purge old messages following the configurations. A maximum of +1000 messages by model will be delete by day to avoid blocking normal Odoo execution. diff --git a/mail_message_purge/security/ir.model.access.csv b/mail_message_purge/security/ir.model.access.csv new file mode 100644 index 0000000000..624a8e2666 --- /dev/null +++ b/mail_message_purge/security/ir.model.access.csv @@ -0,0 +1,3 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_mail_message_purge_all,mail.message.purge.all,model_mail_message_purge,,1,0,0,0 +access_mail_message_purge_admin,mail.message.purge.admin,model_mail_message_purge,base.group_system,1,1,1,1 diff --git a/mail_message_purge/static/description/index.html b/mail_message_purge/static/description/index.html new file mode 100644 index 0000000000..486d89ead6 --- /dev/null +++ b/mail_message_purge/static/description/index.html @@ -0,0 +1,437 @@ + + + + + +Mail Message Purge + + + +
+

Mail Message Purge

+ + +

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

+

Over the years of using an Odoo instance, its database can grow to a very large size. +Message from the chatter can quickly add up to this load and some of them have no added +value after their related records has been settled.

+

This module allows to configure mail message retention policies on a per model basis. +Go to Settings > Technical > Discuss > Message Purge to add some configuration.

+

A cron will run daily to purge old messages following the configurations. A maximum of +1000 messages by model will be delete by day to avoid blocking normal Odoo execution.

+

Table of contents

+ +
+

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

+
    +
  • Camptocamp
  • +
+
+
+

Contributors

+ +
+
+

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:

+

TDu

+

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

+

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

+
+
+
+ + diff --git a/mail_message_purge/tests/__init__.py b/mail_message_purge/tests/__init__.py new file mode 100644 index 0000000000..e38301ee72 --- /dev/null +++ b/mail_message_purge/tests/__init__.py @@ -0,0 +1 @@ +from . import test_mail_message_purge diff --git a/mail_message_purge/tests/test_mail_message_purge.py b/mail_message_purge/tests/test_mail_message_purge.py new file mode 100644 index 0000000000..435e179145 --- /dev/null +++ b/mail_message_purge/tests/test_mail_message_purge.py @@ -0,0 +1,69 @@ +# Copyright 2024 Camptocamp SA +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html) + +import datetime + +from dateutil.relativedelta import relativedelta +from freezegun import freeze_time + +from odoo.tests import SavepointCase + + +class TestMailMessagePurge(SavepointCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True)) + cls.customer = cls.env["res.partner"].create({"name": "Customer One"}) + # Use the partner model to test + cls.contact = cls.env["res.partner"].create({"name": "Contact One"}) + cls.mt_comment = cls.env.ref("mail.mt_comment") + cls.mt_note = cls.env.ref("mail.mt_note") + cls.mm1 = cls.contact.message_post(body="Hello", subtype_id=cls.mt_note.id) + cls.mm2 = cls.contact.message_notify( + partner_ids=cls.customer.ids, body="Hello Two" + ) + contact_model = cls.env.ref("base.model_res_partner") + cls.mm_purge = cls.env["mail.message.purge"].create( + { + "res_model": contact_model.id, + "domain": f"[('id', '=', {cls.contact.id})]", + "retention_period": 1, + } + ) + + def test_purge_before_retention_period(self): + """Check message is not deleted during retention period.""" + purge_time = datetime.datetime.now() + relativedelta(days=360) + with freeze_time(purge_time): + self.env["mail.message.purge"]._cron_purge_mail_message() + self.assertTrue(self.mm1.exists()) + self.assertTrue(self.mm2.exists()) + + def test_purge_after_retention_period(self): + """Check message is deleted after the retention period.""" + purge_time = datetime.datetime.now() + relativedelta(days=370) + with freeze_time(purge_time): + self.env["mail.message.purge"]._cron_purge_mail_message() + self.assertFalse(self.mm1.exists()) + self.assertTrue(self.mm2.exists()) + # Check including the user notification message type + self.mm_purge.include_user_notification = True + with freeze_time(purge_time): + self.env["mail.message.purge"]._cron_purge_mail_message() + self.assertFalse(self.mm2.exists()) + + def test_purge_in_relation_to_subtype(self): + """Check deletion or not of messages based on subtype.""" + purge_time = datetime.datetime.now() + relativedelta(days=370) + # Message is of type note, configure purge to delete comment subtype + self.mm_purge.mail_message_subtype_ids = [(6, 0, self.mt_comment.ids)] + with freeze_time(purge_time): + self.env["mail.message.purge"]._cron_purge_mail_message() + # Message not deleted + self.assertTrue(self.mm1.exists()) + # Configure to purge message of subtype note + self.mm_purge.mail_message_subtype_ids = [(6, 0, self.mt_note.ids)] + with freeze_time(purge_time): + self.env["mail.message.purge"]._cron_purge_mail_message() + self.assertFalse(self.mm1.exists()) diff --git a/mail_message_purge/views/mail_message_purge_views.xml b/mail_message_purge/views/mail_message_purge_views.xml new file mode 100644 index 0000000000..574024e58e --- /dev/null +++ b/mail_message_purge/views/mail_message_purge_views.xml @@ -0,0 +1,57 @@ + + + + mail.message.purge.tree + mail.message.purge + 20 + + + + + + + + + + + + mail.message.purge.form + mail.message.purge + +
+ + + + + + + + + + + + + + +
+
+
+ + + Mail Message Purge + mail.message.purge + tree,form + + + +
diff --git a/setup/mail_message_purge/odoo/addons/mail_message_purge b/setup/mail_message_purge/odoo/addons/mail_message_purge new file mode 120000 index 0000000000..8ba14f849a --- /dev/null +++ b/setup/mail_message_purge/odoo/addons/mail_message_purge @@ -0,0 +1 @@ +../../../../mail_message_purge \ No newline at end of file diff --git a/setup/mail_message_purge/setup.py b/setup/mail_message_purge/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/mail_message_purge/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)