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.

87 lines
4.4 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  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
  25. from odoo.tools.safe_eval import safe_eval
  26. from odoo.tools.misc import DEFAULT_SERVER_DATETIME_FORMAT
  27. _logger = logging.getLogger(__name__)
  28. _types = {
  29. 'days': lambda interval: datetime.timedelta(days=interval),
  30. 'years': lambda interval: datetime.timedelta(weeks=interval*52),
  31. 'hours': lambda interval: datetime.timedelta(hours=interval),
  32. 'weeks': lambda interval: datetime.timedelta(weeks=interval),
  33. 'months': lambda interval: datetime.timedelta(days=interval*30),
  34. 'minutes': lambda interval: datetime.timedelta(minutes=interval),
  35. }
  36. class AutoVacuum(models.AbstractModel):
  37. _inherit = 'ir.autovacuum'
  38. @api.model
  39. def power_on(self, *args, **kwargs):
  40. res = super(AutoVacuum, self).power_on(*args, **kwargs)
  41. rules = self.env['muk_autovacuum.rules'].sudo().search([], order='sequence asc')
  42. for rule in rules:
  43. if rule.state in ['time', 'size', 'domain']:
  44. model = self.env[rule.model.model].sudo()
  45. records = self.env[rule.model.model]
  46. if rule.state == 'time':
  47. computed_time = datetime.datetime.utcnow() - _types[rule.time_type](rule.time)
  48. domain = [(rule.time_field.name, '<', computed_time.strftime(DEFAULT_SERVER_DATETIME_FORMAT))]
  49. if rule.protect_starred:
  50. for field in rule.model.field_id:
  51. if field.name in ['starred', 'favorite', 'is_starred', 'is_favorite']:
  52. domain.append((field.name, '=', False))
  53. if rule.only_inactive and "active" in rule.model.field_id.mapped("name"):
  54. domain.append(('active', '=', False))
  55. _logger.info(_("GC domain: %s"), domain)
  56. records = model.with_context({'active_test': False}).search(domain)
  57. elif rule.state == 'size':
  58. size = rule.size if rule.size_type == 'fixed' else rule.size_parameter_value
  59. count = model.with_context({'active_test': False}).search([], count=True)
  60. if count > size:
  61. limit = count - size
  62. _logger.info(_("GC domain: [] order: %s limit: %s"), rule.size_order, limit)
  63. records = model.with_context({'active_test': False}).search([], order=rule.size_order, limit=limit)
  64. elif rule.state == 'domain':
  65. _logger.info(_("GC domain: %s"), rule.domain)
  66. domain = safe_eval(rule.domain, rules._get_eval_domain_context())
  67. records = model.with_context({'active_test': False}).search(domain)
  68. if rule.only_attachments:
  69. attachments = self.env['ir.attachment'].sudo().search([
  70. ('res_model', '=', rule.model.model),
  71. ('res_id', 'in', records.mapped('id'))])
  72. count = len(attachments)
  73. attachments.unlink()
  74. _logger.info(_("GC'd %s attachments from %s entries"), count, rule.model.model)
  75. else:
  76. count = len(records)
  77. records.unlink()
  78. _logger.info(_("GC'd %s %s records"), count, rule.model.model)
  79. elif rule.state == 'code':
  80. safe_eval(rule.code.strip(), rules._get_eval_code_context(rule), mode="exec")
  81. return res