[FIX] website_slides: prevent constraint error when changing the type

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) <std@odoo.com>
This commit is contained in:
Mayurrajsinh Rathod
2023-11-30 11:44:45 +00:00
parent e725d5615a
commit de983fc207
2 changed files with 26 additions and 0 deletions
@@ -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()
@@ -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': '<p>Hello</p>'})
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 = {