Skip to content

Commit 08fc559

Browse files
committed
[IMP] stock_vertical_lift: add autovacuum for shuttle commands
1 parent b6252b9 commit 08fc559

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

stock_vertical_lift/models/vertical_lift_command.py

+22
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,25 @@ def create(self, vals_list):
4848
if name:
4949
values["name"] = name
5050
return super().create(vals_list)
51+
52+
@api.autovacuum
53+
def _autovacuum_commands(self):
54+
_logger.info("Vacuuming ``vertical.lift.command`` records")
55+
count = 0
56+
param = self.env["ir.config_parameter"].sudo()
57+
value = param.get_param("stock_vertical_lift.delete_command_after_days")
58+
if value:
59+
try:
60+
days = int(value) # ``value`` is a str, try casting to int
61+
except ValueError:
62+
_logger.warning(
63+
"Cannot convert ``stock_vertical_lift.delete_command_after_days``"
64+
f"'s value to integer: '{value}'"
65+
)
66+
else:
67+
limit = fields.Datetime.add(fields.Datetime.now(), days=-days)
68+
commands = self.search([("create_date", "<", limit)])
69+
if commands:
70+
count = len(commands)
71+
commands.unlink()
72+
_logger.info(f"Vacuumed {count} ``vertical.lift.command`` record(s)")

stock_vertical_lift/tests/test_lift_command.py

+45-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Copyright 2019 Camptocamp SA
22
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
33

4-
from odoo import exceptions
4+
from freezegun import freeze_time
5+
6+
from odoo import exceptions, fields
57
from odoo.tools import mute_logger
68

79
from .common import VerticalLiftCase
@@ -23,3 +25,45 @@ def test_lift_commands(self):
2325
"command": "0|test|1",
2426
}
2527
)
28+
29+
@mute_logger("odoo.addons.stock_vertical_lift.models.vertical_lift_command")
30+
def test_lift_commands_autovacuum(self):
31+
command_obj = self.env["vertical.lift.command"]
32+
param_obj = self.env["ir.config_parameter"].sudo()
33+
param_key = "stock_vertical_lift.delete_command_after_days"
34+
35+
# Create 1 new command for the test
36+
vals = {"name": "Test", "command": "Test", "shuttle_id": self.shuttle.id}
37+
command = command_obj.create(vals)
38+
create_date = command.create_date.date()
39+
40+
# Test 1: param ``stock_vertical_lift.delete_command_after_days`` not set
41+
# => command not deleted
42+
param_obj.search([("key", "=", param_key)]).unlink()
43+
command_obj._autovacuum_commands()
44+
self.assertTrue(command.exists())
45+
46+
# Test 2: param ``stock_vertical_lift.delete_command_after_days`` set, but
47+
# the given value is not an integer
48+
# => command not deleted
49+
param_obj.set_param(param_key, "asd")
50+
command_obj._autovacuum_commands()
51+
self.assertTrue(command.exists())
52+
53+
# Test 3: param ``stock_vertical_lift.delete_command_after_days`` set, but
54+
# the command is created later than the time limit (limit is 10 days, method is
55+
# executed 5 days after the command creation)
56+
# => command not deleted
57+
param_obj.set_param(param_key, 10)
58+
with freeze_time(fields.Date.add(create_date, days=5)):
59+
command_obj._autovacuum_commands()
60+
self.assertTrue(command.exists())
61+
62+
# Test 4: param ``stock_vertical_lift.delete_command_after_days`` set, and
63+
# the command is created earlier than the time limit (limit is 10 days, method
64+
# is executed 15 days after the command creation)
65+
# => command not deleted
66+
param_obj.set_param(param_key, 10)
67+
with freeze_time(fields.Date.add(create_date, days=15)):
68+
command_obj._autovacuum_commands()
69+
self.assertFalse(command.exists())

0 commit comments

Comments
 (0)