Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][ADD] mail_message_purge #1503

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
94 changes: 94 additions & 0 deletions mail_message_purge/README.rst
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/OCA/social/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 <https://github.com/OCA/social/issues/new?body=module:%20mail_message_purge%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

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

Credits
=======

Authors
~~~~~~~

* Camptocamp

Contributors
~~~~~~~~~~~~

* `Camptocamp <https://www.camptocamp.com>`_

* Thierry Ducrest <thierry.ducrest@camptocamp.com>

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 <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-TDu|

This module is part of the `OCA/social <https://github.com/OCA/social/tree/14.0/mail_message_purge>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions mail_message_purge/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
20 changes: 20 additions & 0 deletions mail_message_purge/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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",
],
}
22 changes: 22 additions & 0 deletions mail_message_purge/data/ir_cron.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2024 Camptocamp SA
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -->
<odoo noupdate="1">

<record model="ir.cron" id="cron_purge_mail_message">
<field name='name'>Purge Mail Messages</field>
<field name='interval_number'>1</field>
<field name='interval_type'>days</field>
Comment on lines +7 to +9

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<field name='name'>Purge Mail Messages</field>
<field name='interval_number'>1</field>
<field name='interval_type'>days</field>
<field name="name">Purge Mail Messages</field>
<field name="interval_number">1</field>
<field name="interval_type">days</field>

<field name="numbercall">-1</field>
<field name="active" eval="True" />
<field name="doall" eval="False" />
<field
name="nextcall"
eval="(datetime.now() + timedelta(days=1)).strftime('%Y-%m-%d 00:00:00')"
/>
<field name="model_id" ref="mail_message_purge.model_mail_message_purge" />
<field name="state">code</field>
<field name="code">model._cron_purge_mail_message()</field>
</record>

</odoo>
1 change: 1 addition & 0 deletions mail_message_purge/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import mail_message_purge
90 changes: 90 additions & 0 deletions mail_message_purge/models/mail_message_purge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# 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, OR
from odoo.tools import safe_eval

_logger = logging.getLogger(__name__)


class MailMessagePurge(models.Model):
_name = "mail.message.purge"
_description = "Mail Message Purge"

def _get_default_mail_message_domain(self):
selection_values = self.env["mail.message"]._fields["message_type"].selection
domains = [[("message_type", "=", value[0])] for value in selection_values]
return str(OR(domains))

model_id = 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="model_id.model", readonly=True)
retention_period = fields.Integer(
default=5, required=True, help="Retention period in years"
)
domain = fields.Char(string="Filtering domain")
all_message_type = fields.Boolean(
string="All message types",
default=True,
help="If unchecked a domain will allow to customize the message to be purged. "

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
help="If unchecked a domain will allow to customize the message to be purged. "
help="If unchecked a domain will allow to customize the message types to be purged. "

"Otherwise all message types will be included in the purge.",
)
mail_message_subtype_ids = fields.Many2many(
comodel_name="mail.message.subtype",
domain="['|', ('res_model', '=', model_name), ('res_model', '=', False)]",
string="Subtypes",
)
mail_message_domain = fields.Char(
string="Message filter", default=_get_default_mail_message_domain
)
active = fields.Boolean(default=True)

def _domain_mail_message_purge(self):
"""Generate a domain to search for mail message to purge."""
self.ensure_one()
# __import__("pdb").set_trace()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# __import__("pdb").set_trace()

domain = [
("model", "=", self.model_id.model),
(
"create_date",
"<",
fields.Date.today() - relativedelta(years=self.retention_period),
),
]
if not self.all_message_type:
domain = AND([domain, safe_eval.safe_eval(self.mail_message_domain)])
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.model_id.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, limit=1000):
domain = self._domain_mail_message_purge()
messages = self.env["mail.message"].search(domain, limit=limit)
_logger.info(f"Purging {len(messages)} messages for {self.model_id.model}")
messages.unlink()

@api.model
def _cron_purge_mail_message(self, limit=1000):
records = self.env["mail.message.purge"].search([])
for record in records:
record._purge(limit=limit)
3 changes: 3 additions & 0 deletions mail_message_purge/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* `Camptocamp <https://www.camptocamp.com>`_

* Thierry Ducrest <thierry.ducrest@camptocamp.com>
9 changes: 9 additions & 0 deletions mail_message_purge/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions mail_message_purge/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading