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.

73 lines
3.0 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. from datetime import date, timedelta
  5. from openerp import _, api, exceptions, fields, models
  6. from openerp.tools import DEFAULT_SERVER_DATE_FORMAT
  7. class MessageVacuumRule(models.Model):
  8. _name = "message.vacuum.rule"
  9. _description = "Rules Used to delete message historic"
  10. name = fields.Char(required=True)
  11. company_id = fields.Many2one(
  12. 'res.company', string="Company",
  13. default=lambda self: self.env['res.company']._company_default_get(
  14. 'message.vacuum.rule'))
  15. message_subtype_ids = fields.Many2many(
  16. 'mail.message.subtype', string="Subtypes",
  17. help="Message subtypes concerned by the rule. If left empty, the "
  18. "system won't take the subtype into account to find the "
  19. "messages to delete")
  20. empty_subtype = fields.Boolean(
  21. help="Take also into account messages with no subtypes")
  22. model_ids = fields.Many2many(
  23. 'ir.model', string="Models",
  24. help="Models concerned by the rule. If left empty, it will take all "
  25. "models into account")
  26. message_type = fields.Selection([
  27. ('email', 'Email'),
  28. ('comment', 'Comment'),
  29. ('notification', 'System notification'),
  30. ('all', 'All')], required=True)
  31. retention_time = fields.Integer(
  32. required=True, default=365,
  33. help="Number of days the messages concerned by this rule will be "
  34. "keeped in the database after creation. Once the delay is "
  35. "passed, they will be automatically deleted.")
  36. @api.multi
  37. @api.constrains('retention_time')
  38. def retention_time_not_null(self):
  39. for rule in self:
  40. if not rule.retention_time:
  41. raise exceptions.ValidationError(
  42. _("The Retention Time can't be 0 days"))
  43. @api.multi
  44. def get_message_domain(self):
  45. self.ensure_one()
  46. today = date.today()
  47. limit_date = today - timedelta(days=self.retention_time)
  48. limit_date = limit_date.strftime(DEFAULT_SERVER_DATE_FORMAT)
  49. message_domain = [('date', '<', limit_date)]
  50. if self.message_type != 'all':
  51. message_domain += [('message_type', '=', self.message_type)]
  52. if self.model_ids:
  53. models = self.model_ids.mapped('model')
  54. message_domain += [('model', 'in', models)]
  55. subtype_ids = self.message_subtype_ids.ids
  56. subtype_domain = []
  57. if subtype_ids and self.empty_subtype:
  58. subtype_domain = ['|', ('subtype_id', 'in', subtype_ids),
  59. ('subtype_id', '=', False)]
  60. elif subtype_ids and not self.empty_subtype:
  61. subtype_domain += [('subtype_id', 'in', subtype_ids)]
  62. elif not subtype_ids and not self.empty_subtype:
  63. subtype_domain += [('subtype_id', '!=', False)]
  64. message_domain += subtype_domain
  65. return message_domain