From acb064a4872fc137c154dae8048e9d7f54557a0c Mon Sep 17 00:00:00 2001 From: Florian da Costa Date: Sat, 16 Feb 2019 18:07:16 +0100 Subject: [PATCH] Add possibility to filter message to delete by res model domain --- autovacuum_mail_message/__openerp__.py | 2 +- .../models/mail_message.py | 8 +++-- .../models/message_vacuum_rule.py | 31 +++++++++++++++++++ .../tests/test_message_vacuum_rule.py | 27 ++++++++++++++++ .../views/message_rule_vacuum.xml | 9 ++++-- 5 files changed, 72 insertions(+), 5 deletions(-) diff --git a/autovacuum_mail_message/__openerp__.py b/autovacuum_mail_message/__openerp__.py index 38b8282e7..ed5d4a032 100644 --- a/autovacuum_mail_message/__openerp__.py +++ b/autovacuum_mail_message/__openerp__.py @@ -4,7 +4,7 @@ { "name": "AutoVacuum Mail Message", - "version": "9.0.1.0.0", + "version": "9.0.1.1.0", "category": "Tools", "website": "https://github.com/OCA/server-tools", "author": "Akretion, Odoo Community Association (OCA)", diff --git a/autovacuum_mail_message/models/mail_message.py b/autovacuum_mail_message/models/mail_message.py index 8d2c331ce..93406d5ed 100644 --- a/autovacuum_mail_message/models/mail_message.py +++ b/autovacuum_mail_message/models/mail_message.py @@ -20,12 +20,16 @@ class MailMessage(models.Model): self.env.cr.dbname).cursor() as new_cr: new_env = api.Environment(new_cr, self.env.uid, self.env.context) - self = self.with_env(new_env) try: while self: batch_delete_messages = self[0:1000] self -= batch_delete_messages - batch_delete_messages.unlink() + # do not attach new env to self because it may be + # huge, and the cache is cleaned after each unlink + # so we do not want to much record is the env in + # which we call unlink because odoo would prefetch + # fields, cleared right after. + batch_delete_messages.with_env(new_env).unlink() new_env.cr.commit() except Exception as e: _logger.exception( diff --git a/autovacuum_mail_message/models/message_vacuum_rule.py b/autovacuum_mail_message/models/message_vacuum_rule.py index b7d63745a..411148bbd 100644 --- a/autovacuum_mail_message/models/message_vacuum_rule.py +++ b/autovacuum_mail_message/models/message_vacuum_rule.py @@ -6,12 +6,23 @@ from datetime import date, timedelta from openerp import _, api, exceptions, fields, models from openerp.tools import DEFAULT_SERVER_DATE_FORMAT +from openerp.tools.safe_eval import safe_eval +import datetime class MessageVacuumRule(models.Model): _name = "message.vacuum.rule" _description = "Rules Used to delete message historic" + @api.depends('model_ids') + @api.multi + def _compute_model_id(self): + for rule in self: + if rule.model_ids and len(rule.model_ids) == 1: + rule.model_id = rule.model_ids.id + else: + rule.model_id = False + name = fields.Char(required=True) company_id = fields.Many2one( 'res.company', string="Company", @@ -28,6 +39,13 @@ class MessageVacuumRule(models.Model): 'ir.model', string="Models", help="Models concerned by the rule. If left empty, it will take all " "models into account") + model_id = fields.Many2one( + 'ir.model', readonly=True, + compute='_compute_model_id', + help="Technical field used to set attributes (invisible/required, " + "domain, etc...for other fields, like the domain filter") + model_filter_domain = fields.Text( + string='Model Filter Domain') message_type = fields.Selection([ ('email', 'Email'), ('comment', 'Comment'), @@ -38,6 +56,8 @@ class MessageVacuumRule(models.Model): help="Number of days the messages concerned by this rule will be " "keeped in the database after creation. Once the delay is " "passed, they will be automatically deleted.") + active = fields.Boolean() + description = fields.Text() @api.multi @api.constrains('retention_time') @@ -70,4 +90,15 @@ class MessageVacuumRule(models.Model): elif not subtype_ids and not self.empty_subtype: subtype_domain += [('subtype_id', '!=', False)] message_domain += subtype_domain + # Case we want a condition on linked model records + if self.model_id and self.model_filter_domain: + domain = safe_eval(self.model_filter_domain, + locals_dict={'datetime': datetime}) + + res_model = self.model_id.model + res_records = self.env[res_model].with_context( + active_test=False).search(domain) + res_ids = res_records.ids + message_domain += ['|', ('res_id', 'in', res_ids), + ('res_id', '=', False)] return message_domain diff --git a/autovacuum_mail_message/tests/test_message_vacuum_rule.py b/autovacuum_mail_message/tests/test_message_vacuum_rule.py index dda3544e0..d0d8343c4 100644 --- a/autovacuum_mail_message/tests/test_message_vacuum_rule.py +++ b/autovacuum_mail_message/tests/test_message_vacuum_rule.py @@ -93,3 +93,30 @@ class TestMessageVacuumRule(common.TransactionCase): } with self.assertRaises(exceptions.ValidationError): self.env['message.vacuum.rule'].create(rule_vals) + + def test_res_model_domain(self): + partner = self.env['res.partner'].create({'name': 'Test Partner'}) + # automatic creation message + self.assertEqual(len(partner.message_ids), 1) + # change date message to simulate it is an old one + partner.message_ids.write({'date': '2017-01-01'}) + partner_model = self.env.ref('base.model_res_partner') + + rule_vals = { + 'name': 'Partners', + 'retention_time': 399, + 'message_type': 'all', + 'model_ids': [(6, 0, [partner_model.id])], + 'model_filter_domain': "[['name', '=', 'Dummy']]", + 'empty_subtype': True, + } + rule = self.env['message.vacuum.rule'].create(rule_vals) + self.message_obj.autovacuum_mail_message() + # no message deleted as the filter does not match + self.assertEqual(len(partner.message_ids), 1) + + rule.write({ + 'model_filter_domain': "[['name', '=', 'Test Partner']]" + }) + self.message_obj.autovacuum_mail_message() + self.assertEqual(len(partner.message_ids), 0) diff --git a/autovacuum_mail_message/views/message_rule_vacuum.xml b/autovacuum_mail_message/views/message_rule_vacuum.xml index e56528bf9..987a16ae1 100644 --- a/autovacuum_mail_message/views/message_rule_vacuum.xml +++ b/autovacuum_mail_message/views/message_rule_vacuum.xml @@ -8,18 +8,23 @@
- - + + + + + + +