mirror of
https://github.com/OCA/stock-logistics-warehouse.git
synced 2025-01-21 14:27:28 +02:00
[IMP] stock_vertical_lift: add autovacuum for shuttle commands
This commit is contained in:
@@ -48,3 +48,25 @@ class VerticalLiftCommand(models.Model):
|
||||
if name:
|
||||
values["name"] = name
|
||||
return super().create(vals_list)
|
||||
|
||||
@api.autovacuum
|
||||
def _autovacuum_commands(self):
|
||||
_logger.info("Vacuuming ``vertical.lift.command`` records")
|
||||
count = 0
|
||||
param = self.env["ir.config_parameter"].sudo()
|
||||
value = param.get_param("stock_vertical_lift.delete_command_after_days")
|
||||
if value:
|
||||
try:
|
||||
days = int(value) # ``value`` is a str, try casting to int
|
||||
except ValueError:
|
||||
_logger.warning(
|
||||
"Cannot convert ``stock_vertical_lift.delete_command_after_days``"
|
||||
f"'s value to integer: '{value}'"
|
||||
)
|
||||
else:
|
||||
limit = fields.Datetime.add(fields.Datetime.now(), days=-days)
|
||||
commands = self.search([("create_date", "<", limit)])
|
||||
if commands:
|
||||
count = len(commands)
|
||||
commands.unlink()
|
||||
_logger.info(f"Vacuumed {count} ``vertical.lift.command`` record(s)")
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
# Copyright 2019 Camptocamp SA
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import exceptions
|
||||
from freezegun import freeze_time
|
||||
|
||||
from odoo import exceptions, fields
|
||||
from odoo.tools import mute_logger
|
||||
|
||||
from .common import VerticalLiftCase
|
||||
@@ -23,3 +25,45 @@ class TestLiftCommand(VerticalLiftCase):
|
||||
"command": "0|test|1",
|
||||
}
|
||||
)
|
||||
|
||||
@mute_logger("odoo.addons.stock_vertical_lift.models.vertical_lift_command")
|
||||
def test_lift_commands_autovacuum(self):
|
||||
command_obj = self.env["vertical.lift.command"]
|
||||
param_obj = self.env["ir.config_parameter"].sudo()
|
||||
param_key = "stock_vertical_lift.delete_command_after_days"
|
||||
|
||||
# Create 1 new command for the test
|
||||
vals = {"name": "Test", "command": "Test", "shuttle_id": self.shuttle.id}
|
||||
command = command_obj.create(vals)
|
||||
create_date = command.create_date.date()
|
||||
|
||||
# Test 1: param ``stock_vertical_lift.delete_command_after_days`` not set
|
||||
# => command not deleted
|
||||
param_obj.search([("key", "=", param_key)]).unlink()
|
||||
command_obj._autovacuum_commands()
|
||||
self.assertTrue(command.exists())
|
||||
|
||||
# Test 2: param ``stock_vertical_lift.delete_command_after_days`` set, but
|
||||
# the given value is not an integer
|
||||
# => command not deleted
|
||||
param_obj.set_param(param_key, "asd")
|
||||
command_obj._autovacuum_commands()
|
||||
self.assertTrue(command.exists())
|
||||
|
||||
# Test 3: param ``stock_vertical_lift.delete_command_after_days`` set, but
|
||||
# the command is created later than the time limit (limit is 10 days, method is
|
||||
# executed 5 days after the command creation)
|
||||
# => command not deleted
|
||||
param_obj.set_param(param_key, 10)
|
||||
with freeze_time(fields.Date.add(create_date, days=5)):
|
||||
command_obj._autovacuum_commands()
|
||||
self.assertTrue(command.exists())
|
||||
|
||||
# Test 4: param ``stock_vertical_lift.delete_command_after_days`` set, and
|
||||
# the command is created earlier than the time limit (limit is 10 days, method
|
||||
# is executed 15 days after the command creation)
|
||||
# => command not deleted
|
||||
param_obj.set_param(param_key, 10)
|
||||
with freeze_time(fields.Date.add(create_date, days=15)):
|
||||
command_obj._autovacuum_commands()
|
||||
self.assertFalse(command.exists())
|
||||
|
||||
Reference in New Issue
Block a user