Browse Source

Add possibility to filter message to delete by res model domain

12.0-mig-module_prototyper_last
Florian da Costa 6 years ago
parent
commit
acb064a487
  1. 2
      autovacuum_mail_message/__openerp__.py
  2. 8
      autovacuum_mail_message/models/mail_message.py
  3. 31
      autovacuum_mail_message/models/message_vacuum_rule.py
  4. 27
      autovacuum_mail_message/tests/test_message_vacuum_rule.py
  5. 9
      autovacuum_mail_message/views/message_rule_vacuum.xml

2
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)",

8
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(

31
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

27
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)

9
autovacuum_mail_message/views/message_rule_vacuum.xml

@ -8,18 +8,23 @@
<field name="arch" type="xml">
<form string="Message Vacuum Rule">
<sheet>
<group>
<group col="4">
<group col="4">
<group col="4" colspan="4">
<field name="name" colspan="2"/>
<field name="company_id" colspan="2"/>
<field name="message_type" colspan="2"/>
<field name="empty_subtype" colspan="2"/>
<field name="retention_time" colspan="2"/>
<field name="active" colspan="2"/>
</group>
<separator string="Message Models" colspan="4"/>
<field name="model_ids" nolabel="1" colspan="4"/>
<field name="model_id" colspan="4"/>
<field name="model_filter_domain" attrs="{'invisible': [('model_id', '=', False)]}" colspan="4"/>
<separator string="Message Subtypes" colspan="4"/>
<field name="message_subtype_ids" nolabel="1" colspan="4"/>
<separator string="Description" colspan="4"/>
<field name="description" nolabel="1" colspan="4"/>
</group>
</sheet>
</form>

Loading…
Cancel
Save