From 648bbbec9a66bf69b00c3a28f4d95bffebf65246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Warnon?= Date: Thu, 12 Dec 2019 12:17:40 +0000 Subject: [PATCH] [FIX] website_slides: fix slide.slide statistics compute method The slide.slide and slide.channel "_compute_slides_statistics" methods do not always assign a default value for the "total_slides" field. This can cause some CacheMiss and seem related to nondeterministic errors on the runbot. Task#2123711 Runbot build error ID 247 closes odoo/odoo#41942 X-original-commit: c240d04ed0518897f76b315cd354ea6606aea645 Signed-off-by: awa-odoo --- addons/website_slides/models/slide_channel.py | 11 ++++++----- addons/website_slides/models/slide_slide.py | 11 ++++++----- addons/website_slides/tests/test_statistics.py | 2 ++ 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/addons/website_slides/models/slide_channel.py b/addons/website_slides/models/slide_channel.py index 7bbd323fff4..b46df1f68e1 100644 --- a/addons/website_slides/models/slide_channel.py +++ b/addons/website_slides/models/slide_channel.py @@ -246,13 +246,14 @@ class Channel(models.Model): """ Compute statistics based on all existing slide types """ slide_types = self.env['slide.slide']._fields['slide_type'].get_values(self.env) keys = ['nbr_%s' % slide_type for slide_type in slide_types] - result = dict((cid, dict((key, 0) for key in keys)) for cid in self.ids) + result = dict((cid, dict((key, 0) for key in keys + ['total_slides'])) for cid in self.ids) for res_group in read_group_res: cid = res_group['channel_id'][0] - result[cid]['total_slides'] = 0 - for slide_type in slide_types: - result[cid]['nbr_%s' % slide_type] += res_group.get('slide_type', '') == slide_type and res_group['__count'] or 0 - result[cid]['total_slides'] += result[cid]['nbr_%s' % slide_type] + slide_type = res_group.get('slide_type') + if slide_type: + slide_type_count = res_group.get('__count', 0) + result[cid]['nbr_%s' % slide_type] = slide_type_count + result[cid]['total_slides'] += slide_type_count return result def _compute_rating_stats(self): diff --git a/addons/website_slides/models/slide_slide.py b/addons/website_slides/models/slide_slide.py index eb91b9f4725..e60b795bb6a 100644 --- a/addons/website_slides/models/slide_slide.py +++ b/addons/website_slides/models/slide_slide.py @@ -288,13 +288,14 @@ class Slide(models.Model): """ Compute statistics based on all existing slide types """ slide_types = self.env['slide.slide']._fields['slide_type'].get_values(self.env) keys = ['nbr_%s' % slide_type for slide_type in slide_types] - result = dict((cid, dict((key, 0) for key in keys)) for cid in self.ids) + result = dict((cid, dict((key, 0) for key in keys + ['total_slides'])) for cid in self.ids) for res_group in read_group_res: cid = res_group['category_id'][0] - result[cid]['total_slides'] = 0 - for slide_type in slide_types: - result[cid]['nbr_%s' % slide_type] += res_group.get('slide_type', '') == slide_type and res_group['__count'] or 0 - result[cid]['total_slides'] += result[cid]['nbr_%s' % slide_type] + slide_type = res_group.get('slide_type') + if slide_type: + slide_type_count = res_group.get('__count', 0) + result[cid]['nbr_%s' % slide_type] = slide_type_count + result[cid]['total_slides'] += slide_type_count return result @api.depends('slide_partner_ids.partner_id') diff --git a/addons/website_slides/tests/test_statistics.py b/addons/website_slides/tests/test_statistics.py index 80ce7e5b7e6..79f0745aa2c 100644 --- a/addons/website_slides/tests/test_statistics.py +++ b/addons/website_slides/tests/test_statistics.py @@ -165,6 +165,7 @@ class TestSlideStatistics(common.SlidesCase): category.nbr_document, len(category.channel_id.slide_ids.filtered(lambda s: s.category_id == category and s.slide_type == 'document'))) + self.assertEqual(self.channel.total_slides, 3, 'The channel should contain 3 slides') self.assertEqual(category.total_slides, 2, 'The first category should contain 2 slides') other_category = self.env['slide.slide'].with_user(self.user_officer).create({ 'name': 'Other Category', @@ -179,3 +180,4 @@ class TestSlideStatistics(common.SlidesCase): self.slide_3.write({'sequence': 6}) self.assertEqual(category.total_slides, 1, 'The first category should contain 1 slide') self.assertEqual(other_category.total_slides, 1, 'The other category should contain 1 slide') + self.assertEqual(self.channel.total_slides, 3, 'The channel should still contain 3 slides')