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.

88 lines
4.1 KiB

7 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. 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, '<', computed_time.strftime(DEFAULT_SERVER_DATETIME_FORMAT))]
  48. if rule.protect_starred and "starred" in rule.model.field_id.mapped("name"):
  49. domain.append(('starred', '=', False))
  50. if rule.only_inactive and "active" in rule.model.field_id.mapped("name"):
  51. domain.append(('active', '=', False))
  52. _logger.info(_("GC domain: %s"), domain)
  53. records = model.with_context({'active_test': False}).search(domain)
  54. elif rule.state == 'size':
  55. size = rule.size if rule.size_type == 'fixed' else rule.size_parameter_value
  56. count = model.with_context({'active_test': False}).search([], count=True)
  57. if count > size:
  58. limit = count - size
  59. _logger.info(_("GC domain: [] order: %s limit: %s"), rule.size_order, limit)
  60. records = model.with_context({'active_test': False}).search([], order=rule.size_order, limit=limit)
  61. elif rule.state == 'domain':
  62. _logger.info(_("GC domain: %s"), rule.domain)
  63. context = {
  64. 'datetime': datetime,
  65. 'dateutil': dateutil,
  66. 'time': time,
  67. 'uid': self.env.uid,
  68. 'user': self.env.user}
  69. domain = safe_eval(rule.domain, context)
  70. records = model.with_context({'active_test': False}).search(domain)
  71. if rule.only_attachments:
  72. attachments = self.env['ir.attachment'].sudo().search([
  73. ('res_model', '=', rule.model.model),
  74. ('res_id', 'in', records.mapped('id'))])
  75. count = len(attachments)
  76. attachments.unlink()
  77. _logger.info(_("GC'd %s attachments from %s entries"), count, rule.model.model)
  78. else:
  79. count = len(records)
  80. records.unlink()
  81. _logger.info(_("GC'd %s %s records"), count, rule.model.model)
  82. return res