[FIX] base, test_read_group : process duplicated groupbys without lazy

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) <ryv@odoo.com>
This commit is contained in:
Mahdi Cheikh Rouhou (macr)
2023-09-29 08:07:02 +00:00
parent c375b8f534
commit efea7e13c5
2 changed files with 16 additions and 2 deletions
@@ -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)
+2 -2
View File
@@ -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