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.

45 lines
1.6 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. try:
  18. while self:
  19. batch_delete_messages = self[0:1000]
  20. self -= batch_delete_messages
  21. # do not attach new env to self because it may be
  22. # huge, and the cache is cleaned after each unlink
  23. # so we do not want to much record is the env in
  24. # which we call unlink because odoo would prefetch
  25. # fields, cleared right after.
  26. batch_delete_messages.with_env(new_env).unlink()
  27. new_env.cr.commit()
  28. except Exception as e:
  29. _logger.exception(
  30. "Failed to delete messages : %s", str(e))
  31. # Call by cron
  32. @api.model
  33. def autovacuum_mail_message(self):
  34. rules = self.env['message.vacuum.rule'].search([])
  35. for rule in rules:
  36. domain = rule.get_message_domain()
  37. messages = self.search(domain)
  38. messages.batch_unlink()