From efea7e13c5821d2443d1abf9ac4f31fce760df7a Mon Sep 17 00:00:00 2001 From: "Mahdi Cheikh Rouhou (macr)" Date: Tue, 12 Sep 2023 11:05:24 +0200 Subject: [PATCH] [FIX] base, test_read_group : process duplicated groupbys without lazy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue: ====== When you have duplicate groupbys with `lazy=False` you will get an error. Steps to reproduce the error: ============================= - Install timesheet - Go to timesheet / reporting / By Employee - Add a groupby by month for one employee - I will show an error. Origin of the issue: ==================== The timesheet component will the send a request for read_group having `['date:month', 'date:month']` so we will have duplicated groupby which will be processed later in `_read_group_format_result` which update the value of `row[group]` for each group so the first group will have the original values which is ok but the second one will have the updated values by the first iteration which will result in error. Solution: ========= We need to check that the value is of class BaseModel to update it , it means that's the first time encountered. opw-3497803 closes odoo/odoo#137000 X-original-commit: 91d6dc7ed56f67d41adab90ec0019fbb920d9042 Signed-off-by: Rémy Voet (ryv) --- .../test_read_group/tests/test_date_range.py | 14 ++++++++++++++ odoo/models.py | 4 ++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/odoo/addons/test_read_group/tests/test_date_range.py b/odoo/addons/test_read_group/tests/test_date_range.py index 3b0d85e743e..0071c4146ac 100644 --- a/odoo/addons/test_read_group/tests/test_date_range.py +++ b/odoo/addons/test_read_group/tests/test_date_range.py @@ -112,3 +112,17 @@ class TestDateRange(common.TransactionCase): groups = self.Model.read_group([], fields=['date', 'value'], groupby=['date:quarter', 'date:day'], lazy=False) self.assertEqual(groups, expected) + + def test_duplicate_month(self): + records = self.Model.create([ + {'date': '2022-01-29', 'value': 1}]) + expected = [{ + '__domain': ['&', '&', ('id', 'in', records.ids), '&', ('date', '>=', '2022-01-01'), ('date', '<', '2022-02-01'), ('date', '=', 'January 2022')], + '__count': 1, + '__range': {'date:month': {'from': '2022-01-01', 'to': '2022-02-01'}}, + 'value': 1, + 'date:month': 'January 2022' + }] + groups = self.Model.read_group( + [('id', 'in', records.ids)], fields=['date', 'value'], groupby=['date:month', 'date:month'], lazy=False) + self.assertEqual(groups, expected) diff --git a/odoo/models.py b/odoo/models.py index 4627fd60da1..b96a8a17a84 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -2455,7 +2455,7 @@ class BaseModel(metaclass=MetaModel): additional_domain = [(field_name, '=', value)] if field.type in ('date', 'datetime'): - if value: + if value and isinstance(value, (datetime.date, datetime.datetime)): range_start = value range_end = value + interval if field.type == 'datetime': @@ -2485,7 +2485,7 @@ class BaseModel(metaclass=MetaModel): (field_name, '>=', range_start), (field_name, '<', range_end), ] - else: + elif not value: # Set the __range of the group containing records with an unset # date/datetime field value to False. row.setdefault('__range', {})[group] = False