From c28a826471da64e8e9757e050434229bbf8a65dd Mon Sep 17 00:00:00 2001 From: Kevin Baptiste Date: Thu, 2 Dec 2021 15:21:45 +0000 Subject: [PATCH] [FIX] lunch: bring back the favorites lunch products It was no longer possible to favorite a product and the already favorited products were not showing. closes odoo/odoo#81692 Taskid: 2704569 X-original-commit: 328e0384cc2cff34fc5c4131014d94efb5747cb5 Signed-off-by: Kevin Baptiste --- addons/lunch/models/lunch_product.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/addons/lunch/models/lunch_product.py b/addons/lunch/models/lunch_product.py index 1cbb60519c6..d750cd2c40f 100644 --- a/addons/lunch/models/lunch_product.py +++ b/addons/lunch/models/lunch_product.py @@ -32,7 +32,7 @@ class LunchProduct(models.Model): is_new = fields.Boolean(compute='_compute_is_new') favorite_user_ids = fields.Many2many('res.users', 'lunch_product_favorite_user_rel', 'product_id', 'user_id', check_company=True) - is_favorite = fields.Boolean(compute='_compute_is_favorite') + is_favorite = fields.Boolean(compute='_compute_is_favorite', inverse='_inverse_is_favorite') last_order_date = fields.Date(compute='_compute_last_order_date') @@ -58,7 +58,7 @@ class LunchProduct(models.Model): @api.depends('favorite_user_ids') def _compute_is_favorite(self): for product in self: - product.is_favorite = self.env.user in self.favorite_user_ids + product.is_favorite = self.env.user in product.favorite_user_ids @api.depends_context('uid') def _compute_last_order_date(self): @@ -110,13 +110,20 @@ class LunchProduct(models.Model): raise UserError(_("The following suppliers are archived. You should either unarchive the suppliers or change the supplier of the product.\n%s", '\n'.join(invalid_products.supplier_id.mapped('name')))) return super().toggle_active() - def write(self, values): - if 'is_favorite' in values: - if values['is_favorite']: + def _inverse_is_favorite(self): + """ Handled in the write() """ + return + + def write(self, vals): + if 'is_favorite' in vals: + if vals.pop('is_favorite'): commands = [(4, product.id) for product in self] else: commands = [(3, product.id) for product in self] self.env.user.write({ 'favorite_lunch_product_ids': commands, }) - return super().write(values) + + if not vals: + return True + return super().write(vals)