[FIX] lunch: prevent list index out of range error

Currently, an error occurs when the user tries to add an order with toppings.

This is because before 17.0, [1] got values like `[6, False (1, 2)]`, where
we found the `values['topping_ids_1'][0][2] (ids (1,2))`.  But after 17.0,
[1] got values like `[[4, 1], [4, 2]]`. As a result, it cannot be found in
the `[0][2]` index at [1].

This commit fixes the above issue by using 'convert_to_cach()', which returns
the ids from [[4, 1], [4, 2]]. Apart from that, this commit also improves the
code by adding a loop to get topping IDs for each topping.

[1]-https://github.com/odoo/odoo/blob/039407cf2954ce6298aa8af7aff251a1cefdc39b/addons/lunch/models/lunch_order.py#L99

sentry-4627940064

closes odoo/odoo#142701

Signed-off-by: Sofie Gvaladze (sgv) <sgv@odoo.com>
This commit is contained in:
Mahamadasif Ansari
2024-04-19 09:20:25 +00:00
parent 1df706bd75
commit bcf9e08e9e
+17 -14
View File
@@ -82,25 +82,28 @@ class LunchOrder(models.Model):
self._cr.execute("""CREATE INDEX IF NOT EXISTS lunch_order_user_product_date ON %s (user_id, product_id, date)"""
% self._table)
def _get_topping_ids(self, field, values):
return list(self._fields[field].convert_to_cache(values, self))
def _extract_toppings(self, values):
"""
If called in api.multi then it will pop topping_ids_1,2,3 from values
"""
topping_1_values = values.get('topping_ids_1', False)
topping_2_values = values.get('topping_ids_2', False)
topping_3_values = values.get('topping_ids_3', False)
if self.ids:
# TODO This is not taking into account all the toppings for each individual order, this is usually not a problem
# since in the interface you usually don't update more than one order at a time but this is a bug nonetheless
topping_1 = values.pop('topping_ids_1')[0][2] if topping_1_values else self[:1].topping_ids_1.ids
topping_2 = values.pop('topping_ids_2')[0][2] if topping_2_values else self[:1].topping_ids_2.ids
topping_3 = values.pop('topping_ids_3')[0][2] if topping_3_values else self[:1].topping_ids_3.ids
else:
topping_1 = values['topping_ids_1'][0][2] if topping_1_values else []
topping_2 = values['topping_ids_2'][0][2] if topping_2_values else []
topping_3 = values['topping_ids_3'][0][2] if topping_3_values else []
topping_ids = []
return topping_1 + topping_2 + topping_3
for i in range(1, 4):
topping_field = f'topping_ids_{i}'
topping_values = values.get(topping_field, False)
if self.ids:
# TODO This is not taking into account all the toppings for each individual order, this is usually not a problem
# since in the interface you usually don't update more than one order at a time but this is a bug nonetheless
topping_ids += self._get_topping_ids(topping_field, values.pop(topping_field)) \
if topping_values else self[:1][topping_field].ids
else:
topping_ids += self._get_topping_ids(topping_field, topping_values) if topping_values else []
return topping_ids
@api.constrains('topping_ids_1', 'topping_ids_2', 'topping_ids_3')
def _check_topping_quantity(self):