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.

40 lines
1.2 KiB

7 years ago
  1. # Copyright 2016 ABF OSIELL <https://osiell.com>
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. import logging
  4. from datetime import datetime, timedelta
  5. from odoo import models, fields, api
  6. _logger = logging.getLogger(__name__)
  7. class AuditlogAutovacuum(models.TransientModel):
  8. _name = 'auditlog.autovacuum'
  9. _description = "Auditlog - Delete old logs"
  10. @api.model
  11. def autovacuum(self, days):
  12. """Delete all logs older than ``days``. This includes:
  13. - CRUD logs (create, read, write, unlink)
  14. - HTTP requests
  15. - HTTP user sessions
  16. Called from a cron.
  17. """
  18. days = (days > 0) and int(days) or 0
  19. deadline = datetime.now() - timedelta(days=days)
  20. data_models = (
  21. 'auditlog.log',
  22. 'auditlog.http.request',
  23. 'auditlog.http.session',
  24. )
  25. for data_model in data_models:
  26. records = self.env[data_model].search(
  27. [('create_date', '<=', fields.Datetime.to_string(deadline))])
  28. nb_records = len(records)
  29. records.unlink()
  30. _logger.info(
  31. "AUTOVACUUM - %s '%s' records deleted",
  32. nb_records, data_model)
  33. return True