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.

41 lines
1.3 KiB

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