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.

104 lines
4.2 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. from openerp.tools.safe_eval import safe_eval
  8. import datetime
  9. class MessageVacuumRule(models.Model):
  10. _name = "message.vacuum.rule"
  11. _description = "Rules Used to delete message historic"
  12. @api.depends('model_ids')
  13. @api.multi
  14. def _compute_model_id(self):
  15. for rule in self:
  16. if rule.model_ids and len(rule.model_ids) == 1:
  17. rule.model_id = rule.model_ids.id
  18. else:
  19. rule.model_id = False
  20. name = fields.Char(required=True)
  21. company_id = fields.Many2one(
  22. 'res.company', string="Company",
  23. default=lambda self: self.env['res.company']._company_default_get(
  24. 'message.vacuum.rule'))
  25. message_subtype_ids = fields.Many2many(
  26. 'mail.message.subtype', string="Subtypes",
  27. help="Message subtypes concerned by the rule. If left empty, the "
  28. "system won't take the subtype into account to find the "
  29. "messages to delete")
  30. empty_subtype = fields.Boolean(
  31. help="Take also into account messages with no subtypes")
  32. model_ids = fields.Many2many(
  33. 'ir.model', string="Models",
  34. help="Models concerned by the rule. If left empty, it will take all "
  35. "models into account")
  36. model_id = fields.Many2one(
  37. 'ir.model', readonly=True,
  38. compute='_compute_model_id',
  39. help="Technical field used to set attributes (invisible/required, "
  40. "domain, etc...for other fields, like the domain filter")
  41. model_filter_domain = fields.Text(
  42. string='Model Filter Domain')
  43. message_type = fields.Selection([
  44. ('email', 'Email'),
  45. ('comment', 'Comment'),
  46. ('notification', 'System notification'),
  47. ('all', 'All')], required=True)
  48. retention_time = fields.Integer(
  49. required=True, default=365,
  50. help="Number of days the messages concerned by this rule will be "
  51. "keeped in the database after creation. Once the delay is "
  52. "passed, they will be automatically deleted.")
  53. active = fields.Boolean()
  54. description = fields.Text()
  55. @api.multi
  56. @api.constrains('retention_time')
  57. def retention_time_not_null(self):
  58. for rule in self:
  59. if not rule.retention_time:
  60. raise exceptions.ValidationError(
  61. _("The Retention Time can't be 0 days"))
  62. @api.multi
  63. def get_message_domain(self):
  64. self.ensure_one()
  65. today = date.today()
  66. limit_date = today - timedelta(days=self.retention_time)
  67. limit_date = limit_date.strftime(DEFAULT_SERVER_DATE_FORMAT)
  68. message_domain = [('date', '<', limit_date)]
  69. if self.message_type != 'all':
  70. message_domain += [('message_type', '=', self.message_type)]
  71. if self.model_ids:
  72. models = self.model_ids.mapped('model')
  73. message_domain += [('model', 'in', models)]
  74. subtype_ids = self.message_subtype_ids.ids
  75. subtype_domain = []
  76. if subtype_ids and self.empty_subtype:
  77. subtype_domain = ['|', ('subtype_id', 'in', subtype_ids),
  78. ('subtype_id', '=', False)]
  79. elif subtype_ids and not self.empty_subtype:
  80. subtype_domain += [('subtype_id', 'in', subtype_ids)]
  81. elif not subtype_ids and not self.empty_subtype:
  82. subtype_domain += [('subtype_id', '!=', False)]
  83. message_domain += subtype_domain
  84. # Case we want a condition on linked model records
  85. if self.model_id and self.model_filter_domain:
  86. domain = safe_eval(self.model_filter_domain,
  87. locals_dict={'datetime': datetime})
  88. res_model = self.model_id.model
  89. res_records = self.env[res_model].with_context(
  90. active_test=False).search(domain)
  91. res_ids = res_records.ids
  92. message_domain += ['|', ('res_id', 'in', res_ids),
  93. ('res_id', '=', False)]
  94. return message_domain