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.

86 lines
3.2 KiB

  1. # Copyright (C) 2018 Akretion
  2. # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
  3. from odoo import _, api, exceptions, fields, models
  4. class VacuumRule(models.Model):
  5. _name = "vacuum.rule"
  6. _description = "Rules Used to delete message historic"
  7. @api.depends('model_ids')
  8. @api.multi
  9. def _get_model_id(self):
  10. for rule in self:
  11. if rule.model_ids and len(rule.model_ids) == 1:
  12. rule.model_id = rule.model_ids.id
  13. rule.model = rule.model_id.model
  14. else:
  15. rule.model_id = False
  16. rule.model = False
  17. name = fields.Char(required=True)
  18. ttype = fields.Selection(
  19. selection=[('attachment', 'Attachment'),
  20. ('message', 'Message')],
  21. string="Type",
  22. required=True)
  23. filename_pattern = fields.Char(
  24. help=("If set, only attachments containing this pattern will be"
  25. " deleted."))
  26. company_id = fields.Many2one(
  27. 'res.company', string="Company",
  28. default=lambda self: self.env['res.company']._company_default_get(
  29. 'vacuum.rule'))
  30. message_subtype_ids = fields.Many2many(
  31. 'mail.message.subtype', string="Subtypes",
  32. help="Message subtypes concerned by the rule. If left empty, the "
  33. "system won't take the subtype into account to find the "
  34. "messages to delete")
  35. empty_subtype = fields.Boolean(
  36. help="Take also into account messages with no subtypes")
  37. model_ids = fields.Many2many(
  38. 'ir.model', string="Models",
  39. help="Models concerned by the rule. If left empty, it will take all "
  40. "models into account")
  41. model_id = fields.Many2one(
  42. 'ir.model', readonly=True,
  43. compute='_get_model_id',
  44. help="Technical field used to set attributes (invisible/required, "
  45. "domain, etc...for other fields, like the domain filter")
  46. model_filter_domain = fields.Text(
  47. string='Model Filter Domain')
  48. model = fields.Char(
  49. readonly=True,
  50. compute='_get_model_id',
  51. string='Model code'
  52. )
  53. message_type = fields.Selection([
  54. ('email', 'Email'),
  55. ('comment', 'Comment'),
  56. ('notification', 'System notification'),
  57. ('all', 'All')])
  58. retention_time = fields.Integer(
  59. required=True, default=365,
  60. help="Number of days the messages concerned by this rule will be "
  61. "keeped in the database after creation. Once the delay is "
  62. "passed, they will be automatically deleted.")
  63. active = fields.Boolean(default=True)
  64. description = fields.Text()
  65. @api.multi
  66. @api.constrains('retention_time')
  67. def retention_time_not_null(self):
  68. for rule in self:
  69. if not rule.retention_time:
  70. raise exceptions.ValidationError(
  71. _("The Retention Time can't be 0 days"))
  72. def _search_autovacuum_records(self):
  73. self.ensure_one()
  74. model = self.ttype
  75. if model == 'message':
  76. model = 'mail.message'
  77. elif model == 'attachment':
  78. model = 'ir.attachment'
  79. return self.env[model]._get_autovacuum_records(self)