summaryrefslogtreecommitdiff
path: root/auditlog/models/autovacuum.py
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2023-02-06 15:29:55 +0700
committerstephanchrst <stephanchrst@gmail.com>2023-02-06 15:29:55 +0700
commit7cfed1e19f2e340d966ed2068176d21a0e8e9834 (patch)
treeec9077cb4f89d23378ef09f9da0adb7135548081 /auditlog/models/autovacuum.py
parent4b3b2d8b1a9a7a72fbe3d623e93dea3802ef0e56 (diff)
add audit log
Diffstat (limited to 'auditlog/models/autovacuum.py')
-rw-r--r--auditlog/models/autovacuum.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/auditlog/models/autovacuum.py b/auditlog/models/autovacuum.py
new file mode 100644
index 0000000..bf56fc5
--- /dev/null
+++ b/auditlog/models/autovacuum.py
@@ -0,0 +1,37 @@
+# Copyright 2016 ABF OSIELL <https://osiell.com>
+# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
+import logging
+from datetime import datetime, timedelta
+
+from odoo import api, fields, models
+
+_logger = logging.getLogger(__name__)
+
+
+class AuditlogAutovacuum(models.TransientModel):
+ _name = "auditlog.autovacuum"
+ _description = "Auditlog - Delete old logs"
+
+ @api.model
+ def autovacuum(self, days, chunk_size=None):
+ """Delete all logs older than ``days``. This includes:
+ - CRUD logs (create, read, write, unlink)
+ - HTTP requests
+ - HTTP user sessions
+
+ Called from a cron.
+ """
+ days = (days > 0) and int(days) or 0
+ deadline = datetime.now() - timedelta(days=days)
+ data_models = ("auditlog.log", "auditlog.http.request", "auditlog.http.session")
+ for data_model in data_models:
+ records = self.env[data_model].search(
+ [("create_date", "<=", fields.Datetime.to_string(deadline))],
+ limit=chunk_size,
+ order="create_date asc",
+ )
+ nb_records = len(records)
+ with self.env.norecompute():
+ records.unlink()
+ _logger.info("AUTOVACUUM - %s '%s' records deleted", nb_records, data_model)
+ return True