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
4.3 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (C) 2018 MuK IT GmbH
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. ###################################################################################
  19. import time
  20. import logging
  21. import datetime
  22. import dateutil
  23. from odoo import _
  24. from odoo import models, api, fields
  25. from odoo.tools.safe_eval import safe_eval
  26. _logger = logging.getLogger(__name__)
  27. _types = {
  28. 'days': lambda interval: datetime.timedelta(days=interval),
  29. 'years': lambda interval: datetime.timedelta(weeks=interval*52),
  30. 'hours': lambda interval: datetime.timedelta(hours=interval),
  31. 'weeks': lambda interval: datetime.timedelta(weeks=interval),
  32. 'months': lambda interval: datetime.timedelta(days=interval*30),
  33. 'minutes': lambda interval: datetime.timedelta(minutes=interval),
  34. }
  35. class AutoVacuum(models.AbstractModel):
  36. _inherit = 'ir.autovacuum'
  37. @api.model
  38. def power_on(self, *args, **kwargs):
  39. res = super(AutoVacuum, self).power_on(*args, **kwargs)
  40. rules = self.env['muk_autovacuum.rules'].sudo().search([], order='sequence asc')
  41. for rule in rules:
  42. if rule.state in ['time', 'size', 'domain']:
  43. model = self.env[rule.model.model].sudo()
  44. records = self.env[rule.model.model]
  45. if rule.state == 'time':
  46. computed_time = datetime.datetime.utcnow() - _types[rule.time_type](rule.time)
  47. domain = [(rule.time_field.name, '<', fields.Datetime.to_string(computed_time))]
  48. if rule.protect_starred:
  49. for field in rule.model.field_id:
  50. if field.name in ['starred', 'favorite', 'is_starred', 'is_favorite']:
  51. domain.append((field.name, '=', False))
  52. if rule.only_inactive and "active" in rule.model.field_id.mapped("name"):
  53. domain.append(('active', '=', False))
  54. _logger.info(_("GC domain: %s"), domain)
  55. records = model.with_context({'active_test': False}).search(domain)
  56. elif rule.state == 'size':
  57. size = rule.size if rule.size_type == 'fixed' else rule.size_parameter_value
  58. count = model.with_context({'active_test': False}).search([], count=True)
  59. if count > size:
  60. limit = count - size
  61. _logger.info(_("GC domain: [] order: %s limit: %s"), rule.size_order, limit)
  62. records = model.with_context({'active_test': False}).search([], order=rule.size_order, limit=limit)
  63. elif rule.state == 'domain':
  64. _logger.info(_("GC domain: %s"), rule.domain)
  65. domain = safe_eval(rule.domain, rules._get_eval_domain_context())
  66. records = model.with_context({'active_test': False}).search(domain)
  67. if rule.only_attachments:
  68. attachments = self.env['ir.attachment'].sudo().search([
  69. ('res_model', '=', rule.model.model),
  70. ('res_id', 'in', records.mapped('id'))])
  71. count = len(attachments)
  72. attachments.unlink()
  73. _logger.info(_("GC'd %s attachments from %s entries"), count, rule.model.model)
  74. else:
  75. count = len(records)
  76. records.unlink()
  77. _logger.info(_("GC'd %s %s records"), count, rule.model.model)
  78. elif rule.state == 'code':
  79. safe_eval(rule.code.strip(), rules._get_eval_code_context(rule), mode="exec")
  80. return res