From de983fc20737b3474b3791c6b4f9caf76756ecce Mon Sep 17 00:00:00 2001 From: Mayurrajsinh Rathod Date: Fri, 3 Nov 2023 07:01:53 +0000 Subject: [PATCH] [FIX] website_slides: prevent constraint error when changing the type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this commit ================== Modifying the type of content from video to webpage or webpage to video violates the constraint. Technical ========= When we change the 'slide_type' of the slide.slide model from video to any other type, or from webpage to any other type, the value url or html_content is not removed and remains same. However, when switching between video and webpage, the 'exclusion_html_content_and_url' constraint is violated because one of them should be null. After this commit ================= When the type of content is changed, the existing URL or HTML content is removed as they are no longer needed. Task-3474466 closes odoo/odoo#144042 X-original-commit: b68d2f52e9d3d0f18257a17dc1a24db6bee860b1 Signed-off-by: Stéphane Debauche (std) --- addons/website_slides/models/slide_slide.py | 9 +++++++++ addons/website_slides/tests/test_slide_slide.py | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/addons/website_slides/models/slide_slide.py b/addons/website_slides/models/slide_slide.py index eb6d415f096..681ed449f14 100644 --- a/addons/website_slides/models/slide_slide.py +++ b/addons/website_slides/models/slide_slide.py @@ -667,6 +667,15 @@ class Slide(models.Model): values['is_preview'] = True values['is_published'] = True + # if the slide type is changed, remove incompatible url or html_content + # done here to satisfy the SQL constraint + # using a stored-computed field in place does not work + if 'slide_category' in values: + if values['slide_category'] == 'article': + values = {'url': False, **values} + elif values['slide_category'] != 'article': + values = {'html_content': False, **values} + res = super(Slide, self).write(values) if values.get('is_published'): self.date_published = datetime.datetime.now() diff --git a/addons/website_slides/tests/test_slide_slide.py b/addons/website_slides/tests/test_slide_slide.py index efc6a6fc463..df9b855f4e3 100644 --- a/addons/website_slides/tests/test_slide_slide.py +++ b/addons/website_slides/tests/test_slide_slide.py @@ -81,6 +81,23 @@ class TestSlideInternals(slides_common.SlidesCase): slide.user_has_completed = True self.assertTrue(category_slides[0].user_has_completed_category) + def test_change_content_type(self): + """ To prevent constraint violation when changing type from video to article and vice-versa """ + slide = self.env['slide.slide'].with_context(website_slides_skip_fetch_metadata=True).create({ + 'name': 'dummy', + 'channel_id': self.channel.id, + 'slide_category': 'video', + 'is_published': True, + 'url': 'https://youtu.be/W0JQcpGLSFw', + }) + + slide.write({'slide_category': 'article', 'html_content': '

Hello

'}) + self.assertTrue(slide.html_content) + self.assertFalse(slide.url) + + slide.slide_category = 'document' + self.assertFalse(slide.html_content) + class TestVideoFromURL(slides_common.SlidesCase): def test_video_youtube(self): youtube_urls = {