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.

89 lines
4.4 KiB

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