From 35f3d53384a2dd8f29478173f7d8f22bc0b4468f Mon Sep 17 00:00:00 2001 From: CarlosRoca13 Date: Wed, 15 Sep 2021 10:39:06 +0200 Subject: [PATCH] [IMP] stock_lot_filter_available: Add more conditions to avoid errors --- .../models/stock_production_lot.py | 31 ++++++++++++++----- .../tests/test_stock_lot_filter_available.py | 4 +-- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/stock_lot_filter_available/models/stock_production_lot.py b/stock_lot_filter_available/models/stock_production_lot.py index a5a7d28a1..873fe974e 100644 --- a/stock_lot_filter_available/models/stock_production_lot.py +++ b/stock_lot_filter_available/models/stock_production_lot.py @@ -1,6 +1,8 @@ # Copyright 2021 Tecnativa - Carlos Roca # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from odoo import _, api, fields, models +import operator as operator_lib + +from odoo import _, fields, models class StockProductionLot(models.Model): @@ -8,12 +10,16 @@ class StockProductionLot(models.Model): product_qty = fields.Float(search="_search_product_qty") - @api.model - def _is_unsupported_search_operator(self, operator): - return operator != ">" - def _search_product_qty(self, operator, value): - if self._is_unsupported_search_operator(operator) or value: + operator_mapping = { + ">": operator_lib.gt, + ">=": operator_lib.ge, + "<": operator_lib.lt, + "<=": operator_lib.le, + "=": operator_lib.eq, + "!=": operator_lib.ne, + } + if operator not in operator_mapping: raise ValueError(_("Unsupported search")) ids = [] domain = [ @@ -23,7 +29,18 @@ class StockProductionLot(models.Model): groups = self.env["stock.quant"].read_group( domain, ["lot_id", "quantity"], ["lot_id"] ) + involved_lot_ids = [group["lot_id"][0] for group in groups] + if ( + (operator == "=" and value == 0) + or (operator == "<" and value > 0) + or (operator == "<=" and value >= 0) + ): + ids.extend( + self.env["stock.production.lot"] + .search([("id", "not in", involved_lot_ids)]) + .ids + ) for group in groups: - if group["quantity"] > value: + if operator_mapping[operator](group["quantity"], value): ids.append(group["lot_id"][0]) return [("id", "in", ids)] diff --git a/stock_lot_filter_available/tests/test_stock_lot_filter_available.py b/stock_lot_filter_available/tests/test_stock_lot_filter_available.py index 4f0634a1f..f4fdae2e0 100644 --- a/stock_lot_filter_available/tests/test_stock_lot_filter_available.py +++ b/stock_lot_filter_available/tests/test_stock_lot_filter_available.py @@ -28,9 +28,9 @@ class TestStockLotFilterAvailable(SavepointCase): def test_bad_operator(self): with self.assertRaises(ValueError): self.StockProductionLot._search_product_qty("in", [10, 15]) - with self.assertRaises(ValueError): - self.StockProductionLot._search_product_qty(">", 2) def test_good_operator(self): domain = self.StockProductionLot._search_product_qty(">", 0) self.assertTrue(self.lot.id in domain[0][2]) + domain = self.StockProductionLot._search_product_qty(">", 2) + self.assertTrue(self.lot.id in domain[0][2])