You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.3 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright (C) 2018 Akretion
  3. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  4. import logging
  5. import openerp
  6. from openerp import api, models
  7. _logger = logging.getLogger(__name__)
  8. class MailMessage(models.Model):
  9. _inherit = "mail.message"
  10. @api.multi
  11. def batch_unlink(self):
  12. with api.Environment.manage():
  13. with openerp.registry(
  14. self.env.cr.dbname).cursor() as new_cr:
  15. new_env = api.Environment(new_cr, self.env.uid,
  16. self.env.context)
  17. self = self.with_env(new_env)
  18. try:
  19. while self:
  20. batch_delete_messages = self[0:1000]
  21. self -= batch_delete_messages
  22. batch_delete_messages.unlink()
  23. new_env.cr.commit()
  24. except Exception as e:
  25. _logger.exception(
  26. "Failed to delete messages : %s", str(e))
  27. # Call by cron
  28. @api.model
  29. def autovacuum_mail_message(self):
  30. rules = self.env['message.vacuum.rule'].search([])
  31. for rule in rules:
  32. domain = rule.get_message_domain()
  33. messages = self.search(domain)
  34. messages.batch_unlink()